Looks OK to me.  "str" is an array of char's so the name by itself (without 
subscript brackets) is actually a pointer to that storage (IOW a char *).

Someone well versed in C once told me to remember:

str == &str[0]

That is, the array name is equal to the address of the first array member.

Peter

-----Original Message-----
From: IBM Mainframe Discussion List [mailto:[email protected]] On Behalf 
Of Frank Swarbrick
Sent: Monday, June 19, 2017 5:26 PM
To: [email protected]
Subject: Simple (?) C question

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.

Frank
--

This message and any attachments are intended only for the use of the addressee 
and may contain information that is privileged and confidential. If the reader 
of the message is not the intended recipient or an authorized representative of 
the intended recipient, you are hereby notified that any dissemination of this 
communication is strictly prohibited. If you have received this communication 
in error, please notify us immediately by e-mail and delete the message and any 
attachments from your system.

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to