calling function that takes char**

F

FMorales

aurgathor said:
Let assume there is:
void f( char** str_ptr) {
}

if I want to call this from main with a predefined
string, I need to do:

char* str = "some_string";
f(&str);

but I'd like to do something like:
f(&("some_string"));
but this doesn't compile; I even tried:
f(&(char*)"some_string");
to no avail.

So, is there a way to call f() without having
to use an extra variable? If yes, how?

TIA

I believe what you're looking for is a feature (TMK) new to C99,
called, "Compound Literals". I will warn i'm not very familiar
w/ them, i've only done some basic things to get an understanding
of how they could benefit me. If you wish to research them, in my
C99 PDF their section is: 6.5.2.5 Compound Literals. With that
said, here's an example i compiled\ran ok w/ gcc as my compiler:

#include <stdio.h>
#include <stdlib.h>

void
foo( char **str )
{
printf( "%s\n", *str );
}

int
main( void )
{
foo( &(char *){"Hello World"} );
return 0;
}

Compiled w/: gcc -o foo foo.c -std=c99 . It ran just fine. I
apologize if there's any error's i missed, hopefully someone will
correct them. Hope it helps.

FMorales...
 
A

aurgathor

Let assume there is:
void f( char** str_ptr) {
}

if I want to call this from main with a predefined
string, I need to do:

char* str = "some_string";
f(&str);

but I'd like to do something like:
f(&("some_string"));
but this doesn't compile; I even tried:
f(&(char*)"some_string");
to no avail.

So, is there a way to call f() without having
to use an extra variable? If yes, how?

TIA
 
K

Kiru Sengal

aurgathor said:
Let assume there is:
void f( char** str_ptr) {
}

if I want to call this from main with a predefined
string, I need to do:

char* str = "some_string";
f(&str);

but I'd like to do something like:
f(&("some_string"));
but this doesn't compile; I even tried:
f(&(char*)"some_string");
to no avail.

So, is there a way to call f() without having
to use an extra variable? If yes, how?

TIA


This is not possible because a literal string can only decay to a
pointer value, not a pointer object (can also be used for other
reasons, but they don't come to play here). The & address of operator
requires an object as it's operand.

Let's try to disprove this another way. Say you did manage to make the
call to f(), now when you dereference the parameter holding this value
within the funciton, what could you possibly be designating?
 
A

aurgathor

Kiru Sengal said:
This is not possible because a literal string can only decay to a
pointer value, not a pointer object (can also be used for other
reasons, but they don't come to play here). The & address of operator
requires an object as it's operand.

Thanks, I was afraid this is going to be the answer, but I wasn't sure,
since one may never know if there's a loophole somewhere.
Let's try to disprove this another way. Say you did manage to make the
call to f(), now when you dereference the parameter holding this value
within the funciton, what could you possibly be designating?

The memory address where the literal string starts....?
 
W

William Ahern

aurgathor said:
Let assume there is:
void f( char** str_ptr) {
}
if I want to call this from main with a predefined
string, I need to do:
char* str = "some_string";
f(&str);
but I'd like to do something like:
f(&("some_string"));
but this doesn't compile; I even tried:
f(&(char*)"some_string");
to no avail.
So, is there a way to call f() without having
to use an extra variable? If yes, how?

C99 defines, and some compilers support, compound literals. A simple
example:

f((char *[]){"some_string"});

Compound literals effectively let you create anonymous arrays and
structures. Above we're creating an anonymous array of character pointers.

I've only ever used GCC, which has supported this for quite awhile. YMMV.
 
A

aurgathor

C99 defines, and some compilers support, compound literals.

Well, the datestamp on the compiler (BC++ 5.02) I'm using
is 1997, so I guess I can't really expect C99 features in it.
Compound literals effectively let you create anonymous arrays and
structures. Above we're creating an anonymous array of character pointers.

I've only ever used GCC, which has supported this for quite awhile. YMMV.

Thanks for all who replied -- I guess I need to look for a newer compiler.
 
L

Lawrence Kirby

Well, the datestamp on the compiler (BC++ 5.02) I'm using
is 1997, so I guess I can't really expect C99 features in it.


Thanks for all who replied -- I guess I need to look for a newer compiler.

Or, even better, just use an extra variable.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top