Thread: Global Structure question.

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    Global Structure question.

    Hi guys, I've been trying to figure this out for hours now, but can't seem to make it work.

    I have defined a global struct outside main, and also set it to NULL. In main I assign an int to the struct and prints it fine, but then when I call another function and create a copy of the global struct, I can't seem to access the same struct. The example below is just what I'm trying to figure out first to then apply it to my own code.

    any input is highly appreciated.
    Happy 4th of July!!!

    tdep

    Code:
    struct hello{
      int test;
    }
    
    struct hello newHello = NULL;
    
    main(){
       struct hello * newHello = malloc....
       newHello->test = 1980
       printf("%d", newHello->test); //PRINTS 1980
       outside();
    }
    
    outside(){
         struct hello *SecondHello = malloc....
         SecondHello = newHello;
        printf("%d", SecondHello->test); //WONT print 1980
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean structure assignment?
    Code:
    *SecondHello = *newHello;
    If you assign one structure to another, each member will be copied over. (It's what's termed a shallow copy.)

    [edit] There were several other problems with your code. I'm assuming that this is what you're looking for:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct hello {
        int test;
    };
    
    struct hello *newHello = NULL;
    
    void outside();
    
    int main() {
        newHello = malloc(sizeof(*newHello));
        newHello->test = 1980;
        printf("&#37;d\n", newHello->test); //PRINTS 1980
        outside();
        
        getchar();
        return 0;
    }
    
    void outside() {
        struct hello *SecondHello = malloc(sizeof(*SecondHello));
        *SecondHello = *newHello;
        printf("%d\n", SecondHello->test); //WONT print 1980
    }
    [/edit]
    Last edited by dwks; 07-04-2007 at 03:37 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    It does print 1980 =/

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, I just copied your code, comments and all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Yeah I was just missing the pointer signs there.

    I just added a next pointer to hello. I have to add more ints to the list using the function outside.

    For some reason, the implementation is not working in the big picture. I'm getting a segmentation fault.

    Hopefully I'll get it working before fireworks. yehhhhhh

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    58
    It wasn't mine lol

    I need an avatar..

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    oh yeah Forgot to say thanks. You put me on the right track to do what I have to do.

    Thanks a lot!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure question
    By kolliash in forum C Programming
    Replies: 1
    Last Post: 06-05-2008, 09:55 AM
  2. Tree structure programming question help?
    By Rob4226 in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2008, 08:35 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. switch structure question
    By MyntiFresh in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2005, 05:18 PM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM