static variable scope inside and outside the function

user1323328

Please notice the static variable selection. I am testing if the selection is assigned the correct char string within the different scope.

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

static char* selection;

static char* sel_item(char* text)
{
    char* pch;
    char buf[80];
    strcpy(buf, text);
    pch = strtok(buf, " ");
    return pch;
}

static int display_ecnt_codes(char* text)
{   
    char buf[80];
    strcpy(buf, text); 
    selection = sel_item(buf);
    // why if I output the selection here, the selection is random char, not the correct char "SRFPRO".
    // printf("display_ecnt_codes: %s\n", selection); 
}

int acode_prockey()
{
    char text[] = "SRFPRO - Surface Protection (DealerProduct)";
    display_ecnt_codes(text); 
    // why if I output the selection here, it prints the correct char string "SRFPRO".
    // what's the difference between this scope and the above scope?
    printf("acode_prockey: %s\n", selection); 
}

int main ()
{
    acode_prockey();
    // it will output SRFPRO, the first token of the char text[].
    printf("main: %s\n", selection);  
}   

I am hoping someone can explain the global static varible "selection". When I printf it inside the function "display_ecnt_codes", it outputs the random char. If I do not printf it inside the function, it output the correct char in the main function.

R Sahu

In the following function, you are returning a pointer that is not valid after the function returns.

static char* sel_item(char* text)
{
    char* pch;

    // An array on the stack
    char buf[80];
    strcpy(buf, text);

    // A pointer to some element of the array.
    pch = strtok(buf, " ");

    // Returns a pointer that is not valid after the function returns.
    return pch;
}

Later on, you use that invalid pointer which is stored in selection. Your program exhibits undefined behavior because of that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Static Function: Put Inside or Outside Scope of Class?

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

bash: variable outside function scope

From Dev

variable scope outside function in php

From Dev

Variable scope inside php function

From Dev

Javascript, outside variable scope in callback function?

From Dev

angular watch - update variable outside function scope

From Dev

Python - Static Variable inside Function

From Dev

php static variable inside function

From Dev

Calling a JavaScript variable inside a function from outside

From Dev

How to make a variable inside a function be available outside?

From Dev

Permanently changing an outside variable inside a function

From Dev

Javascript Scope: variable declared inside as opposed to outside of a forEach loop

From Dev

Set scope variable inside $window function

From Dev

vbscript variable scope inside class default function

From Dev

JS - variable scope and initilizing inside function

From Dev

Setting a global scope variable inside a function

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

Referencing a variable inside a function inside a $scope member function in angularjs

From Dev

Can I access static variables inside a function from outside

From Dev

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

From Dev

PHP Static variable inside a function not incrementing

From Dev

What is the value of the static variable declared inside a function?

From Dev

nodejs pg-promise get variable outside function scope

From Dev

Can't access variable outside the function scope in JS

From Dev

return variable name from outside of function, as string inside python function

From Dev

Will a variable declared with cdef outside a function have the same type inside the function?

Related Related

  1. 1

    C++ Static Function: Put Inside or Outside Scope of Class?

  2. 2

    AngularJS: variable $scope inside a function appears undefined outside the function

  3. 3

    AngularJS: variable $scope inside a function appears undefined outside the function

  4. 4

    bash: variable outside function scope

  5. 5

    variable scope outside function in php

  6. 6

    Variable scope inside php function

  7. 7

    Javascript, outside variable scope in callback function?

  8. 8

    angular watch - update variable outside function scope

  9. 9

    Python - Static Variable inside Function

  10. 10

    php static variable inside function

  11. 11

    Calling a JavaScript variable inside a function from outside

  12. 12

    How to make a variable inside a function be available outside?

  13. 13

    Permanently changing an outside variable inside a function

  14. 14

    Javascript Scope: variable declared inside as opposed to outside of a forEach loop

  15. 15

    Set scope variable inside $window function

  16. 16

    vbscript variable scope inside class default function

  17. 17

    JS - variable scope and initilizing inside function

  18. 18

    Setting a global scope variable inside a function

  19. 19

    array variable inside function affects the array variable outside the function in c

  20. 20

    array variable inside function affects the array variable outside the function in c

  21. 21

    Referencing a variable inside a function inside a $scope member function in angularjs

  22. 22

    Can I access static variables inside a function from outside

  23. 23

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

  24. 24

    PHP Static variable inside a function not incrementing

  25. 25

    What is the value of the static variable declared inside a function?

  26. 26

    nodejs pg-promise get variable outside function scope

  27. 27

    Can't access variable outside the function scope in JS

  28. 28

    return variable name from outside of function, as string inside python function

  29. 29

    Will a variable declared with cdef outside a function have the same type inside the function?

HotTag

Archive