Thread: array of struct pointers

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    13

    array of struct pointers

    hi there,

    i am trying to create an array of struct pointers, set each element in the array to null, and (later) then assign struct values to certain positions within the array... and i am getting confused with accessing the
    elements in the array ...

    if i have an array of char pointers...

    Code:
             char *charPtr = malloc(sizeof(char*)*5);
    and go

    Code:
      	  charPtr[2] = 'b';
      	  
      	  printf(" the char pointer is :%c: \n",charPtr[2]);
    i get

    Code:
              the char pointer is :b:
    but if i go...

    Code:
    typedef struct indexType
    {
    	nodePtr head;
    	nodePtr tail;
           int count;
    } IndexType;
    
    
    typedef IndexType * IndexTypePtr;
    
    int main()
    {
           IndexTypePtr tester = NULL;
    
           tester = malloc(sizeof(IndexTypePtr)*10);
      	   
           tester[2] = NULL;
    
      return 0;
    
    }


    I get an assignment type mismatch ?

    Code:
            assignment type mismatch:
            struct indexType {pointer to struct node {..} head, pointer to struct node {..} tail,   
            int count} "=" int
    is this because i am trying to assign null to something that has elements like head, tail
    etc...

    but if that is true then how does

    Code:
             IndexTypePtr  tester = NULL;
    work ??



    thanks

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    char *charPtr = malloc(sizeof(char*)*5);
    OK, there's a problem here. You say you're making an array of char pointers (and we'll pretend that a pointer is an array for the sake of this discussion). But charPtr is, in that case, an array of char, not an array of pointers. As such, you should not be using sizeof(char *) to size the elements, but sizeof(char). Of course, sizeof(char) is 1 so it's redundant to multiply by it, but it's OK for illustration.

    What you really want for an array of char pointers is something like:
    Code:
    char **a = malloc(sizeof *a * 5);
    You can use sizeof(char *) there if you want, but I prefer not scattering type names around. Now you have an array of char pointers, and you can do this sort of thing:
    Code:
    a[0] = NULL;
    a[1] = "string";
    /* a[1][0] is 's' now */
    The same holds for your array of struct pointers. The way you have it is an array of structs, and so tester[2] is a struct, not a pointer. Again, you want a pointer to a pointer:
    Code:
    struct foo **a = malloc(sizeof *a * 5); /* Typedefing a pointer makes things unclear, in my opinion */
    a[0] = NULL; /* perfectly OK now */
    The thing with this, though, is that for each element you have to point it somewhere useful. That could be via a malloc():
    Code:
    a[0] = malloc(sizeof *a[0]);
    a[0]->head = whatever;

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    sorry to interrupt but whats a node?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It comes from the word "knot" in Latin. In this case, I'd say that it is a "knot" or "junction" in a linked list structure (or similar).

    Linked list - Wikipedia, the free encyclopedia

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    13
    thanks cas, very helpful response

    i got confused there for a while.... ;p

    notation is exactly what i was trying to do and now it works great

    very cool i can now add any sort of data structure ,bst,linked lists etc... from this
    (index / array not really sure what to call it ) ... which i will create a hash for the relative positions ... exciting ..

    and yeah node here is part of a bst or list..

    thanks again to all of the response

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM