Knowing the end of an array of char*

E

Eric

Suppose I have an arbitrarily long array of character pointers that
should be looped through until the end is reached. What is the correct
way to mark the end of the strings to be processed? E.g.:


#include <stdio.h>

int main(int argc, char **argv)
{
char *month[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
0 /* <== should this be 0 or NULL? */
};

int i = 0;

while (month) { /* Is this safe to do in all cases? */
printf("%s\n", name);
i++;
}

return 0;
}
 
J

Jens Thoms Toerring

Eric said:
Suppose I have an arbitrarily long array of character pointers that
should be looped through until the end is reached. What is the correct
way to mark the end of the strings to be processed? E.g.:
#include <stdio.h>
int main(int argc, char **argv)
{
char *month[] = {

It might be better to make that

const char *month[] = {

since the pointers all point to non-modifiable strings.
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
0 /* <== should this be 0 or NULL? */

It's a matter of taste. I personally would use NULL to make
it clear that I mean a pointer, but 0 will automatically be
converted to the appropriate NULL pointer.
int i = 0;
while (month) { /* Is this safe to do in all cases? */
printf("%s\n", name);
i++;
}


If you can be sure that the last element of the array is
a NULL pointer (and you'll never have a situation where
any of the pointers in between gets set to NULL) than this
is fine. Looping over all arguments in argv is often done
in exactly this way, it's a very common technique.
return 0;
}
Regarss, Jens
 
S

Sjouke Burry

Eric said:
Suppose I have an arbitrarily long array of character pointers that
should be looped through until the end is reached. What is the correct
way to mark the end of the strings to be processed? E.g.:


#include <stdio.h>

int main(int argc, char **argv)
{
char *month[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
0 /* <== should this be 0 or NULL? */
};

int i = 0;

while (month) { /* Is this safe to do in all cases? */
printf("%s\n", name);
i++;
}

return 0;
}

In a program you should always write what you mean,
and use either ""(empty) or NULL.
Use zero only when you actually mean that, only in c++
they always mean the same.
Personally I always use "" to signal the end of the list.
 
E

Eric

Sjouke said:
Eric said:
Suppose I have an arbitrarily long array of character pointers that
should be looped through until the end is reached. What is the correct
way to mark the end of the strings to be processed? E.g.:


#include <stdio.h>

int main(int argc, char **argv)
{
char *month[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
0 /* <== should this be 0 or NULL? */
};

int i = 0;

while (month) { /* Is this safe to do in all cases? */
printf("%s\n", name);
i++;
}

return 0;
}

In a program you should always write what you mean,
and use either ""(empty) or NULL.
Use zero only when you actually mean that, only in c++
they always mean the same.
Personally I always use "" to signal the end of the list.


That seemed wrong so I tried it:

const char *name[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
"","foo", NULL
};

Sure enough, "foo" got printed.
 
J

James Kuyper

Sjouke said:
Eric said:
Suppose I have an arbitrarily long array of character pointers that
should be looped through until the end is reached. What is the correct
way to mark the end of the strings to be processed? E.g.:


#include <stdio.h>

int main(int argc, char **argv)
{
char *month[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
0 /* <== should this be 0 or NULL? */
};

int i = 0;

while (month) { /* Is this safe to do in all cases? */
printf("%s\n", name);
i++;
}

return 0;
}

In a program you should always write what you mean,
and use either ""(empty) or NULL.
Use zero only when you actually mean that, only in c++
they always mean the same.


NULL and 0 are both null pointer constants; when they occur in a pointer
context, they will both be automatically converted into null pointers of
the appropriate type. This is equally true in both C and in C++.

I agree that it is better to use NULL, because it makes the intent
clearer. However, the difference between C and C++ is not about '0'. The
difference is that in C++, a null pointer constant cannot have a pointer
type, while in C, a null pointer constant can have the type 'void*'.

In C++, I would use std::nullptr (if it's supported) instead of either 0
or NULL. It has the advantage that it does not have integral type, and
cannot be implicitly converted to an integral type.
 
J

James Kuyper

Eric said:
Sjouke said:
Eric said:
Suppose I have an arbitrarily long array of character pointers that
should be looped through until the end is reached. What is the
correct way to mark the end of the strings to be processed? E.g.:


#include <stdio.h>

int main(int argc, char **argv)
{
char *month[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
0 /* <== should this be 0 or NULL? */
};

int i = 0;

while (month) { /* Is this safe to do in all cases? */
printf("%s\n", name);
i++;
}

return 0;
}

In a program you should always write what you mean,
and use either ""(empty) or NULL.
Use zero only when you actually mean that, only in c++
they always mean the same.
Personally I always use "" to signal the end of the list.


That seemed wrong so I tried it:

const char *name[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
"","foo", NULL
};

Sure enough, "foo" got printed.


I don't think using "" to terminate the list is a good idea, but it can
be made to work, with the apprpriate code change. He would have to use
something equivalent to while(*month) rather than while(month); I
presume he didn't mention the need for such a code change because he
assumed it was obvious.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top