0

I have a global variable declared char* global=NULL and a function where I parse a separate string but copy its value into the global variable. Essentially copying the second word in the string into the global variable. And at the end of this function in the last print statement it prints out the global variable correctly, which again contains the second word of the string.

void parsestring(char* s, char** ssp){
int i;
  char st[30];
  strcpy(st,s);
  printf("String st after copy is %s",st);
  char* first=strtok(st," ");
  char* second=strtok(NULL," ");
  printf("\nstring s at the end is %s\n",s);
  global=second;
  printf("\nsecond after assigning to blobal is is %s\n",second);

  printf("\n global at end of parse function is %s",global);
}

But when I call this global variable from a different function and test print its value, it only prints part of the string, not the entirety of it like it does at the end of the above function.

I know what i'm doing isn't optimal but I don't get why the global variable changes when called later in my program.

Turpintine
  • 11
  • 2
  • 1
    `st` is a local variable. It only exists inside the function. Accessing it outside the function is Undefined Behaviour. Which is exactly what you do by keeping a global pointer to it and accessing that pointer outside the function. – kaylum Sep 21 '20 at 07:39
  • Does this answer your question? [How to access a local variable from a different function using pointers?](https://stackoverflow.com/questions/4570366/how-to-access-a-local-variable-from-a-different-function-using-pointers) – kaylum Sep 21 '20 at 07:41
  • 1
    What's the purpose of `ssp` in this function? – Bob__ Sep 21 '20 at 08:38
  • 1
    I guess all the countless teachers, book authors and C veterans telling you not to use global variables were right. – Lundin Sep 21 '20 at 10:09

2 Answers2

0

No, you cannot do that safely:

char st[30];
char * second=strtok(NULL," ");
global = second;

The first line above creates a local variable in the function (which you copy some string into).

The second line gives you an address within that string, and the third line assigns that address to your global variable.

Unfortunately for you, the st object goes out of scope at the end of the function so you are not allowed to use it by, for example, dereferencing an address that points into it.


What's probably happening (although this is irrelevant) is that the area of the stack where st is stored is being reused somehow, "damaging" the string at which the global variable points.

I say "irrelevant" since the word "stack" appears approximately zero times in the ISO C standard. The basic rule is that you need to follow what the standard says, or bad things may happen. What those bad things are is dependent totally on the implementation :-)


In terms of fixing it, you basically need to make sure the information goes somewhere that won't go out of scope before you need to use it. That could be, for example, making global a character array (much like st) rather than a pointer, and strcpying to it.

Or, having your function allocate memory that will survive function exit, such as with (see here if your implementation doesn't provide a strdup):

global = strdup(second);

Just remember to free that memory once you're done with it.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

in your function you assign global to point into a buffer on the stack.

when the function exists, st is no longer yours.

change global to be a buffer by itself.

eyalm
  • 3,366
  • 19
  • 21