0

I wrote this C code to set to NULLan 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.

6
  • 1
    you don't have any member named nombre. Is it a typo? Commented Nov 10, 2015 at 17:48
  • 1
    Please post the actual source code. At least use consistent names (nombre and name are not the same; num and number are not the same) and formatting (you're missing at least one } at the end of main). Commented Nov 10, 2015 at 17:49
  • Your code fails to compile for a different reason: error: ‘num’ undeclared (first use in this function). Also, int main() should be int main(void) in modern C. Commented Nov 10, 2015 at 17:50
  • 6
    Your member name/nombre is an array of 50 chars. It is not a pointer to char and so you can't set it to NULL. What you can do is make it an empty string by making the first char in it the null terminator with *num[n].name = '\0';. Commented Nov 10, 2015 at 17:50
  • @MOehm You should make your comment an answer since it clearly shows why Patty is getting the error and what he/she could use as an alternative solution :). Commented Nov 10, 2015 at 17:55

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I want to do something to see if an array is empty or already completed with a string. And if it's free to write on it.
1

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; 
    }
}

2 Comments

And if I want to see if the array is empty or written, how I see that?
updated so you can see how to examine if array is empty

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.