Path: csiph.com!news.mixmin.net!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Thu, 08 Jun 2017 10:56:17 -0500 From: Gareth Owen Newsgroups: comp.lang.c Subject: Re: sizeof environment variables array = 1? References: Date: Thu, 08 Jun 2017 16:56:17 +0100 Message-ID: <878tl2v5ge.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Cancel-Lock: sha1:NiPPWOPIKrNo1tHTRznwZUYDAAI= MIME-Version: 1.0 Content-Type: text/plain Lines: 31 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-2FVNatEUO6bAqUCQPdyOv3IoivVWitduZdsgUhWYYuR7Eh0Pwe8Px8x+qtoQX6UmvS1npNvGybuYUk2!xgf9aLAYW293aLKJDYtxSSdeJ31YXW9RxmzIgnnEySVvx+nowFOoEzaWpszDbeeennlAEnc= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1682 Xref: csiph.com comp.lang.c:111826 DFS writes: > int main(int argc, char *argv[], char *env[]) > { > printf("%d\n",sizeof(env)/sizeof(env[0])); > return(0); > ) > > Output is 1. > > Why? Anything that looks like an array object - when used in the argument list of a function, is not an array object, but "decays" into a pointer. so in int foo(char *arr1[12]) { char *arr2[12]; char **arr3; } arr1 and arr3 have the same type, even though arr1 and arr2 have the same declaration. A misfeature, but an explicable one. There is no way to obtain the size of arr1 in foo() without passing it in explicitly (or counting them using a sentinel such as NULL to mark the end, as is done with environment variables)