C: how to declare a static function that returns a nonstatic string?

hbp

If a function is declared as

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

Does it mean that this is a nonstatic function that returns a static char *, or a static function that returns a nonstatic char *?

Compare the following two functions. Which one is the correct use?

static char *fn1(void)
{
  static char s[] = "hello";
  return s;
}


static char *fn2(void)
{
  char *s = malloc(6);
  strcpy(s, "world");
  return s;
}
Jon Purdy

static applies to the function, not its return type. Both of these functions are correct—the difference is that s will be initialised once on the first call to fn1, and all calls to fn1 will share s; whereas in fn2, a new s will be allocated on every call. And since both fn1 and fn2 have static linkage, they will be private to the translation unit (source file, approximately) where they are defined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to declare static C++ function as friend on OSX

From Dev

How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

From Dev

Static or nonstatic getters/setters for static class variable in C++

From Dev

Java static/nonstatic method

From Dev

How to declare friend a static locale function

From Dev

How to declare a Static Variable inside a function in Typescript?

From Dev

How to declare the type of a function that returns an array in Fortran?

From Dev

How to declare function that returns an array of object?

From Dev

How to declare a string inside a PostgreSQL function?

From Dev

How to declare a function that returns an array in glsl es (version 100)

From Dev

'private static readonly' fields and static/nonstatic constructors

From Dev

Is it possible to declare a friend function as static?

From Dev

Declare static functions as friend function?

From Dev

How to declare extern("C") const function?

From Dev

How to declare char sequence for function (C++)?

From Dev

How to declare a function with dynamic arguments in c#

From Dev

Bloch Effective Java - favor static classes over nonstatic - how many instances?

From Dev

Java Static NonStatic issues - how to create a new instance without starting the program again

From Dev

Android implementing static or nonstatic fragment interfaces

From Dev

Static vs. nonstatic functions in Pebble apps

From Dev

java - a nonstatic field inside a static class

From Dev

Android implementing static or nonstatic fragment interfaces

From Dev

Get Data to Static From NonStatic Java

From Dev

C# Declare a Static List

From Dev

Java 8 - how do I declare a method reference to an unbound non-static method that returns void

From Dev

How to declare a static attribute in Python?

From Dev

How to declare a static variable in cuda

From Dev

How to declare static nested object

From Dev

C String Return Function Returns Garbage

Related Related

  1. 1

    How to declare static C++ function as friend on OSX

  2. 2

    How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

  3. 3

    Static or nonstatic getters/setters for static class variable in C++

  4. 4

    Java static/nonstatic method

  5. 5

    How to declare friend a static locale function

  6. 6

    How to declare a Static Variable inside a function in Typescript?

  7. 7

    How to declare the type of a function that returns an array in Fortran?

  8. 8

    How to declare function that returns an array of object?

  9. 9

    How to declare a string inside a PostgreSQL function?

  10. 10

    How to declare a function that returns an array in glsl es (version 100)

  11. 11

    'private static readonly' fields and static/nonstatic constructors

  12. 12

    Is it possible to declare a friend function as static?

  13. 13

    Declare static functions as friend function?

  14. 14

    How to declare extern("C") const function?

  15. 15

    How to declare char sequence for function (C++)?

  16. 16

    How to declare a function with dynamic arguments in c#

  17. 17

    Bloch Effective Java - favor static classes over nonstatic - how many instances?

  18. 18

    Java Static NonStatic issues - how to create a new instance without starting the program again

  19. 19

    Android implementing static or nonstatic fragment interfaces

  20. 20

    Static vs. nonstatic functions in Pebble apps

  21. 21

    java - a nonstatic field inside a static class

  22. 22

    Android implementing static or nonstatic fragment interfaces

  23. 23

    Get Data to Static From NonStatic Java

  24. 24

    C# Declare a Static List

  25. 25

    Java 8 - how do I declare a method reference to an unbound non-static method that returns void

  26. 26

    How to declare a static attribute in Python?

  27. 27

    How to declare a static variable in cuda

  28. 28

    How to declare static nested object

  29. 29

    C String Return Function Returns Garbage

HotTag

Archive