Thread: Pointers and structures

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

    Pointers and structures

    I have a problem when I try to allocate some memory for my structrure.

    Code:
    #define BUFSIZE		100
    
    
    typedef struct hash_t{
    		char 			*buffer[50];
    		unsigned int		*t_index;
    		}HASH_T;
    HASH_T	*phtable[BUFSIZE];
    
    if ((phtable = ((struct hash_t *) malloc(sizeof (struct hash_t)*100))) == NULL)
    			printf("Allocation error");
    At compilation I get:"Operands of = have incompatible types 'struct hash_t * [100]' and 'struct hash_t *'." " Lvalue required".

    Thank you

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're taking an array and trying to assign it something. If you want to dynamically allocate a block of memory and treat it like an array, then you use a pointer.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Smile

    Hi,

    You have declared a two dimensional array of structures:

    HASH_T *phtable[BUFSIZE];

    i,e; pointer phtable points to the first element of BUFSIZE no. of structures of type hash_t...

    so you ned to allocate memory for each element..

    since phtable is a pointer to the two dimensional array...it needs the array index([]),
    thats why the compiler is throwing the error : " Lvalue required".

    try this out
    Code:
            for( count = 0 ; count < BUFSIZE ; ++count) {
                if ((phtable[count] = ((struct hash_t *) malloc(sizeof (struct hash_t)*1))) == NULL)
    			printf("Allocation error");
            }
    This might resolve the problem...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need to cast malloc in C - see FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM