Thread: Accessing members of structures

  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,708
    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...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Again, this type of dereferencing is double dereferencing. A pointer to a pointer...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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, 12:52 PM
  2. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  3. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07: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, 03:06 PM