0

Getting the following warning message:

database.c:15:19: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
  ptr->LastName[0] = NULL;
                   ^
database.c:16:26: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
  ptr->FirstMiddleName[0] = NULL;

                      ^

I tried using pointers a few different ways but I don't understand them very well and can't figure out how to avoid this.

#include <stdio.h>

int main() {
        struct employee {
                char LastName[30];
                char FirstMiddleName[35];
                float Salary;
                int YearHired;
        };

        struct employee employees[20];
        struct employee *ptr, person;
        ptr = &person;

        ptr->LastName[0] = NULL;
        ptr->FirstMiddleName[0] = NULL;
        ptr->Salary = -1;
        ptr->YearHired = -1;

        printf("%i", person.YearHired);
        printf("%s", person.LastName[0]);

        for(int i = 0; i < 20; i++) {
                employees[i] = person;
                //printf("%i\n", i);
        }

        printf("%c", employees[3].LastName[0]);
}

I would like to initialize an array of 20 "employees" with the initial values such that numerical values are set to -1, and the strings contain the null character as the zeroth character. Instead I get the above warning, and if I replace the NULL assignment with a letter it says "Segmentation fault (core dumped)".

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 2
    Possible duplicate of [What is the difference between NULL, '\0' and 0](https://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0) – SergeyA Dec 27 '18 at 18:47

5 Answers5

3

NULL is a null pointer constant, not the null character. Use:

ptr->LastName[0] = '\0';
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

Compiler warning message is clear enough to tell what you are doing the wrong. Here

ptr->LastName[0]  = NULL; /* NULL is equivalent of (void*)0 not \0 */

LastName[0] is character not character pointer. You might want

ptr->LastName[0]  = '\0'; /* now here \0 and Lastname[0] both are of char type */
Achal
  • 11,821
  • 2
  • 15
  • 37
1

NULL is a pointer constant, and you're trying to assign that to an element of the LastName or FirstMiddleName fields. Instead, assign 0 to the first character of each, which makes them empty strings.

    ptr->LastName[0] = 0;
    ptr->FirstMiddleName[0] = 0;

This also isn't valid:

printf("%s", person.LastName[0]);

Because person.LastName[0] is a single character, not a string. You instead want:

printf("%s", person.LastName);
dbush
  • 205,898
  • 23
  • 218
  • 273
1

To fix the warning replace NULL with the null character '\0'. NULL is a pointer, not what you want.

ptr->LastName[0] = '\0';

To fix the segmentation fault replace person.LastName[0] with person.LastName in your printf. person.LastName[0] is a single character. printf %s expects the address of a null-terminated string.

printf("%s", person.LastName);

That printf statement may still fail if you don't put a null termination somewhere in your string:

 ptr->LastName[0] = 'a';   //may still fail in printf without termination.

vs

ptr->LastName[0] = 'a';   
ptr->LastName[1] = '\0';  
serpixo
  • 310
  • 1
  • 7
0

The macro NULL is an invalid pointer not the ASCII NUL character, you should not use it as a character constant. The NUL character is \0:

    ptr->LastName[0] = `\0` ;
    ptr->FirstMiddleName[0] = `\0` ;

Or it is also valid to simply use a literal zero:

    ptr->LastName[0] = 0 ;
    ptr->FirstMiddleName[0] = 0 ;
Clifford
  • 88,407
  • 13
  • 85
  • 165