Thread: Accessing structure members

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Accessing structure members

    The following code is just an example of a problem I having with structures and accessing the structure members. This program contains two structures that are to be part of a larger program for a project, but I just wanted to play with them to see how I would use them. The code for the typedef's and structures was the suggested code that was supplied in my text book, and since it is not the way I would have gone about it I wanted to test it first.

    Anyway the code compiles fine - but upon running it the program fails while attempting to store the input at the specified location in memory. I get the feeling that this is a real fundamental error.. stupid brain!


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

    typedef struct node NODE;
    typedef struct list DLL;

    struct node {
    int *data;
    NODE *prior;
    NODE *next;
    };

    struct list {
    long int count;
    NODE *head;
    NODE *tail;
    };

    int main(void)
    {
    DLL *foobar;

    foobar = (DLL *) malloc(sizeof(DLL));
    if(foobar == NULL)
    {
    printf("Could not allocate memory.");
    exit(0);
    }

    printf("Enter a value: ");
    scanf("%d", &foobar->head->data);

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    I think the problem is
    scanf("%d", &foobar->head->data);

    because data is already a pointer you mustn't use the & operator!
    try:
    scanf("%d", foobar->head->data);

    it should work this way
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    oh, i'm stupid!

    data also has no memory! you should allocate some if you want to store some value in it!
    Hope you don't mind my bad english, I'm Austrian!

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    D'oh! Thanks - that was pretty obvious now that you have pointed that out.

    Cheers!

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Actually I have to query this..

    Just have to make sure I have this right in my mind..

    Now Question 1:

    DLL *foobar;

    creates a pointer to a structure of type list.. of which NODE *head is a member and therefore data is also a member.

    Therefore when I allocate memory for foobar, aren't I also allocating memory for foobar->head->data??

    Question 2:

    scanf("%d", &foobar->head->data);

    data is a pointer already, true - but my text books (and granted these are rather dodgy textbooks) have examples using the same syntax. So is this the case.. Should I not be using the address operator in this case??

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Hi,
    Question 1.
    No. When you allocate memory for foobar you get memory for a long int (count) and two pointers to NODE's. No data in that.
    To start the list make head point to memory allocated for a NODE(ie.)

    head = (NODE *)malloc(sizeof(NODE));

    Now you can use:
    scanf("%d", &foobar->head->data);

    BTW
    At this point tail should also point to the NODE pointed to by head.

    Hope this helps
    Pappy
    You learn something new everyday.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%d", &foobar->head->data);
    This is correct, foobar->head->data points to an integer variable. Unless the links point to a char * or an array you need to use the address of operator with scanf, even through multiple node links.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Cool, thanks guys - I see where I am going wrong here.

  9. #9
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Allright - for the sake of space I have not included the headers, any testing or the structs (same as before..).

    Now 1 of two things have happened here..
    1. I have not done/understood what was suggested in the previous post
    2. I have not had enough sleep..

    I understand the point that Pappy has made that - makes sense, but shouldn't this then work?

    This seems like it should be really simple..


    int main(void)
    {
    DLL *foobar;
    NODE *head;

    foobar = (DLL *) malloc(sizeof(DLL));

    head = (NODE *) malloc(sizeof(NODE));

    printf("Enter an int: ");
    scanf("%d", &foobar->head->data);

    return 0;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hope this helps...

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    typedef struct node NODE; 
    typedef struct list DLL; 
    
    struct node { 
        int   data; /*NB * removed, unless you want to malloc this as well */
        NODE *prior; 
        NODE *next; 
    }; 
    
    struct list { 
        long int count; 
        NODE *head; 
        NODE *tail; 
    }; 
    
    int main(void) 
    { 
        /* this is the anchor for the DLL */
        /* unless you're creating a dynamic number of them */
        /* you just create specific ones as needed */
        DLL foobar = { 0, NULL, NULL }; /* an empty list */
        NODE *newnode;
    
        /* create a new node */ 
        newnode = malloc( sizeof(NODE) );
        newnode->data = 0;
        newnode->prior= NULL;
        newnode->next = NULL;
    
        /* insert into the list */
        if ( foobar.head == NULL ) {
            /* first node */
            foobar.head = newnode;
            foobar.tail = newnode;
            foobar.count= 1;
        } else {
            newnode->prior    = foobar.tail;    /* point newnode back to the current tail */
            foobar.tail->next = newnode;        /* current tail points at newnode */
            foobar.tail       = newnode;        /* new node is the new tail */
            foobar.count++;
        }
    
        printf("Enter a value: "); 
        scanf("%d", &foobar.head->data); 
    
        return 0; 
    }

  11. #11
    Unregistered
    Guest

    Question

    Forgive my possibly stupid question, but where in the code is the Heap RAM that was allocated being released?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Freeing a linked list is tomorrows lesson

  13. #13
    Unregistered
    Guest

    Lightbulb

    The question I posed was stupid, I now understand that ( and should have already known this ) the end of the program releases the memory that was allocated. I guess though that it would be a good programming practice to eplicitly free the memory.

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    now understand that ( and should have already known this ) the end of the program releases the memory that was allocated.
    Does it? Are you sure? That really depends on your operating systems ability to reclaim the memory.Would you trust windows to do this?

    freeing a linked list is incredibly simple...
    Code:
    void KillList(Node* Head)
    {
    	Node* Current;
    	while(Head != NULL)
    	{
    		Current=Head;
    		Head=Head->Next;
    		free(Current);
    	}
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Unregistered
    Guest
    I was not aware that differing Operating Systems will handle how allocated memory is released. It really does matter to explicitly free the memory in your program code. Thank you for enlightening me on this subject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I point to structure members?
    By RaisinToe in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2009, 11:34 PM
  2. Regarding accessing the structure member
    By kollurisrinu in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:51 AM
  3. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM