#1
  1. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11

    assign structure a global value


    I have a structure:
    Code:
    struct s{
         int a;
    };
    And a function:
    Code:
    void assign(struct s S){
         s.a = 1000;
    }
    And this is in main():
    Code:
    int main(){
         struct s S;
         printf("%d",S.a);
         return 0;
    }
    This should work, but a is empty. It shows me -370000 blabla...
    How can I assign a value to a structure which will be viewed from all functions?
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    umm is it just me being blind or are you not calling the function in main???
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Also, the assign function gets the struct passed by value, not by reference. If this is C, then it needs to accept a pointer to a struct; e.g.:
    Code:
    void assign(struct s *S)
    {
         s->a = 1000;
    }
    
    int main()
    {
         struct s S;
         assign(&S);
         printf("%d",S.a);
         return 0;
    }
  6. #4
  7. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    I did this:
    Code:
    void assign(){
          struct s S;
          S.a  = 1000;
    }
    If I do this, the value won't be "visible" from other functions...
    Do I have to assign a value in every single function?

    BTW, dwise's answer works.
    Last edited by Loser; August 24th, 2003 at 01:34 AM.
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    just pass the same variable by reference to each function. when you pass it by reference you are passing the actual variables address. when you do it normally, you pass a new copy of the variable, so any changes made arent in the copy you have.
  10. #6
  11. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    OK, I understand (weird). Thanks. :)
  12. #7
  13. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    so why do you have to point at me and call me stupid, that s not very nice ;) :D
  14. #8
  15. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    ?
  16. #9
  17. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    heh ur avatar...sorry im very bored lol.
  18. #10
  19. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    11
    lol, it points on me...:D

    Look at this: highlight this smiley and it won't be so happy anymore...
    :)
    and this
    ;)
    and now look at the alien
    :confused:
    and this one has the arrow-mouth turned down
    :eek:


    I'm bored too. :)

IMN logo majestic logo threadwatch logo seochat tools logo