2
    typedef struct 
    {
        char*title;
        int year;
        int length; //in minutes
    } record;

    record list[1024];
    int j;
    for(j=0;j<1024;++j)
        list[j]=NULL;

I am trying to initialize an array of struct and let each element point to null initially. gcc gives me an error "incompatible types when assigning to type 'record' from type 'void*". How could I solve it? The purpose of doing this is when I access an element I am able to see if it has data or just empty.

6
  • 5
    People often use memset(list, '\0', sizeof(list));. Commented Mar 17, 2014 at 21:35
  • memset() Commented Mar 17, 2014 at 21:36
  • Your array is not an array of pointers to records, it is an array of record values. You cannot set the elements of the array to NULL because they are not pointers. This is not java... Commented Mar 17, 2014 at 21:39
  • I wanted a array of pointers not struct..Thank you. Commented Mar 17, 2014 at 21:41
  • @pat: You should post that as an answer. Commented Mar 17, 2014 at 21:41

4 Answers 4

7

You can initialize your array at declaration time this way:

record list[1024] = {{0}};
Sign up to request clarification or add additional context in comments.

4 Comments

How could I check if there is no element in this block? for example list[2] is empty, should I use if(list[2]==0) ?
You can use title member as a sentinel to mean an element has not been initialized: if (list[2].title == NULL)
@Robin: when you allocate an array of 1024 entries, then all 1024 entries are allocated, present. You can test whether there's a name using list[2].title == 0 (or list[2].title == NULL), but the space is allocated.
@ouah: I didn't downvote, but whoever did probably thought you didn't really answer the question. There's no such thing as a null structure, except perhaps by convention. A record object with title==NULL, year==0, and length==0 could be a valid record.
3

list[1024]; is array of object of your struct, which you access like

list[j].title;
list[j].year;
list[j].length; 

What you are doing is:

list[j]=NULL

list[j] is of type record, NULL is void*. I guess this is not what you have in mind.

Either initialize individual elements in the struct by accessing them individually or use memset as suggested by others.

Comments

3

If you want to do it one at a time in a for loop, it would look like this

for(j=0;j<1024;++j)
    list[j]=(record){0,0,0};

I would suggest using memset(list,0,1024*sizeof(record))

or bzero(list, 1024*sizeof(record))

If you want to do it with pointers, then you could declare your array like this:

record * list[1024];

Then you could set each one to NULL like you are and malloc each one when you're ready for it.

3 Comments

You should probably mention that (record){0,0,0} is a C99 compound literal, not least because it probably isn't supported by MSVC. Is there a good reason to use 1024 * sizeof(record) rather than just sizeof(list), which will automatically change to the correct size if the dimension of list is changed from 1024 to some other number?
@JonathanLeffler, I can't think of a good reason not to use sizeof(list), it's just not what I familiar with. I would be most comfortable defining a macro for RECORDS_IN_LIST because I always feel bad if there is a number in my code that has no name. Then I would have RECORDS_IN_LIST*sizeof(record) which I find very readable.
If performance was a consideration, 1 large allocation will take less time than 1024 small ones.
2

Your array is not an array of pointers to records, it is an array of record values. You cannot set the elements of the array to NULL because they are not pointers. This is not java...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.