why temporary char** argument is ilegal?
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I have a function f(char **p)
and I wanted to call it in the simplest way I could.
I tried char ** p = "a", "b";
f(p);
and got:
scalar object requires one element in initializer
so I changed into char * p[2] = "a", "b";
f(p);
and that went fine [would have been fine with just char * p
].
My question is, why can't I create an array of pointers on the fly like this:
f("a","b");
c
add a comment |Â
up vote
7
down vote
favorite
I have a function f(char **p)
and I wanted to call it in the simplest way I could.
I tried char ** p = "a", "b";
f(p);
and got:
scalar object requires one element in initializer
so I changed into char * p[2] = "a", "b";
f(p);
and that went fine [would have been fine with just char * p
].
My question is, why can't I create an array of pointers on the fly like this:
f("a","b");
c
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have a function f(char **p)
and I wanted to call it in the simplest way I could.
I tried char ** p = "a", "b";
f(p);
and got:
scalar object requires one element in initializer
so I changed into char * p[2] = "a", "b";
f(p);
and that went fine [would have been fine with just char * p
].
My question is, why can't I create an array of pointers on the fly like this:
f("a","b");
c
I have a function f(char **p)
and I wanted to call it in the simplest way I could.
I tried char ** p = "a", "b";
f(p);
and got:
scalar object requires one element in initializer
so I changed into char * p[2] = "a", "b";
f(p);
and that went fine [would have been fine with just char * p
].
My question is, why can't I create an array of pointers on the fly like this:
f("a","b");
c
c
edited 7 mins ago
asked 16 mins ago


CIsForCookies
6,50411441
6,50411441
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
6
down vote
This gives a warning:
char ** p = "a", "b";
Because p
is not an array.
This is also not legal:
f({"a", "b"));
Since curly braces by themselves are not allowed in an expression (but can be used as an initializer).
It is possible to create an array on the fly like that using a compound literal:
f((char *)"a", "b");
You could also use a compound literal to initialize a temporary:
char ** p = (char *)"a", "b";
Unlike the first statement, this is valid because an array of type char *[2]
will decay to a char **
and can be used to initialize a variable of this type.
See section 6.5.2.5 of the C standard for more details on compound literals.
add a comment |Â
up vote
3
down vote
This declaration char ** p = "a", "b";
triggers a warning because C does not know how to interpret the content of
braces:
- Declaration type
char**
suggests it's a single pointer-to-pointer-to-char - Content of
specifies two items.
You need to tell C that you are initializing a pointer to pointer with a pointer to the initial element of an anonymous array by specifying a type in front of the braces:
char ** p = (char*)"a", "b"
This construct is called a "compound literal". It could be used in a declaration, but it also works as an expression.
Note: if you are planning to pass an array like that to a function, you need to provide a way to determine the size of the array. One approach is to pass NULL
to "terminate" the array, like this:
void f(char **p)
int i = 0;
while (*p)
printf("%d:%sn", i++, *p++);
int main(void)
f((char*)"a", "b", NULL);
return 0;
Demo.
add a comment |Â
up vote
0
down vote
C99 and later added compound literals for the exact purpose: creating array and structs on the fly
Example:
struct foo int a; char b[2]; structure;
structure = ((struct foo) 1, 'a', 0);
int y = (int ) 1, 2, 3;
int z = (int [3]) 1;
Apart from std C99 and later, GCC provide this feature as an extension too.
In you case this will work
f((char *)"a","b");
char *)"a","b"
is a compound literal which creates an array of 2 pointers to char
.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
This gives a warning:
char ** p = "a", "b";
Because p
is not an array.
This is also not legal:
f({"a", "b"));
Since curly braces by themselves are not allowed in an expression (but can be used as an initializer).
It is possible to create an array on the fly like that using a compound literal:
f((char *)"a", "b");
You could also use a compound literal to initialize a temporary:
char ** p = (char *)"a", "b";
Unlike the first statement, this is valid because an array of type char *[2]
will decay to a char **
and can be used to initialize a variable of this type.
See section 6.5.2.5 of the C standard for more details on compound literals.
add a comment |Â
up vote
6
down vote
This gives a warning:
char ** p = "a", "b";
Because p
is not an array.
This is also not legal:
f({"a", "b"));
Since curly braces by themselves are not allowed in an expression (but can be used as an initializer).
It is possible to create an array on the fly like that using a compound literal:
f((char *)"a", "b");
You could also use a compound literal to initialize a temporary:
char ** p = (char *)"a", "b";
Unlike the first statement, this is valid because an array of type char *[2]
will decay to a char **
and can be used to initialize a variable of this type.
See section 6.5.2.5 of the C standard for more details on compound literals.
add a comment |Â
up vote
6
down vote
up vote
6
down vote
This gives a warning:
char ** p = "a", "b";
Because p
is not an array.
This is also not legal:
f({"a", "b"));
Since curly braces by themselves are not allowed in an expression (but can be used as an initializer).
It is possible to create an array on the fly like that using a compound literal:
f((char *)"a", "b");
You could also use a compound literal to initialize a temporary:
char ** p = (char *)"a", "b";
Unlike the first statement, this is valid because an array of type char *[2]
will decay to a char **
and can be used to initialize a variable of this type.
See section 6.5.2.5 of the C standard for more details on compound literals.
This gives a warning:
char ** p = "a", "b";
Because p
is not an array.
This is also not legal:
f({"a", "b"));
Since curly braces by themselves are not allowed in an expression (but can be used as an initializer).
It is possible to create an array on the fly like that using a compound literal:
f((char *)"a", "b");
You could also use a compound literal to initialize a temporary:
char ** p = (char *)"a", "b";
Unlike the first statement, this is valid because an array of type char *[2]
will decay to a char **
and can be used to initialize a variable of this type.
See section 6.5.2.5 of the C standard for more details on compound literals.
edited 3 mins ago
answered 13 mins ago
dbush
86.6k1091124
86.6k1091124
add a comment |Â
add a comment |Â
up vote
3
down vote
This declaration char ** p = "a", "b";
triggers a warning because C does not know how to interpret the content of
braces:
- Declaration type
char**
suggests it's a single pointer-to-pointer-to-char - Content of
specifies two items.
You need to tell C that you are initializing a pointer to pointer with a pointer to the initial element of an anonymous array by specifying a type in front of the braces:
char ** p = (char*)"a", "b"
This construct is called a "compound literal". It could be used in a declaration, but it also works as an expression.
Note: if you are planning to pass an array like that to a function, you need to provide a way to determine the size of the array. One approach is to pass NULL
to "terminate" the array, like this:
void f(char **p)
int i = 0;
while (*p)
printf("%d:%sn", i++, *p++);
int main(void)
f((char*)"a", "b", NULL);
return 0;
Demo.
add a comment |Â
up vote
3
down vote
This declaration char ** p = "a", "b";
triggers a warning because C does not know how to interpret the content of
braces:
- Declaration type
char**
suggests it's a single pointer-to-pointer-to-char - Content of
specifies two items.
You need to tell C that you are initializing a pointer to pointer with a pointer to the initial element of an anonymous array by specifying a type in front of the braces:
char ** p = (char*)"a", "b"
This construct is called a "compound literal". It could be used in a declaration, but it also works as an expression.
Note: if you are planning to pass an array like that to a function, you need to provide a way to determine the size of the array. One approach is to pass NULL
to "terminate" the array, like this:
void f(char **p)
int i = 0;
while (*p)
printf("%d:%sn", i++, *p++);
int main(void)
f((char*)"a", "b", NULL);
return 0;
Demo.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
This declaration char ** p = "a", "b";
triggers a warning because C does not know how to interpret the content of
braces:
- Declaration type
char**
suggests it's a single pointer-to-pointer-to-char - Content of
specifies two items.
You need to tell C that you are initializing a pointer to pointer with a pointer to the initial element of an anonymous array by specifying a type in front of the braces:
char ** p = (char*)"a", "b"
This construct is called a "compound literal". It could be used in a declaration, but it also works as an expression.
Note: if you are planning to pass an array like that to a function, you need to provide a way to determine the size of the array. One approach is to pass NULL
to "terminate" the array, like this:
void f(char **p)
int i = 0;
while (*p)
printf("%d:%sn", i++, *p++);
int main(void)
f((char*)"a", "b", NULL);
return 0;
Demo.
This declaration char ** p = "a", "b";
triggers a warning because C does not know how to interpret the content of
braces:
- Declaration type
char**
suggests it's a single pointer-to-pointer-to-char - Content of
specifies two items.
You need to tell C that you are initializing a pointer to pointer with a pointer to the initial element of an anonymous array by specifying a type in front of the braces:
char ** p = (char*)"a", "b"
This construct is called a "compound literal". It could be used in a declaration, but it also works as an expression.
Note: if you are planning to pass an array like that to a function, you need to provide a way to determine the size of the array. One approach is to pass NULL
to "terminate" the array, like this:
void f(char **p)
int i = 0;
while (*p)
printf("%d:%sn", i++, *p++);
int main(void)
f((char*)"a", "b", NULL);
return 0;
Demo.
answered 10 mins ago
dasblinkenlight
603k547591174
603k547591174
add a comment |Â
add a comment |Â
up vote
0
down vote
C99 and later added compound literals for the exact purpose: creating array and structs on the fly
Example:
struct foo int a; char b[2]; structure;
structure = ((struct foo) 1, 'a', 0);
int y = (int ) 1, 2, 3;
int z = (int [3]) 1;
Apart from std C99 and later, GCC provide this feature as an extension too.
In you case this will work
f((char *)"a","b");
char *)"a","b"
is a compound literal which creates an array of 2 pointers to char
.
add a comment |Â
up vote
0
down vote
C99 and later added compound literals for the exact purpose: creating array and structs on the fly
Example:
struct foo int a; char b[2]; structure;
structure = ((struct foo) 1, 'a', 0);
int y = (int ) 1, 2, 3;
int z = (int [3]) 1;
Apart from std C99 and later, GCC provide this feature as an extension too.
In you case this will work
f((char *)"a","b");
char *)"a","b"
is a compound literal which creates an array of 2 pointers to char
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
C99 and later added compound literals for the exact purpose: creating array and structs on the fly
Example:
struct foo int a; char b[2]; structure;
structure = ((struct foo) 1, 'a', 0);
int y = (int ) 1, 2, 3;
int z = (int [3]) 1;
Apart from std C99 and later, GCC provide this feature as an extension too.
In you case this will work
f((char *)"a","b");
char *)"a","b"
is a compound literal which creates an array of 2 pointers to char
.
C99 and later added compound literals for the exact purpose: creating array and structs on the fly
Example:
struct foo int a; char b[2]; structure;
structure = ((struct foo) 1, 'a', 0);
int y = (int ) 1, 2, 3;
int z = (int [3]) 1;
Apart from std C99 and later, GCC provide this feature as an extension too.
In you case this will work
f((char *)"a","b");
char *)"a","b"
is a compound literal which creates an array of 2 pointers to char
.
edited 11 secs ago
answered 5 mins ago


haccks
84.7k20125216
84.7k20125216
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53083520%2fwhy-temporary-char-argument-is-ilegal%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password