Thread: a globals changes value via function argument

  1. #1
    Registered User
    Join Date
    Mar 2008

    a globals changes value via function argument

    I'm meant to have decent c programming experience, but I've hit a snag with globals.

    I'll give a code example soon, but explain first.

    If I declare a certain variable name globally, and later feed this var name by reference to a function, which then changes the value, how come other functions fail to see this value change in the globally declared variable name. example:
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      5 int i, sz;
      7 void allarr(int *arr, int *sz)
      8 {
      9     printf("from allarr, sz is %i\n",*sz);
     10     *sz=9;
     11     for(i=0;i<*sz;i++)
     12         arr[i]=i;
     13 }
     15 int addthem(int *arr)
     16 {
     17     int sum=0;
     18     printf("from addthem, sz is %i\n",sz);
     19     for(i=0;i<sz;i++)
     20         sum+=arr[i];
     21     return sum;
     22 }
     24 int main(int argc, char *argv[])
     25 {
     27     int *ar, sz=12;
     28     ar=malloc(sz*(sizeof(int)));
     29     allarr(ar,&sz);
     30     /* print stuff out */
     31     for(i=0;i<sz;i++)
     32         printf("%i ", ar[i]);
     33     printf("\n");
     34     printf("addthem: %i\n",addthem(ar));
     36     free(ar);
     37     return 0;
     38 }
    So here I declare "sz" as a global int. In my main section I allocate the value 12 to it. Then I use function allarr() to change this value. My main section does in fact register these changes in value. My problem is that the addthem() function doesn't. It doesn't see any change in value whatsoever. In my tests, it outputs sz as zero, not 12, not 9.

    Where I confuse myself also (not hard to do, as you can see) is that by passing "sz" to function allarr() by reference, I essentially change the nature of the variale name for the function allarr(), because presumably, as a global, it was aware of "sz" being an int, but now it receives this name as an int*. This local definition of "sz" seems to supplant the global definition, for the allarr() function at least.

    Meanwhile, back in main(), no such re-definition has taken place, and it registers the new values of "sz" without problems.

    So I'm not sure what's going on. Have I changed a variable's global scope into a local scope? That's a hunch, any comments welcome. Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    You aren't changing the global. You are changing the local variable of the same name. You have declared a local variable 'sz' in main which you are using.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2008
    thanks for quick reply Quzah.

    Darn, I see that now. This was a hasty example, so I am indeed declaring a local variable with the same name as a global.

    by changing "int sz=12;" in main to just "sz=12;" then I do change the name of the global itself.

    I think this error is due to my being trigger-happy with the combined delcarations and assignations of the type "float <varname>=0.0", etc.

    I thought I had hit upon a fundamental gap in my understanding, but it's just another case of my lack of care sometimes.

    Thank you!

    PS. I just wan tto add that when I looked up globals in K&R, it was describing the use of "extern" and how it may be left out in a function if the said variable is global, and it is in the same file as where it's gloablly declared. This could lead you to simply deleting the qualifier "extern" i.e. "extern int sz" is changed to "int sz" whereby you define a local variable with the same name as the global. This is definitely going into my "gotchas" note file :-)
    Last edited by stabu; 01-11-2010 at 04:06 AM. Reason: afterthought

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM