Thread: Typedef pointer array access question

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    Typedef pointer array access question

    I have a data structure declared like this:

    Code:
    typedef struct {
       double x;
       double y;
    } xyPoints;
    Then I created a pointer like this:

    Code:
    xyPoints *points[30];
    I can't seem to store anything in the x and y values. I'm try to do something like this:

    Code:
    points[0].x = 0.0;
    No matter what combinations of * and & I try with points[0] I keep getting an error message when I try to compile. What is the proper syntax to gain access to the x and y values at a specific point in the array. Thanks.

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by fxtdr79 View Post
    I have a data structure declared like this:

    Code:
    typedef struct {
       double x;
       double y;
    } xyPoints;
    Then I created a pointer like this:

    Code:
    xyPoints *points[30];
    I can't seem to store anything in the x and y values. I'm try to do something like this:

    Code:
    points[0].x = 0.0;
    No matter what combinations of * and & I try with points[0] I keep getting an error message when I try to compile. What is the proper syntax to gain access to the x and y values at a specific point in the array. Thanks.
    Hi Please post your complete code. I had a similar problem a few months ago (I'm a C student also) and found that I was initializing my pointers in the wrong place.

    I don't know if that is the problem here, but I did learn one thing: it's important to post
    your entire code. :-)

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You need to use -> instead of . to access the members.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by claudiu View Post
    You need to use -> instead of . to access the members.
    Yes, but I believe there are 3 ways to access the members.

    In my book, I read "If p_str is a pointer to the structure str, the following 3 expressions are
    all equivalent:

    1) str.memb

    2) (*p_str).memb

    3) p_str -> memb

    In other words, there are 3 ways to access a structure member: using the structure name,
    or using a pointer to the structure with the indirection operator (*) or using a pointer to
    the structure with the indirect membership operator (-> )

    I wasn't clear on the problem, as I had a similar problem a few months ago. It turned
    out to be where I initialized my pointer, and Elysia set me straight on that one. :-)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Sure, there are three equivalent ways. However, regardless of which method you use, it is necessary to use one that is correct.

    Your code snippet creates an array of 30 pointers, not a pointer. Those 30 pointers are not initialised so, if you actually want to do anything useful, they need to be initialised (i.e. made to point at valid xyPoint objects)

    Once they are initialised, you need to use appropriate syntax to access members of the individual structs.

    For example;
    Code:
    xyPoints *points[30];
    for (int i = 0; i < 30; ++i) points[i] = malloc(sizeof(xyPoints);
    points[0]->x = 42;
    /*  cleanup  */
    for (int i = 0; i < 30; ++i) free(points[i]);

    More simply, create an array of 30 xyPoints
    Code:
    xyPoints points[30];    /* an array of xyPoint's, not an array of pointers */
    points[0].x = 42;
    Your basic problem seems to be that you are misunderstanding the notions of pointers and arrays. A pointer is a different thing from an array. Your code is creating an array of 30 pointers contrary to your description as begin "creating a pointer". All 30 of those pointers are uninitialised unless YOU initialise them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    35
    Thanks for the tips guys, I managed to get it working correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. question about array and pointer
    By thungmail in forum C Programming
    Replies: 5
    Last Post: 11-08-2009, 12:34 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. typedef for pointer to array of char
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2003, 04:21 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM