Environment: OS X (v10.4.10) running gnu gcc 4.0.1 compiler.
Greetings...
I'm toying with functions passing strings written in pure ANCI C. I've written a simple 'C' program below to develop an understanding how C works.
Questions:
1) What will be in variable 'q'?
2) Is there another way to write: char* myFunction() {}?
Regards,
Ric.
BTW: the GNU compiler gives me a warning that 'myFunction()' returns the address of a local variable.
------------------------------------------------------------------------------------------------------
=============== =============== =============== ===========
Here's the output (via Mac OS X using gcc 4.0.1 compiler):
Greetings...
I'm toying with functions passing strings written in pure ANCI C. I've written a simple 'C' program below to develop an understanding how C works.
Questions:
1) What will be in variable 'q'?
2) Is there another way to write: char* myFunction() {}?
Regards,
Ric.
BTW: the GNU compiler gives me a warning that 'myFunction()' returns the address of a local variable.
------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h> #include <string.h> char *myFunction(); int main() { char * b, q, *r; b=myFunction(); q=*b; // <--- what will 'q' contain? printf("\nDone.\n"); return 0; } // end main() // ---------------------------------------------------- char* myFunction() { char str[15]; strcpy(str, "Hello, Ric!\n"); printf("Inside myFunction: %s",str); return (char *) str; }
Here's the output (via Mac OS X using gcc 4.0.1 compiler):
Code:
[/Users/Ric/workarea/C workspace/c_strings]./myTest Inside myFunction: Hello, Ric! Done.
Comment