Some additional remarks below ...

Am 20.06.2017 um 22:56 schrieb Bernd Oppolzer:
I tried the program, too, using my local Watcom C compiler.
No problems, but I had to add some global definitions to make it run.
Looks like this:

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

char *get_static_string(void) {
    static char str[81] = "This is a statically allocated C string";
    return str;
}

int main (void)
{
printf("%s", get_static_string());
}

To improve the program a little bit, I would like to add some
const directives:

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

static const char *get_static_string(void)

{
   static const char str[81] = "This is a statically allocated C string";
   return str;
}


I would like to emphasize the difference between the two meanings
of "static" in the function before:

a) the first "static" before the function head only tells that the
function name is not known outside the module (not known to
the linker); it does not tell anything about the function result type,
which is "const char *". This keyword should have been selected "local"
by the C language designers. That they re-used "static" here, is kind
of criminal in my opinion (all functions are "static" in the normal sense;
same goes for all external variables, be they local to the module or not).

b) the keyword "static" befor the variable str has the common meaning;
it tells that the variable is "static" (as opposite to "auto", which is a
C keyword, too, BTW).

int main (void)
{
printf ("%s", get_static_string());
}

using this, and the correct warning level, and a sensible C compiler,
the program does not have any problem, IMO. (Assignments to the
const static string should be flagged by the compiler; you even can
force the str variable to be in the STATIC CSECT, even if you compile using RENT,
if you use the ROCONST compiler option - there is even a #pragma NORENT,
IIRC, which is crucial for VERY large const tables, which MUST be in the STATIC CSECT,
even in the RENT case).

(the remarks in the last paragraphs are mostly specific to the mainframe
and the IBM C compilers, of course).

Kind regards

Bernd


----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to