Is argv[0] guaranteed to be a valid pointer?

A

August Derleth

What does the C Standard (any of them) say about this issue? Can a
conformant implementation allow argv[0] == NULL? Can a conformant
implementation allow argv[0] to cause the machine to segfault if it is
dereferenced?

I understand, I think, the difference between hosted implementations and
freestanding ones (as in microcontrollers). I just want to know if a
program to be compiled by a conformant compiler needs to be careful with
argv[0].
 
K

Kevin Easton

August Derleth said:
What does the C Standard (any of them) say about this issue? Can a
conformant implementation allow argv[0] == NULL? Can a conformant
implementation allow argv[0] to cause the machine to segfault if it is
dereferenced?

argc can be 0, which case argv[0] == NULL.

argv[0] must be either NULL, or a pointer to a string. If argc is
non-zero, argv[0] cannot be NULL.

- Kevin.
 
R

Russell Hanneken

August said:
What does the C Standard (any of them) say about this issue? Can a
conformant implementation allow argv[0] == NULL? Can a conformant
implementation allow argv[0] to cause the machine to segfault if it is
dereferenced?

argv[0] can be NULL only if argc is 0. If argc is greater than 0, argv[0]
must be a valid string, although it might be an empty string (i.e.,
argv[0][0] might be '\0').

I'm looking at the C99 standard. I don't know what C89 says.
 

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

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top