Thread: Help needed with complex array of pointer to structures

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    Help needed with complex array of pointer to structures

    Hi,

    Imagine the following situation.

    Code:
    typedef struct{
    	unsigned short id;							
    	unsigned long val;						
    	unsigned long cap;						
    }SV;
    
    typedef SV* s_ptr;
    then at another place in the program, I make the following variables of the above type:

    Code:
    SV s[4];
    
    s_ptr s1[2];
    s_ptr s2[2];
    
    s[0].id = 1;
    s[0].val = 34;
    s[0].cap = 50;
    s1[0] = &s[0];
    
    s[1].id = 2;
    s[1].val = 23;
    s[1].cap = 100;
    s1[1] = &s[1];
    
    s[2].id = 3;
    s[2].val = 45;
    s[2].cap = 150;
    s2[0] = &s[2];
    
    s[3].id = 4;
    s[3].val = 55;
    s[3].cap = 250;
    s2[1] = &s[3];
    As you can see, I create an array of 4 structures of type SV, that is given by SV s[4]. Also, I create 4 pointers to the structure i.e. s1[0], s1[1], s2[0], s2[1].
    The structure variables are filled up and then pointers are assigned to point to these structures.

    My problem or question is the following. If I need to have an array where I can keep the two pointer arrays i.e. s1 and s2. How do we do it, what is the safe way to do it. Is the following right way to do it ?

    Code:
    s_ptr* svs[2]
    
    svs[0] = s1;
    svs[1] = s2;
    Thanks for help and comments.

    Sincerely,
    Zahid

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you mean

    Code:
    s_ptr(* svs)[2]
    
    svs[0] = &s1;
    svs[1] = &s2;
    If we consider the types for a moment, when you dereference svs, like this, you'll get a s_ptr array back, which is right.
    Last edited by whiteflags; 06-14-2011 at 07:40 AM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Quote Originally Posted by zahid990170 View Post
    Code:
    s_ptr* svs[2]
    
    svs[0] = s1;
    svs[1] = s2;
    This doesn't mean anything.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whiteflags View Post
    I think you mean

    Code:
    s_ptr(* svs)[2]
    
    svs[0] = &s1;
    svs[1] = &s2;
    If we consider the types for a moment, when you dereference svs, like this, you'll get a s_ptr array back, which is right.
    Wouldn't this give you a pointer to an array, but not an array itself? (I.e., svs[0] doesn't actually exist, etc.)

    If you want an array of 2 pointer-to-array-of-two-s_ptr, there's probably a way to do that straight off but I would use typedef:
    Code:
    typedef s_ptr two_s_ptr[2];
    two_s_ptr* svs_array[2];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing array of structures by pointer
    By edharvey in forum C Programming
    Replies: 7
    Last Post: 04-13-2011, 02:58 AM
  2. Complex Structures
    By JJ007 in forum C Programming
    Replies: 3
    Last Post: 10-27-2009, 09:47 AM
  3. Complex numbers and C structures
    By overlord21 in forum C Programming
    Replies: 20
    Last Post: 09-20-2008, 09:25 AM
  4. Complex numbers and C structures help
    By overlord21 in forum C Programming
    Replies: 1
    Last Post: 09-10-2008, 08:24 AM
  5. pointer to array of structures
    By strokebow in forum C Programming
    Replies: 14
    Last Post: 12-09-2006, 02:14 PM