On Mon, 19 Jun 2017 21:25:50 +0000, Frank Swarbrick wrote:
>I know there are at least a few C developers here, so I was wondering if you
>could answer a question. Is the following valid C? (I'm not asking if one
>should actually do it; only if its valid at all.)
>char *get_static_string(void) {
> static char str[81] = "This is a statically allocated C string";
> return str;
>}
>
>
>printf("%s", get_static_string());
>
>I don't have a C compiler available at work else I'd try it myself.
>
What do you have on your desktop?
OK. Trying as published:
536 $ gmake stati
cc stati.c -o stati
stati.c:7: error: expected declaration specifiers or ‘...’ before string
constant
stati.c:7: error: expected declaration specifiers or ‘...’ before
‘get_static_string’
stati.c:7: warning: data definition has no type or storage class
stati.c:7: warning: conflicting types for built-in function ‘printf’
gmake: *** [stati] Error 1
537 $
So, repairing the problems:
537 $ diff -u stati.c stati2.c
--- stati.c 2017-06-19 17:56:07.000000000 -0600
+++ stati2.c 2017-06-19 18:01:27.000000000 -0600
@@ -3,5 +3,7 @@
return str;
}
-
+#include <stdio.h>
+int main( void ) {
printf("%s", get_static_string());
+}
538 $
And trying again:
538 $ gmake stati2 && ./stati2
cc stati2.c -o stati2
This is a statically allocated C string539 $
539 $
-- gil
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN