Click to See Complete Forum and Search --> : c: passing string through function


bryan.6
06-11-2004, 08:57 AM
what i want to do is modify an array of strings within a function.
this code works:


char string[255];
int testing( char* t ) {
strcpy( t, "test succeeded" );
}
int main( void ) {
testing( string );
printf( "string: %s\n", string )
}


but this code doesn't:

char string[1][255];
int testing( char** t ) {
strcpy( t[0], "test succeeded" );
}
int main( void ) {
testing( string );
printf( "string: %s\n", string[0] )
}


obviously this is just a simple example, but i want to modify an array of strings in a function. Does anybody have any idea what i'm going to have to do to make this work?

goon12
06-11-2004, 09:39 AM
I *think* you would have to declare your array of strings, inside of main(), as

char **string;


-goon12

madcompnerd
06-11-2004, 10:16 AM
You can only pass scalar values through functions. To send a string you would send a pointer to the string.
char *string = malloc(however you use it cause I'm a c++ programmer)[255];
int testing( char* t ) {
strcpy( t, "test succeeded" );
}
int main( void ) {
testing( string );
printf( "string: %s\n", string )
}

bwkaz
06-11-2004, 07:23 PM
An array of char arrays is NOT the same thing as a pointer to a char pointer!

In other words, you need to look at the comp.lang.c FAQ, specifically, this entry:

http://www.eskimo.com/~scs/C-faq/q6.18.html

and then change the prototype of your "testing" function.

;)

bryan.6
06-12-2004, 02:35 AM
that helps me understand better... i figured it had to be some language specification like that.