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)
"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.
**
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
.
Yes.
char **argv
is the same thing as
char* argv[]
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 ***
.
char **argv
is same as char *argv[]
.
Here argv
is the argument vector.
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#.
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".
精彩评论