Lost
Lost

Reputation: 13565

Printing value stored in **char in C

I am new to C and I do know concept of pointers a little bit bu when I see something like **char(Pointer to a pointer?) it confuses me. So I am looking at a function, definition of which looks like below:

void Settings_ParseCommandLine( int argc, char **argv, thread_Settings *mSettings )

Anyways, what I want at the moment is what is the value that *argv is holding. I would like to do Printf on it. How can I do that?

Upvotes: 1

Views: 4887

Answers (5)

Hot Licks
Hot Licks

Reputation: 47699

If something is declared char it means it is an (usually) 8-bit character value.

If something is declared char* it means it's a pointer to a char.

If something is declared char** it means it's a pointer to a pointer to a char.

When you apply the * operator to a pointer in an expression, you "dereference" is and cause the value that the pointer points to to be returned. If you apply * more than once presumably the thing you're applying it to is a pointer to a pointer.

In the above case the char **argv is a declaration of the argv parameter, defining it as a pointer to a pointer to char.

There are two other important things to understand. First, a sometype* pointer may (or may not) be a pointer to an array of sometype, rather than a single sometype value. (There is no way to know if this is the case other than to examine how the pointer was set.)

Second, an array of char values is the usual way that a "character string" is represented in C, and hence char* often addresses a character string.

By induction, then, char** may be a pointer to an array of pointers to character strings.

Upvotes: 3

idfah
idfah

Reputation: 1438

In C we usually think of a char * as a string right? So, think of char ** as a pointer to a string, or a pointer to a char pointer. This means that argv contains multiple strings (each of which should be null terminated). argc tells you how many strings argv contains.

To print it, try something like this:

for (i = 0; i < argc; ++i)
  printf("%s ", argv[i])

Upvotes: 2

Johan Henriksson
Johan Henriksson

Reputation: 687

char** argv can be thought of as a pointer to a pointer, but it can also be thought of as an array of arrays. Since a string is an array of characters in C, char** points to an array of strings.

If you want to access arguments, you can do it like this:

// argv[0] is the executable name
char* first_arg = argv[1]; // argv[1] is the first argument and so on...
printf("first arg: %s\n", first_arg);

Make sure to use argc to check that you have enough arguments before you use them though :)

Upvotes: 0

Jesus Ramos
Jesus Ramos

Reputation: 23268

printf("%s\n", argv[0]);

or

printf("%s\n", *argv);

Be sure to check argc for number of entries in argv.

Upvotes: 4

Carl Norum
Carl Norum

Reputation: 224864

argv is an array of strings. *argv is equivalent to argv[0], also known as the first string in argv. You can just print it exactly as you suggest:

printf("%s\n", *argv);

If you want to print them all (which is probably more useful):

for (int i = 0; i < argc; i++)
    printf("%s\n", argv[i]);

Upvotes: 6

Related Questions