ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yeah they're terminated with a \0 all "strings" in the second dimension.
The first dimension I'm not sure of I haven't ever not based it on the count I don't think.. Although 0 will return the program call .. Hrm look how execl works for a better understanding . I would assume the final index to be a null pointer in the first dimension of the vector.. But not sure.
The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
[pedantic-mode]That just says how those members of the exec() family are called. It says nothing about what is actually passed to the invoked program.[/pedantic-mode]
The C Language Standard does state in section 5.1.2.2.1, "argv[argc] shall be a null pointer".
argv is kind-of doubly defined, both by argc and the final NULL-pointer.
You can loop through the arguments in two ways:
Code:
int i = 1;
char *s;
while (1) {
s = argv[i++];
if (s == NULL) break;
/* ... */
}
Code:
int i;
char *s;
for (i=1; i<argc; i++) {
s = argv[i];
/* ... */
}
Note that a 'string' in c is always zero-terminated. You can easily have a byte-string with zero's in it, but then you also have to carry along a length-entity, and the usual c-library string-functions are no longer valid (well, not invalid but inappropriate)
Inside your programme, you are dealing with \0 terminated strings like any other string.
I mean when you type it in at the command line, it is a series of strings (quoted if there are intermediate spaces) with each argument terminated by a space.
In C, what are the strings in argv[][] in main(int argc, *argv[]). Are they null terminated strings? Are they terminated by a line terminator?
Of course argv could be any other name.
Do all the varieties of more complex answers solve your question?
Why did you ask? Are you looking to write a program which uses command line arguments and wish to ensure that you get things correct? There are a few ways. Firstly argc tells you how lengthy the argv array is, or should be. And further you can use library functions like the getopt(3) to assist you. I've done both and depending on how I want the invocation line to look, I either parse the arguments manually or choose to use getopt(), and honestly as good as getopt() is, I always have to look it up, thus wasting a bit of time. Well ... at least I've managed to save an example or two over the years so I know what works and what doesn't.
I did printf("%s", argv[1]) and it printed argv[1]. I saw then that argv[n] is a null terminated string for any n. Thanks also for the replies, which were very helpful.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.