Thread: Setting values of pointers to struct

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

    Setting values of pointers to struct

    I have a typedef struct as follows:

    Code:
    typedef struct {
       float x, y, z;
    } point3d;
    Then I create the following:

    Code:
    point3d *vertices;
    Then during run time I set the size:

    Code:
    vertices = (point3d *) malloc(numberOfVertices * sizeof(point3d));
    My stupid questions is and I can't seem to get this working is I want to do something along the lines of this (setting the value of x in index 3)

    Code:
    vertices[3]->x = 4.5;

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) don't cast malloc()
    2) show your entire code please
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You have to use

    vertices[3].x = 4.5;

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ahh of course. Bayint is right. My eyes are playing tricks on me again.
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    35
    Yeah that worked. I'm sure I tried just using a dot initially but I guess not. Another quick question, if in the struct I had a variable 'value' which was an array of 4 ints, would that be accessed using something like

    Code:
    verticies[3].value[2] = 5;

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with nested struct pointers
    By vampireiam in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 11:59 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM