Accessing members of structures

This is a discussion on Accessing members of structures within the C Programming forums, part of the General Programming Boards category; Hi, In my book, it says that a structure can be accessed in two ways. For example i have a ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Accessing members of structures

    Hi,

    In my book, it says that a structure can be accessed in two ways. For example i have a structure:
    Code:
    struct sample {
       int data;
       struct sample *nextPtr;
    };
    I can access data member with sample.data or if i have a pointer to the structure, i can go like sample->data. But if i have this line:
    Code:
    ( *sample )->data;
    what does it mean? Why brackets around it? Why can't i replace it with sample->data ? They're both pointers.

    thnx

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    typedef struct sample {
    int data;
    sample *nextPtr;
    };


    sample s;
    sample *p;

    s.data = 5;
    s.nextPtr = malloc(sizeof(sample));

    p->data = 10;
    p->nextPtr = malloc(sizeof(sample));

    (*p).data = 90;
    (*p).nextPtr = NULL;

    ...In other words, the alternatives for accessing members of pointers to structures are two:

    x->data or (*x).data

    Both are the same.

    ...But notice that sample s is NOT a pointer to a structure, so all members are accessed only with a ".", even if they(the members themselves) are pointers...

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    what i'm asking here is why do i have to use ( *sample )->nextPtr

    If i didn't explain enough pls have a look at the code from my book (attached).

    thnx
    Attached Files Attached Files

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Again, this type of dereferencing is double dereferencing. A pointer to a pointer...

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok then, thnx.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hey man. Calm down. When ppl don't get something, he needs time to get it. What's your point.

  7. #7
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    my answer to what i think your asking nutshell .
    it's all to do with the precedence of operators

    the -> operator is recognised before the * operator by your compiler, and since your line of syntax is >( *sample )->data;

    the compile reads the data stored in the memory location of sample while its in the brackets then it will read the data stored at ->data.
    if the bracket were not there then your compiler would read
    ->data first then attempt to read *sample.

    I don't exactly know what your compiler would do but there would be some kind of error
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing an array of structures
    By creative in forum C Programming
    Replies: 4
    Last Post: 12-27-2007, 11:52 AM
  2. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 01:00 PM
  3. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM
  4. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM
  5. Accessing structure members
    By foniks munkee in forum C Programming
    Replies: 18
    Last Post: 02-13-2002, 02:06 PM

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