Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #111837
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: sizeof environment variables array = 1? |
| Followup-To | comp.lang.c |
| Date | 2017-06-08 12:43 -0400 |
| Organization | The Pitcher Digital Freehold |
| Message-ID | <ohbuk2$p49$1@dont-email.me> (permalink) |
| References | <ohbpd7$5r3$2@dont-email.me> <878tl2v5ge.fsf@gmail.com> <ohbt7i$jm2$2@dont-email.me> |
Followups directed to: comp.lang.c
DFS wrote:
> On 6/8/2017 11:56 AM, Gareth Owen wrote:
>> DFS <nospam@dfs.com> 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.
>
> Gotcha
>
>
>> 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)
>
> I want to check the Path env variable for the existence of a string:
>
> \GNU\Win32\bin
>
>
> I counted the env variables:
>
> int envars=0;
> while (*env!=NULL) {*env++;envars++;}
> printf("%d\n",envars);
> 65
>
> But then I can't go back and reference the values in env[] again because
> the pointer has moved to the end of the array (or past it?).
>
> What would you suggest?
>
> 1) can I reset the pointer back to the beginning of env[]? How?
By taking another approach.
You can either make a copy of env (the pointer), and walk through the array
using /that/, or
You can walk through the array using an index. Using your code (above) as an
example, this might look like...
int envars=0;
while (env[envars]!=NULL) {envars++;}
printf("%d\n",envars)
but, I'd probably write it something like...
int envars;
/* advance through env[] array, looking for NULL pointer */
for (envars = 0; env[envars] != NULL; ++envars) {/*NOP*/}
printf("%d\n",envars);
> 2) make a copy of the env[] array and use it? How do I make a copy?
That's doable, but not the brightest move. You don't need a copy.
The code to do this would be more complex than you have now.
> 3) can I create a new pointer pointing back to the beginning of env[]?
Using your code, above, you would do something like...
char **beginning;
int envars=0;
beginning = env; /* beginning points the start of env[] array */
while (*env!=NULL) {*env++;envars++;}
printf("%d\n",envars);
> How?
>
>
> Thanks
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 11:13 -0400
Re: sizeof environment variables array = 1? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-08 11:25 -0400
Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 09:34 -0700
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 13:14 -0400
Re: sizeof environment variables array = 1? jameskuyper@verizon.net - 2017-06-08 10:51 -0700
Re: sizeof environment variables array = 1? Gareth Owen <gwowen@gmail.com> - 2017-06-08 16:56 +0100
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 12:18 -0400
Re: sizeof environment variables array = 1? scott@slp53.sl.home (Scott Lurndal) - 2017-06-08 16:35 +0000
Re: sizeof environment variables array = 1? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-08 12:43 -0400
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 13:49 -0400
Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-08 13:58 -0400
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 14:45 -0400
Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 12:10 -0700
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-09 11:21 -0400
Re: sizeof environment variables array = 1? Richard Heathfield <rjh@cpax.org.uk> - 2017-06-09 16:23 +0100
Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-09 11:48 -0400
Re: sizeof environment variables array = 1? Richard Heathfield <rjh@cpax.org.uk> - 2017-06-09 16:53 +0100
Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-09 12:18 -0400
Re: sizeof environment variables array = 1? Richard Heathfield <rjh@cpax.org.uk> - 2017-06-09 17:28 +0100
Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-09 09:12 -0700
Re: sizeof environment variables array = 1? "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-06-10 09:28 +0200
Re: sizeof environment variables array = 1? jak <please@nospam.tnx> - 2017-06-08 22:18 +0200
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 17:31 -0400
Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 15:12 -0700
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-09 11:21 -0400
Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-09 16:39 +0100
Re: sizeof environment variables array = 1? Gareth Owen <gwowen@gmail.com> - 2017-06-10 07:26 +0100
Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-10 13:21 +0100
Re: sizeof environment variables array = 1? gordonb.q80bx@burditt.org (Gordon Burditt) - 2017-06-10 17:32 -0500
Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-11 01:01 +0100
Re: sizeof environment variables array = 1? luser droog <luser.droog@gmail.com> - 2017-06-11 01:28 -0700
Re: sizeof environment variables array = 1? raltbos@xs4all.nl (Richard Bos) - 2017-06-17 18:38 +0000
Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-09 08:41 -0700
Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-09 11:50 -0400
Re: sizeof environment variables array = 1? raltbos@xs4all.nl (Richard Bos) - 2017-06-17 18:37 +0000
Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-18 09:11 -0400
Re: sizeof environment variables array = 1? "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-06-10 09:19 +0200
Re: sizeof environment variables array = 1? Robert Wessel <robertwessel2@yahoo.com> - 2017-06-08 12:44 -0500
Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-08 18:01 +0100
Re: sizeof environment variables array = 1? Gareth Owen <gwowen@gmail.com> - 2017-06-08 18:45 +0100
Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 11:51 -0700
csiph-web