Thread: accessing malloc'ed struct's with [], ->, and .

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    accessing malloc'ed struct's with [], ->, and .

    I wrote this code as an experiment, before I tried same thing with my project. However, can't get it to compile. See my comments inside the code:

    Code:
    #define MAX_T 42
    #define MAX_CHARS 42
    #define SOME_INT 42
    
    struct t_container {
    	struct t_named *ptrFirst;
    };
    
    struct t_named {
    	char name[MAX_CHARS];
    	int x;
    };
    
    int function(int index, struct t_container *ptrContainer);
    
    int main(){
    	int i;
    	struct t_container *new;
    
    	new = malloc( sizeof *new );
    	new->ptrFirst = malloc(  MAX_T * sizeof *new->ptrFirst  );
    	
    	for(i=0; i<MAX_T; i++) function(i, new);
    	
    	return 1;
    }
    
    int function(int index, struct t_container *ptrContainer)
    {
    	ptrContainer->ptrFirst[index].name = "Arbitrary string./n";
    	// error:  incompatible types in assignment
    
    	ptrContainer->ptrFirst[index].x = SOME_INT;
    	return 1;
    }
    Read earlier posts about pointer arithmetic, and about the difference between "->" and ".", but I'm still a bit muddled. Why does ptrContainer->ptrFirst[index].x work and ptrContainer->ptrFirst[index]->x cause an "invalid type" error?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Smile

    Hey....
    Always use strcpy(); to copy a string into another variable..
    Try it. and see the defination of strcpy();
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's because you are using array notation and not pointer arithmetic notation.

    array[i] in array notation is equivalent to *(array+i) and (array+i)->... if you got something to put instead of the dots.

    As to the fault with the name, you need to use strcpy to copy the literal to your char array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Cool, question answered

    Thanks all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  2. accessing variables using structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-25-2006, 11:09 AM
  3. accessing a struct with ->
    By Anddos in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 03:28 PM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM