pointer & malloc problem 
Author Message
 pointer & malloc problem

hello,

having problems assigning memory to a structure LISTDATA which is
contained in another
structure LIST, mixing up pointer assignments somehow. Any assistance
would be appreciated

header file

#define MAXLISTSIZE 10000
#define MAXLEN 80
#define DATASIZE 30
#define NUMSIZE  9

typedef struct listData
{

   char key[DATASIZE];
   char data[DATASIZE];

Quote:
}  LISTDATA;

typedef struct List
{

   LISTDATA *list[MAXLISTSIZE];
   unsigned list_size;

Quote:
}  MYLIST;

void Make_Dictionary( MYLIST * );
int  Enter_Dictionary( MYLIST *,  char *, int * );

file with prob

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "arrayList.h"

void Make_Dictionary( MYLIST * assign1 )
{
   assign1->list_size = 0;
   //printf("list size : %d",  assign1->list_size );

Quote:
}

int Enter_Dictionary( MYLIST * assign1, char * key, int * data )
{
  // printf("input key: %s data: %d\n", key, *data );
  // printf("listsize: %d\n", assign1->list_size);

/********* problem in line below *********/
   if ( assign1->(list[ (assign1->list_size) ])  = (LISTDATA *
)malloc(sizeof(LISTDATA))) == NULL )
      {
         printf("Memory allocation failed program aborted");
         return EXIT_FAILURE;
      }

assign1->list_size++;
return 0;

Quote:
}

compiling with gcc

arrayList.c: In function `Enter_Dictionary':
arrayList.c:27: parse error before `=='
arrayList.c: At top level:
arrayList.c:36: parse error before `->'

Hopefully i have provided enough info

regards

Guy



Sun, 22 Jul 2001 03:00:00 GMT  
 pointer & malloc problem

#define MAXLISTSIZE 10000
#define MAXLEN 80
#define DATASIZE 30
#define NUMSIZE  9

typedef struct listData
{

   char key[DATASIZE];
   char data[DATASIZE];

Quote:
}  LISTDATA;

typedef struct List
{

   LISTDATA* list[MAXLISTSIZE];
   unsigned list_size;

Quote:
}  MYLIST;

void Make_Dictionary( MYLIST * );
int  Enter_Dictionary( MYLIST *,  char *, int * );

file with prob

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "arrayList.h"

void Make_Dictionary( MYLIST * assign1 )
{
   assign1->list_size = 0;
   //printf("list size : %d",  assign1->list_size );

Quote:
}

int Enter_Dictionary( MYLIST * assign1, char * key, int * data )
{
  // printf("input key: %s data: %d\n", key, *data );
  // printf("listsize: %d\n", assign1->list_size);

/********* problem in line below *********/
  if (((assign1->list[(assign1->list_size)]) = malloc(sizeof(LISTDATA))) ==
NULL)
      {
         printf("Memory allocation failed program aborted");
         return EXIT_FAILURE;
      }

(assign1->list_size)++;
return 0;

Quote:
}

compiling with gcc

arrayList.c: In function `Enter_Dictionary':
arrayList.c:27: parse error before `=='
arrayList.c: At top level:
arrayList.c:36: parse error before `->'

Hopefully i have provided enough info

regards

Guy



Sun, 22 Jul 2001 03:00:00 GMT  
 pointer & malloc problem

Quote:

> /********* problem in line below *********/
>   if (((assign1->list[(assign1->list_size)]) = malloc(sizeof(LISTDATA))) ==
> NULL)
>       {
>          printf("Memory allocation failed program aborted");
>          return EXIT_FAILURE;
>       }

> (assign1->list_size)++;
> return 0;

> }

> compiling with gcc

> arrayList.c: In function `Enter_Dictionary':
> arrayList.c:27: parse error before `=='
> arrayList.c: At top level:
> arrayList.c:36: parse error before `->'

> Hopefully i have provided enough info

Way too much, actually.  Try it this way:

if ( NULL == ( assign1->pointer_yada = malloc(size_yada) ) )

The compiler is confused about your parentheses.



Sun, 22 Jul 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Struct & malloc problem : pointer expected

2. SIMPLE malloc & pointer question

3. (void *) & casting the pointer returned by malloc

4. pointers and malloc problems

5. Q: problems allocating pointers with malloc

6. problem with malloc and pointer matrixes

7. Some help with malloc pointer problems

8. HELP--problem with fread&malloc!!

9. malloc pointer problem !!

10. malloc/pointer problem

11. problems with functions & pointers in C

12. problem with pointer/array & string

 

 
Powered by phpBB® Forum Software