Global Variable Pointers

This is a discussion on Global Variable Pointers within the C Programming forums, part of the General Programming Boards category; Ahoy there mates! I'm having a problem here... I asked a couple of my friends and they say there's nothing ...

  1. #1
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24

    Global Variable Pointers

    Ahoy there mates!
    I'm having a problem here... I asked a couple of my friends and they say there's nothing wrong with this code, so I can't figure out what's the problem...

    I have declared four global pointers and i need to call them in a function(posted next). These are pointers to a double linked list.

    Code:
    typedef struct NODE {
        struct NODE *next;
        struct NODE *prev;
        char name[BUFFDIM]
    } node;
    
    node *first_list1 = NULL;
    node *last_list1 = NULL;
    
    node *first_list2 = NULL;
    node *last_list2 = NULL;
    
    void insert(node *first, node *last, char *as) {
        node *new = first;
        node *n;
        if(first==NULL) {
            first = (node *) malloc(sizeof(node));
            strcpy(first->name, as);
            first->next = NULL;
            first->prev = NULL;
            last = first;
        }
        else {
            while(n->next != NULL) {
                n = n->next;
            }
            new = (node*) malloc(sizeof(node));
            strcpy(new->name, as);
            new->prev = n;
            n->next = new;
            last = new;
        }
    }
    the thing is, if i call the function insert with one of those pointers (first_list1 or first_list2) and they're NULL, they don't change their value to point to a node...
    the "last" ones don't too.
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > void insert(node *first, node *last, char *as);
    To make changes to pointers, you need pointers to pointers
    void insert(node **first, node **last, char *as);

    Which you would call with
    insert ( &first, &last, "hello" );

    > first = (node *) malloc(sizeof(node));
    Ugh, casting (see the FAQ)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I believe Salem's talking about this:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  4. #4
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24
    hmm that's interesting... gotta blame those teachers for teaching us right!
    i called the function like you said but it gives me warnings... what more do i have to change in my code? guess i'm begining to think i'm trying to do stuff that's beyond my lead...
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > i called the function like you said but it gives me warnings... what more do i have to change in my code?
    Well posting your latest code and errors would seem like a good move to me.
    Otherwise, it's just guesswork.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. controlling modifying global variable
    By sunny_master_07 in forum C Programming
    Replies: 9
    Last Post: 04-11-2008, 04:30 AM
  2. Refering to a global variable in a different object file
    By Canadian0469 in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 07:53 PM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  4. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 03:38 AM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21