Dynamic array of pointers

This is a discussion on Dynamic array of pointers within the C Programming forums, part of the General Programming Boards category; I want to change the structure of a code from array of structs to struct of arrays. So I changed ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Dynamic array of pointers

    I want to change the structure of a code from array of structs to struct of arrays. So I changed this part (arrays are declared dynamically) :
    Code:
    float *velxl, *velyl,*childl;
    
          int inc = some_value;
          int i;
          velxl = (float *)&childl[0*inc];
          velyl = (float *)&childl[1*inc];
    To this:
    Code:
    struct body { float velxd, velyd;};
    body *nbodiesl;
    for(i=0;i<something;i++){
          nbodiesl[i].velxd = (float *)&childl[0*inc+i];
          nbodiesl[i].velyd = (float *)&childl[1*inc+i];
    }
    I know the last two lines are wrong, ( I think nbodiesl should be defined as an array of pointers to the struct instead of an array of structs ) but I don't know how to do it in C.
    I appreciate if you could come up with an alternative solution, too.

    Thanks
    Last edited by rohams; 05-12-2011 at 04:13 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rohams View Post
    I want to change the structure of a code from array of structs to struct of arrays.
    I have no idea what you mean by this.
    Quote Originally Posted by rohams View Post
    So I changed this part (arrays are declared dynamically) :
    Code:
    float *velxl, *velyl,*childl;
    
          int inc = some_value;
          int i;
          velxl = (float *)&childl[0*inc];
          velyl = (float *)&childl[1*inc];
    To this:
    Code:
    struct body { float velxd, velyd;};
    body *nbodies;
    for(i=0;i<something;i++){
          nbodiesl[i].velxd = (float *)&childl[0*inc+i];
          nbodiesl[i].velyd = (float *)&childl[1*inc+i];
    }
    I know the last two lines are wrong, ( I think nbodies should be defined as an array of pointers to the struct instead of an array of structs ) but I don't know how to do it in C.
    I appreciate if you could come up with an alternative solution, too.

    Thanks
    In actual words, not code, what is it you are trying to do? nbodies as you have it is just a pointer to a structure. That means you have to dynamically allocate a structure and make nbodies point at it, or you have to make it hold the address of a non-dynamic struct you already have some place. In your example, it doesn't point anywhere specific, so you are likely going to crash as soon as you try to access it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've gone from an array of no-structs-at-all to an array of structs. If you really want a struct of arrays, then you would need to define the arrays (or the pointers, if you use dynamically allocated memory) inside the struct definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 02-16-2011, 01:52 AM
  2. Dynamic Array of Pointers to String
    By renjith.resmi in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2009, 10:58 PM
  3. Dynamic Array of Pointers to String
    By renjith.resmi in forum C++ Programming
    Replies: 0
    Last Post: 10-19-2009, 08:48 PM
  4. dynamic array of pointers?
    By baniakjr in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2006, 09:46 AM
  5. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 03:06 PM

Tags for this Thread


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