Getting warning message when trying to initialize “employee” struct












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("%in", 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)".










share|improve this question




















  • 2




    Possible duplicate of What is the difference between NULL, '' and 0
    – SergeyA
    Dec 27 '18 at 18:47
















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("%in", 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)".










share|improve this question




















  • 2




    Possible duplicate of What is the difference between NULL, '' and 0
    – SergeyA
    Dec 27 '18 at 18:47














0












0








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("%in", 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)".










share|improve this question















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("%in", 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)".







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 18:35









Andrey Akhmetov

29.5k45988




29.5k45988










asked Dec 27 '18 at 18:34









user2324350

83




83








  • 2




    Possible duplicate of What is the difference between NULL, '' and 0
    – SergeyA
    Dec 27 '18 at 18:47














  • 2




    Possible duplicate of What is the difference between NULL, '' and 0
    – SergeyA
    Dec 27 '18 at 18:47








2




2




Possible duplicate of What is the difference between NULL, '' and 0
– SergeyA
Dec 27 '18 at 18:47




Possible duplicate of What is the difference between NULL, '' and 0
– SergeyA
Dec 27 '18 at 18:47












5 Answers
5






active

oldest

votes


















3














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



ptr->LastName[0] = '';





share|improve this answer





























    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  */


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



    ptr->LastName[0]  = ''; /* now here  and Lastname[0] both are of char type */





    share|improve this answer





























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





      share|improve this answer





























        1














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



        ptr->LastName[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] = '';





        share|improve this answer





























          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 :



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


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



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





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949398%2fgetting-warning-message-when-trying-to-initialize-employee-struct%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














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



            ptr->LastName[0] = '';





            share|improve this answer


























              3














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



              ptr->LastName[0] = '';





              share|improve this answer
























                3












                3








                3






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



                ptr->LastName[0] = '';





                share|improve this answer












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



                ptr->LastName[0] = '';






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 27 '18 at 18:37









                Barmar

                419k34244344




                419k34244344

























                    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  */


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



                    ptr->LastName[0]  = ''; /* now here  and Lastname[0] both are of char type */





                    share|improve this answer


























                      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  */


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



                      ptr->LastName[0]  = ''; /* now here  and Lastname[0] both are of char type */





                      share|improve this answer
























                        2












                        2








                        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  */


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



                        ptr->LastName[0]  = ''; /* now here  and Lastname[0] both are of char type */





                        share|improve this answer












                        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  */


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



                        ptr->LastName[0]  = ''; /* now here  and Lastname[0] both are of char type */






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 27 '18 at 18:39









                        Achal

                        9,1352730




                        9,1352730























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





                            share|improve this answer


























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





                              share|improve this answer
























                                1












                                1








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





                                share|improve this answer












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






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 27 '18 at 18:39









                                dbush

                                92.9k12101133




                                92.9k12101133























                                    1














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



                                    ptr->LastName[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] = '';





                                    share|improve this answer


























                                      1














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



                                      ptr->LastName[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] = '';





                                      share|improve this answer
























                                        1












                                        1








                                        1






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



                                        ptr->LastName[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] = '';





                                        share|improve this answer












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



                                        ptr->LastName[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] = '';






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Dec 27 '18 at 19:07









                                        serpixo

                                        486




                                        486























                                            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 :



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


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



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





                                            share|improve this answer


























                                              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 :



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


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



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





                                              share|improve this answer
























                                                0












                                                0








                                                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 :



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


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



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





                                                share|improve this answer












                                                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 :



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


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



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






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Dec 27 '18 at 18:39









                                                Clifford

                                                58.3k858125




                                                58.3k858125






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949398%2fgetting-warning-message-when-trying-to-initialize-employee-struct%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Mossoró

                                                    Error while reading .h5 file using the rhdf5 package in R

                                                    Pushsharp Apns notification error: 'InvalidToken'