Reputation: 29
I wrote this C code to set to NULL
an array of structures on C. Here it goes,
struct Worker
{
char name[50];
unsigned int salary;
};
int main()
{
int n;
struct Worker number[50];
for(n=0;n<50;n++)
{
number[n].name=NULL;
}
}
Compiler is giving this error: main.c:65:26: error: assignment to expression with array type
.
Upvotes: 0
Views: 442
Reputation: 29126
In your struct, the field name
is an array of 50 chars. Arrays are not pointers and you cannot change them; you can only change their contents.
Therefore, if you want to initialise the name
field, you could make the name an empty string by making the first char a null terminator:
*number[n].name = '\0';
Note that you name will always hold 50 chars, but you are using only the chars up to the first terminator.
Because your array isn't a pointer, the following doesn't work, either:
number[0].name = "Tom";
You must fill the contents to the array, probably with strcpy
from <string.h>
:
strcpy(number[0].name, "Tom");
If you want to test whether a string is empty, test whether the first character is the null terminator:
if (*number[n].name == '\0') ... // string is empty
You can also initialise the array explicitly:
struct Worker number[50] = {
{"Alice", 3200},
{"Bob", 2700},
{"Charlotte", 3000}
};
This will give you 3 workers with names and a salary and 47 workers with empty strings as names and zero salary. You can, of course, fill them in later or reset the first three.
If you initialise the array with:
struct Worker number[50] = {{""}};
you'll get an array of all empty-named, all zero-salary workers that you can fill.
(That said, if your name
member were a pointer like char *name;
, you could say number[n].name = "Tom";
or number[n].name = NULL;
. But that would mean that you'd have to handle all the memory that the pointer points to yourself, which is not easy.
The advantage of your array of 50 chars is that each worker has already 50 chars that you can use for their name. (Well, up to 49 plus a null terminator.) The disadvantage is that you waste memory on most names, because they are shorter and you are not flexible enough if you need a name that is longer. But before you have learned more about pointers and memory management, the array is the way to go. Just make sure that you don't exceed its limits.)
Upvotes: 2
Reputation: 325
Your name is array of 50 chars, if you want to clear it instead of:
num[n].name=NULL;
do:
memset(num[n].name, 0, 50);
You can set to NULL pointer not the array.
You can see if array is empty by examining each of arrays elem, like:
int i;
int non_empty = 0
for( i=0; i < sizeof(array)/sizeof(array[0]); ++i)
{
if(array[i] != 0)
{
non_empty = 1;
break;
}
}
Upvotes: 1
Reputation: 1520
You can't set an array of characters to null like that; there's no point. Literally there's no pointer. Arrays aren't pointers; their memory is already allocated on the stack, so I can't redirect the array how you're trying to do it. You're trying to manually set the array's address.
If however your struct looked like this
struct Worker{
char *name;
unsigned int salary;
};
Then you would be able to set each worker's name to null.
If your interest is to zero out an array of characters, you could do this
int n, j;
struct Worker number[50];
for(n=0;n<50;n++)
for(j=0;j<50;j++)
number[n].name[j]=0;
But I have no idea why you'd want to do that. I'm assuming you may use strcpy to assign values to each worker's name, which takes care of the null terminating character for you.
Upvotes: 1