0

I'm making a program that dynamically creates a list of integers.

int ins_dlist(int data, struct dlist **p){
struct dlist *q;

if((*p)->sz == (*p)->maxsz){
    q = realloc(*p, DLISTSZ((*p)->maxsz + INCRSZ));
    if(q == NULL)
        return (-1);
    q->maxsz += INCRSZ;
    *p = q;
}

//(*p)->item[(*p)->sz++] = data; <-Gives me pointer from integer without cast
*((*p)->item + (*p)->sz++) = data;
return(0);

}

My problem is on *((*p)->item + (*p)->sz++) = data; I tried declaring it in different ways but I still can't get access to the sz variable in my struct. Heres my struct declaration, its inside a file named dlist.h:

#include <stdlib.h>
struct dlist{
     int sz;
    int maxsz;
    int *item[1];
};

#define INITSZ 5
#define INCRSZ 5
#define DLISTSZ(n) ((size_t)(sizeof(struct dlist)) + ((n-1)*sizeof(int)))

struct dlist *init_dlist(int num);
int ins_dlist(int data, struct dlist **p);
2
  • 1
    Review your declaration of struct dlist or post it here. Commented Feb 10, 2014 at 8:21
  • I declared it in a file called dlist.h, edited my post Commented Feb 10, 2014 at 8:44

1 Answer 1

1

You probably wanted to define dllist as:

struct dlist{
   int sz;
   int maxsz;
   int item[1];
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.