Array of Pointers help

This is a discussion on Array of Pointers help within the C Programming forums, part of the General Programming Boards category; Hi all, I am currently stuck in my program. I am trying to create a struct that will have multiple ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    23

    Array of Pointers help

    Hi all,
    I am currently stuck in my program. I am trying to create a struct that will have multiple (and dynamic) children. My struct looks like this so far:
    Code:
    struct DIR{
      char *name;
      int i;
      short type;
      int max_children;
      int current_child;
      struct DIR *child;
    };
    When I want to traverse down the struct through a particular child the following command does not work folder = temp_folder->child[i]; (where folder was initialized as struct DIR*). The gcc compiler tells me that it is an invalid assignment. Does anyone know how to fix this? Should I change struct DIR* child to struct DIR** child? If so how do I use malloc for it and how do I access individual elements in the array.
    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Post some code which shows you trying to use that struct, in particular a line of code which actually causes an error message.

    In itself, there is nothing wrong with your structure.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    the structure must be malloc'ed with sizeof(struct) +number of bytes in the flexible array, so as to make room for it.. .
    http://www.redhat.com/docs/manuals/e...ro-length.html

    *EDIT* sorry, i think i was a little vague... the entire DIR structure needs to be malloced with sizeof(struct DIR)+(sizeof(struct DIR)*number of array elemnts)
    Last edited by ubermensch; 02-07-2006 at 02:34 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    23
    Hi,
    here is the portion of code that causes the error:
    Code:
    how folder is initialized elsewhere in the code:
     struct DIR* HOME = (struct DIR*)malloc(sizeof(struct DIR));
     HOME->child = (struct DIR *)malloc( (HOME->max_children) * sizeof (struct DIR) );
    
    void foo(char *d, struct DIR* folder)
    {
        struct DIR* temp_folder;
        temp_folder = folder;
        int i;
       
        for(i=0;i<temp_folder->max_children;i++)
    	{
    	    if(strcmp(temp_folder->child[i].name,d) == 0)
    		{
    		    folder = temp_folder->child[i];
    		    return;
    		}
    	}
    my_functions.c:134: error: incompatible types in assignment
    the error occurs at the line - folder = temp_folder->child[i];
    For some reason it does not recognize each individual child as its own pointer.
    Any suggestions?
    Thanks.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > For some reason it does not recognize each individual child as its own pointer.
    That's because each one isn't a pointer.

    > folder = temp_folder->child[i];
    Try
    folder = &temp_folder->child[i];

    But this won't solve your next problem that changing the parameter here will have NO effect on the pointer you used to call this function with.

    Also, see the FAQ on why casting malloc is bad in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 05:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

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