How could I initialize a array of struct to null?
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 record
s, 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
-
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 about 10 yearsPeople often use
memset(list, '\0', sizeof(list));
. -
motoku about 10 years
-
pat about 10 yearsYour array is not an array of pointers to
record
s, it is an array ofrecord
values. You cannot set the elements of the array toNULL
because they are not pointers. This is not java... -
An Overflowed Stack about 10 yearsYou are so right! Thank you so much.
-
An Overflowed Stack about 10 yearsI wanted a array of pointers not struct..Thank you.
-
Keith Thompson about 10 years@pat: You should post that as an answer.
-
Jonathan Leffler about 10 yearsYou declare an array of pointers as
record *list[1024];
and you can still zero/nullify the pointers withmemset(list, '\0', sizeof(list))
and you might then testif (list[2] == 0)
(orNULL
in place of 0).
-
-
An Overflowed Stack about 10 yearsHow 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 about 10 yearsYou can use
title
member as a sentinel to mean an element has not been initialized:if (list[2].title == NULL)
-
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
(orlist[2].title == NULL
), but the space is allocated. -
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 withtitle==NULL
,year==0
, andlength==0
could be a valid record. -
Jonathan Leffler about 10 yearsYou don't need to multiply
sizeof(list)
bysizeof(record)
becausesizeof(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 about 10 yearsYou 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 use1024 * sizeof(record)
rather than justsizeof(list)
, which will automatically change to the correct size if the dimension oflist
is changed from 1024 to some other number? -
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 forRECORDS_IN_LIST
because I always feel bad if there is a number in my code that has no name. Then I would haveRECORDS_IN_LIST*sizeof(record)
which I find very readable. -
Azeroth2b over 3 yearsIf performance was a consideration, 1 large allocation will take less time than 1024 small ones.