How could I initialize a array of struct to null?

23,618

Solution 1

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.

Solution 2

You can initialize your array at declaration time this way:

record list[1024] = {{0}};

Solution 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.

Solution 4

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...

Share:
23,618
An Overflowed Stack
Author by

An Overflowed Stack

/A programmer/

Updated on March 18, 2020

Comments

  • An Overflowed Stack
    An Overflowed Stack about 4 years
        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 a array of struct and let each element point to null initially and gcc gives me a 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 a element I am able to see if it has data or just empty.

    • Jonathan Leffler
      Jonathan Leffler about 10 years
      People often use memset(list, '\0', sizeof(list));.
    • motoku
      motoku about 10 years
    • pat
      pat about 10 years
      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...
    • An Overflowed Stack
      An Overflowed Stack about 10 years
      You are so right! Thank you so much.
    • An Overflowed Stack
      An Overflowed Stack about 10 years
      I wanted a array of pointers not struct..Thank you.
    • Keith Thompson
      Keith Thompson about 10 years
      @pat: You should post that as an answer.
    • Jonathan Leffler
      Jonathan Leffler about 10 years
      You declare an array of pointers as record *list[1024]; and you can still zero/nullify the pointers with memset(list, '\0', sizeof(list)) and you might then test if (list[2] == 0) (or NULL in place of 0).
  • An Overflowed Stack
    An Overflowed Stack about 10 years
    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) ?
  • ouah
    ouah about 10 years
    You can use title member as a sentinel to mean an element has not been initialized: if (list[2].title == NULL)
  • Jonathan Leffler
    Jonathan Leffler about 10 years
    @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.
  • Keith Thompson
    Keith Thompson about 10 years
    @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.
  • Jonathan Leffler
    Jonathan Leffler about 10 years
    You don't need to multiply sizeof(list) by sizeof(record) because sizeof(list) is the size in byte of the entire list. This would trample a long way out of bounds, leading to a crash.
  • Jonathan Leffler
    Jonathan Leffler about 10 years
    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?
  • Luke
    Luke about 10 years
    @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.
  • Azeroth2b
    Azeroth2b over 3 years
    If performance was a consideration, 1 large allocation will take less time than 1024 small ones.