C: how to declare a static function that returns a non-statistical string?

If the function is declared as

static char *function(...) { ... } 

Does this mean that it is a non-static function that returns a static char * or a static function that returns a non-static char * ?

Compare the following two features. Which one is correct?

 static char *fn1(void) { static char s[] = "hello"; return s; } static char *fn2(void) { char *s = malloc(6); strcpy(s, "world"); return s; } 
+7
c function-prototypes
source share
4 answers

static applies to the function, not its return type. Both of these functions are true - the difference is that s will be initialized once upon the first call of fn1 , and all calls to fn1 will be divided by s ; whereas in fn2 a new s will be allocated for each call. And since both fn1 and fn2 have a static connection, they will be closed to the translation unit (approximately, the source file) where they are defined.

+8
source share

Both definitions of your function are correct.

Note that static is used here to limit the function in the file area, and not to indicate the type of return, i.e. this function can be used [called] by other functions present in the same file, but not by functions that are present in some other files, although they may have been compiled and linked together to form a binary file.

+3
source share

static before the function is applied to the function, and not its type (or even its part, as the return type):
This means that the function has a static binding, which is a translation unit / local file (and therefore, it is very likely that it will be built-in).

Both functions are true, although they have different semantics:

  • The first returns a pointer to a static array whose contents can be modified. Beware of concurrency and reconnection issues if you do.
  • The second allocates some heap memory, initializes it with a string and returns it. Remember free .
+3
source share

The static when used with a function definition indicates that the function has a file level scope. This means that the function name is displayed only inside the file itself. The static used in the definition of a function changes the visibility of the function name and does not change the type of the returned function.

Thus, a function declared static can return some type.

Using static for a variable defined in the function body is used to indicate that the variable should be created at the time the application loads and starts. Therefore, if you use the static modifier for a variable inside a function, this variable is not created on the stack when the function is called. It exists regardless of when the function is called. However, variable visibility is only inside the function.

In your example, you have a function that returns the address of a static variable. You can do this, however, you must understand that this use is not thread safe. In other words, all threads calling the function will receive the same variable in the same memory location, and not in their own version of the variable.

You should also understand that if you return the address of a static variable, you can also cause problems with reraction and recursion. The reason is that the variables on the stack provide reinclusion and recursion, because every time a function is called, a new frame is added to the stack, so each function call has its own stack stack, therefore its own set of variables.

This is a known issue with the old strtok() in the C standard library, which used a variable in strtok() to maintain state between calls, using NULL as the string address, where the last call to strtok() parsed. I saw a problem when a function called strtok() started parsing a string and then called another function, which in turn was called strtok() , to start parsing another string. The result was some really strange errors and behavior until the cause was clarified.

Using the static modifier for a global variable in a file will create a global variable that can be used by several functions within the file. However, like using the static modifier of the function name, the static variable will have the visibility of the file's scope.

 // .. top of a file of C source code static int aStaticInt = 0; // a static int that can be shared by all functions in the file but is visible only in the file int aNonStaticInt = 0; // a non static int that is visible outside of the file static int myStaticFunc (void) { // a function that is visible only within the file } int myNonStaticFunc (void) { // a function that is visible outside the file as well as inside the file } 
+1
source share

All Articles