Click to See Complete Forum and Search --> : Termination of character string array


Pistol_Pete
October 23rd, 2001, 05:27 AM
How do I terminate character string array?

The following example does not work!!!

void main (void) {

int i;
char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "" };

// notice "" - empty string at the end of array

char **work_day;

work_day=workdays;

while (*work_day) printf("%d\n", i++);

exit(1);
}

This example will print numbers from 1 to xxx instead of printing from 1 to 6.

I need explanation why "" at the end of array isn't treated as \n?

Tomislav

Pistol_Pete
October 23rd, 2001, 05:33 AM
Sorry instead of \n should be \0!!!

balaji_n29
October 23rd, 2001, 07:25 AM
Hi,
What you had done is u never incremened the pointer to point to the next location. U can do that like this
[ccode]
void main (void) {

int i=0;
char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "" };

// notice "" - empty string at the end of array

char **work_day;

work_day=&workdays[0];

while (strlen(*work_day)>0)
{
printf("%s\n", *work_day);
i++;
work_day=&workdays[i];


}


}
[/code]

Regards,
Balaji.
If it helps rate it

Igor Soukhov
October 23rd, 2001, 07:36 AM
Another approach:


int main (void)
{
char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

for (int i = 0; i < sizeof(workdays)/sizeof(*workdays); ++i)
printf("%d\0", i);

exit(1);
}







Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)

Best regards,

-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com

Member of Russian Software Developer Network http://rsdn.ru

medleyj
October 24th, 2001, 12:55 PM
The simple answer is that '\n' and "" are not the same in C++. Either use '\n' and '\n' or "" and '\0'.

James Curran
October 26th, 2001, 03:08 PM
There are two problems with this.
The first as balijj pointed out, is that you are not incrementing work_day, so that it always points to workdays[0]. Since that is never 00, the loop never terminates.

That's easy enough to fixed.while (*work_day++) printf("%d\n", i++);


But that's not going to solve your problem, which is a bit more fundemental.

work_day is a pointer to a char pointer, or, more exactly, a pointer into an array of char pointer. So first work_day points to workdays[0], then, after incrementing, it points to workdays[1], and so on.

workdays[0], workdays[1], etc are themselves pointer. workdays[0] *points* to the text literal "Monday", and workdays[5] *points* to the text literal "".

Now, let's look at your test while (*work_day)

which says "loop until what work_day points to is zero". But work_day only points to char*, and each of the char* in workdays have non-zero values, that is, they all point to something. (What that something is, is irrelevant).

So, we don't want workdays[5] to *point* to a zero. workdays[5] must *be* a zero. The is also easily accomplished:char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 0 };






Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.