tags:

views:

192

answers:

8

Does ** have any special meaning in C?

Like this:

static intparse_one (int argc, char **argv)
{
    cmd_line       *slot;
    int             value;
    Flag_name       flag_name;
    int         i;

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

If so, does the line

**argv

make sense? A program I am trying to get to run is choking on it. If I try to print it I get a segmentation fault.

The first printf prints the string fine. The second printf fails.

Here is what I am getting for the output (The first line is correct):

argv -aps_instance1001-aps_ato0-aps_ipc_debug3

Segementation Fault (core dumped)
A: 

Isn't that a pointer to a pointer? I think the ** in front of the argv parameter has the same effect as ref in C#, in case you know C#.

eWolf
+1  A: 

Yes.

char **argv

is the same thing as

char* argv[]

Jason
+4  A: 

"Does ** have any special meaning in C?"

No, it is just two dereferences.

char **argv

means: argv dereferenced two times is a char. In other words argv is a pointer to a pointer to char.


The same for:

"If so, does the line: **argv make sense?"

The declaration says that **argv is a char. At runtime argv will be dereferenced two times; the value is the char that argv, the pointer to a pointer to char, is pointing to.

Peter Mortensen
+1  A: 

No.

Every * introduces a level of indirection. In this context, it means that argv is a pointer to a pointer. Which it is not, really, as technically you can see it a an array of pointers - it can be declared as char * argv[].

Obviously, by "no", I mean that ** is not special in itself. Or it is special, but neither more nor less than * or ***.

RaphaelSP
A: 

char **argv is same as char *argv[].

Here argv is the argument vector.

Andriyev
A: 

While the string "**" does have a well defined meaning, I think the answer to the questions is "**" is not one operator, but two "*" in a row.

This is in contrast to Fortran where "**" is a single token and functions as the exponentiation operator.

So the meaning is well covered by Martin Beckett, but it really isn't "an operator".

dmckee
A: 

The declaration of the argv argument is often a novice programmer's first encounter with pointers to arrays of pointers and can prove intimidating. However, it is really quite simple to understand. Since argv is used to refer to an array of strings, its declaration will look like this:

char *argv[]

Remember too that when it is passed to a function, the name of an array is converted to the address of its first element. This means that we can also declare argv as char **argv; the two declarations are equivalent in this context.

Indeed, you will often see the declaration of main expressed in these terms. This declaration is exactly equivalent to that shown above:

int main(int argc, char **argv);

drorhan
+2  A: 

** is just two *.

As far as the segfault goes, look at your line printf("argv %s\n",argv);. The printf expects a char *, not char **, and so it's looking at an array of pointers rather than an array of characters. printf is trying to print everything at argv as a string of characters until it encounters a zero, and probably goes out of bounds before it finds one.

argv is a char **, which is a pointer to a pointer, or in this case an array of pointers. Don't print it directly, because it has no external meaning. (You can print the pointer value if you want to see it, of course.)

*argv or argv[0] is a char *, which is a pointer to char, which in this case is the first string in argv.

**argv is a char, which in this case is the first character of the first string in argv.

David Thornley