Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > walking through an array of char pointers

Reply
Thread Tools

walking through an array of char pointers

 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-22-2004
gaga <(E-Mail Removed)> wrote:
> Please correct me if i am wrong, isn't this:


> char c[] = "yes"


> really:


> "yes\0"


That creates an array of chars named 'c' and initialized with
the literal string "yes", i.e it's equivalent to writing

char c[] = { 'y', 'e', 's', '\0' };

> If that is true, shouldn't this:


> char *names[] = {"jack", "jill", "zack"};


> really be this:


> "jack\0", "jill\0", "zack\0", \0


No. A literal string like "zack" automatically has a '\0' at
the end. But the 'names' array is an array of char pointers.
So it just consists of 3 char pointers, each initialized to
point to a literal string.

> why isn't a final null terminator appened after an array of pointers?


What would a "null terminator" for an array of char pointers be?
A pointer to an empty string? A NULL pointer?

Arrays are never automatically "null teminated". The only case
where you get an automatic "null terminator" is when you use
a literal string, i.e. something that's enclosed in double
quotes. Then the compiler automatically appends a '\0' character
to the characters you write between the quotes. But a literal
string isn't an array, you just can use it to e.g. initialize
an array of chars.
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-22-2004
CBFalconer <(E-Mail Removed)> wrote:
> Joe Wright wrote:
>>

> ... snip ...
>>
>> char *names[] = {"jack", "jill", "zack", NULL };
>> char **np = names;
>> while (*np) printf("%s\n", *np++);
>>
>> Note that names is an array of pointers to char. 'names' decays
>> to a pointer to the array's first element. A pointer to char. So
>> the assignment is 'np = names;', not 'np = &names;'.


> I don't think so. names is locally declared, so the type being
> used in the assignment is not subject to decay to a pointer. We


Locally or not, with

char **np = names;

'names' is used in value context and thus decays to a pointer. It's
equivalent to

char **np = &names[ 0 ];

which is just what's needed. On the other hand

char **np = &names;

gives you a compiler warning about assignment from incompatible
pointer type because you try to assign a pointer to an array of
pointers to a pointer to pointer.
Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-22-2004
(E-Mail Removed)-berlin.de wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>> Joe Wright wrote:
>>>

>> ... snip ...
>>>
>>> char *names[] = {"jack", "jill", "zack", NULL };
>>> char **np = names;
>>> while (*np) printf("%s\n", *np++);
>>>
>>> Note that names is an array of pointers to char. 'names' decays
>>> to a pointer to the array's first element. A pointer to char. So
>>> the assignment is 'np = names;', not 'np = &names;'.

>
>> I don't think so. names is locally declared, so the type being
>> used in the assignment is not subject to decay to a pointer. We

>
> Locally or not, with
>
> char **np = names;
>
> 'names' is used in value context and thus decays to a pointer. It's
> equivalent to
>
> char **np = &names[ 0 ];
>
> which is just what's needed. On the other hand
>
> char **np = &names;
>
> gives you a compiler warning about assignment from incompatible
> pointer type because you try to assign a pointer to an array of
> pointers to a pointer to pointer.


I guess I am guilty of fuzzy thinking. Mmm - when was the last
time I made a mistake in public ...

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison


 
Reply With Quote
 
gaga
Guest
Posts: n/a
 
      08-22-2004
(E-Mail Removed)-berlin.de wrote in message news:<(E-Mail Removed)>...
> gaga <(E-Mail Removed)> wrote:
> > Please correct me if i am wrong, isn't this:

>
> > char c[] = "yes"

>
> > really:

>
> > "yes\0"

>
> That creates an array of chars named 'c' and initialized with
> the literal string "yes", i.e it's equivalent to writing
>
> char c[] = { 'y', 'e', 's', '\0' };
>
> > If that is true, shouldn't this:

>
> > char *names[] = {"jack", "jill", "zack"};

>
> > really be this:

>
> > "jack\0", "jill\0", "zack\0", \0

>
> No. A literal string like "zack" automatically has a '\0' at
> the end. But the 'names' array is an array of char pointers.
> So it just consists of 3 char pointers, each initialized to
> point to a literal string.
>
> > why isn't a final null terminator appened after an array of pointers?

>
> What would a "null terminator" for an array of char pointers be?
> A pointer to an empty string? A NULL pointer?
>
> Arrays are never automatically "null teminated". The only case
> where you get an automatic "null terminator" is when you use
> a literal string, i.e. something that's enclosed in double
> quotes. Then the compiler automatically appends a '\0' character
> to the characters you write between the quotes. But a literal
> string isn't an array, you just can use it to e.g. initialize
> an array of chars.
> Regards, Jens


Jens,
Thanks for your suggestions and perspective. Your responses
will allow me to properly (finally) explain my source of confusion.

K&R(ansi) pages 114 and 115.

on 114 an array of char pointers is depicted as such:

[ * ]-----> illegal month\0
[ * ]-----> jan\0
[ * ]-----> feb\0
[ * ]-----> mar\0

which would confirm your remark,
"But the 'names' array is an array of char pointers. So it just consists of 3
char pointers, each initialized to point to a literal string."

and yet, on the very next page, when discussing the main() params, argc
and argv, argv which is an array of char pointers (or pointer to pointers),
it is depicted as such:

argv:
[ * ]-----> [ * ]-----> echo\0
[ * ]-----> hello\0
[ * ]-----> world\0
[ 0 ]

Interesting, 0, (or NULL), is appended for us onto the argv array of pointers,
but not to a locally declared array of pointers.

allow me to restate my original question...


why does this work?


int main(int argc, char *argv[])
{
while (*argv)
printf("\%s\n",*argv++);
return 0;
}


But this doesn't:


int main()
{
char *names[] = {"jack", "jill", "zack"};
while (*names)
printf("%s\n",*names++);
return 0;
}


Why is a NULL appended to argv, but not to locally declared
array of char pointers?

- gaga
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-22-2004
gaga <(E-Mail Removed)> wrote:
> K&R(ansi) pages 114 and 115.


> on 114 an array of char pointers is depicted as such:


> [ * ]-----> illegal month\0
> [ * ]-----> jan\0
> [ * ]-----> feb\0
> [ * ]-----> mar\0


> which would confirm your remark,
> "But the 'names' array is an array of char pointers. So it just consists of 3
> char pointers, each initialized to point to a literal string."


> and yet, on the very next page, when discussing the main() params, argc
> and argv, argv which is an array of char pointers (or pointer to pointers),
> it is depicted as such:


> argv:
> [ * ]-----> [ * ]-----> echo\0
> [ * ]-----> hello\0
> [ * ]-----> world\0
> [ 0 ]


> Interesting, 0, (or NULL), is appended for us onto the argv array of
> pointers, but not to a locally declared array of pointers.


Yes, but the extra NULL pointer isn't there because argv is an array
of pointers or because it's coming from somewhere else but because,
according to the requirements, argv must be set up that way (i.e. to
have a NULL pointer as the last argument). Under some operating
systems you can execute a new program from within your own and in
that case you have to assemble argv for the new program yourself.
And thus you have to create an array of pointers with one more
element than you want to pass to the new program and have to
explicitely set that extra pointer to NULL in order to make that
array an array that can be used as the argv array. No magic involved
and nothing of that sort (i.e. appending an extra NULL pointer) gets
done for you automatically. That's exactly the same thing you must
do with your 'names' array.

> allow me to restate my original question...


> why does this work?


> int main(int argc, char *argv[])
> {
> while (*argv)
> printf("\%s\n",*argv++);


Because you're guaranteed that argv always has an extra element
that's set to NULL. It has been set up that way by whatever
invokes your program.

> return 0;
> }


> But this doesn't:


> int main()
> {
> char *names[] = {"jack", "jill", "zack"};
> while (*names)
> printf("%s\n",*names++);
> return 0;
> }


Because your 'names' array isn't argv and hasn't been set up like
argv would. To do that you have to add the extra NULL pointer at
the end.
Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-22-2004
Joe Wright wrote:
>
> pete wrote:


> > for (np = names; *np != NULL; ++np) {
> > puts(*np);
> > }


> Ok, printf is too complicated but for is too.


> char **np = names;
> while (*np) puts(*np++);


As points of style,
I prefer to compare pointers against NULL explicitly,
I always use compound statements with loops, ifs and elses,
and I prefer not to have side effects in function arguments.

--
pete
 
Reply With Quote
 
gaga
Guest
Posts: n/a
 
      08-22-2004
(E-Mail Removed)-berlin.de wrote in message news:<(E-Mail Removed)>...
> gaga <(E-Mail Removed)> wrote:
> > K&R(ansi) pages 114 and 115.

>
> > on 114 an array of char pointers is depicted as such:

>
> > [ * ]-----> illegal month\0
> > [ * ]-----> jan\0
> > [ * ]-----> feb\0
> > [ * ]-----> mar\0

>
> > which would confirm your remark,
> > "But the 'names' array is an array of char pointers. So it just consists of 3
> > char pointers, each initialized to point to a literal string."

>
> > and yet, on the very next page, when discussing the main() params, argc
> > and argv, argv which is an array of char pointers (or pointer to pointers),
> > it is depicted as such:

>
> > argv:
> > [ * ]-----> [ * ]-----> echo\0
> > [ * ]-----> hello\0
> > [ * ]-----> world\0
> > [ 0 ]

>
> > Interesting, 0, (or NULL), is appended for us onto the argv array of
> > pointers, but not to a locally declared array of pointers.

>
> Yes, but the extra NULL pointer isn't there because argv is an array
> of pointers or because it's coming from somewhere else but because,
> according to the requirements, argv must be set up that way (i.e. to
> have a NULL pointer as the last argument). Under some operating
> systems you can execute a new program from within your own and in
> that case you have to assemble argv for the new program yourself.
> And thus you have to create an array of pointers with one more
> element than you want to pass to the new program and have to
> explicitely set that extra pointer to NULL in order to make that
> array an array that can be used as the argv array. No magic involved
> and nothing of that sort (i.e. appending an extra NULL pointer) gets
> done for you automatically. That's exactly the same thing you must
> do with your 'names' array.
>
> > allow me to restate my original question...

>
> > why does this work?

>
> > int main(int argc, char *argv[])
> > {
> > while (*argv)
> > printf("\%s\n",*argv++);

>
> Because you're guaranteed that argv always has an extra element
> that's set to NULL. It has been set up that way by whatever
> invokes your program.
>
> > return 0;
> > }

>
> > But this doesn't:

>
> > int main()
> > {
> > char *names[] = {"jack", "jill", "zack"};
> > while (*names)
> > printf("%s\n",*names++);
> > return 0;
> > }

>
> Because your 'names' array isn't argv and hasn't been set up like
> argv would. To do that you have to add the extra NULL pointer at
> the end.
> Regards, Jens


Jens,
Thank you. Now I get.
Thanks for your patience and explanations.
Thanks to everyone else who contributed as well. Much appreciated.

- gaga
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-23-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (gaga) wrote:
> I can't seem to get this to work:
>
> char *names[3];
>
> names[0] = "jack";
> names[1] = "jill";
> names[2] = "zack";
>
> I was under the impression names is actually:
>
> [] --> "jack\0"
> [] --> "jill\0"
> [] --> "zack\0"
> [] --> \0
>


Explain how you think 4 values can fit into 3 memory locations?
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      08-23-2004
pete wrote:

> Joe Wright wrote:
>
>>pete wrote:

>
>
>>> for (np = names; *np != NULL; ++np) {
>>> puts(*np);
>>> }

>
>
>>Ok, printf is too complicated but for is too.

>
>
>> char **np = names;
>> while (*np) puts(*np++);

>
>
> As points of style,
> I prefer to compare pointers against NULL explicitly,
> I always use compound statements with loops, ifs and elses,
> and I prefer not to have side effects in function arguments.


A chacun son gout.

I seriously prefer 'if (p)' over 'if (p != NULL)'.

By 'compound statememts' I suppose you mean curly braces. The use of
curly braces to encompass one statement annoys me.

Expressing 'foo(a++)' is well defined. That a is incremented is not
a side effect. It is an explicit part of the language.

I hope this disagreement doesn't mean we can't play anymore. :=)
--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-23-2004
Joe Wright wrote:
>
> pete wrote:


> I prefer not to have side effects in function arguments.


> Expressing 'foo(a++)' is well defined. That a is incremented is not
> a side effect. It is an explicit part of the language.


"side effect" is technical term in C.

N869
5.1.2.3 Program execution
[#2] Accessing a volatile object, modifying an object,
modifying a file, or calling a function that does any of
those operations are all side effects, which are changes
in the state of the execution environment. Evaluation of an
expression may produce side effects.

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 6 09-01-2006 05:57 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments