Pointers and structures

This is a discussion on Pointers and structures within the C Programming forums, part of the General Programming Boards category; I have a problem when I try to allocate some memory for my structrure. Code: #define BUFSIZE 100 typedef struct ...

  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,211
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    you do not need to cast malloc in C - see FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03:44 AM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 08: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, 11:53 AM

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