Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 尝试初始化“employee”结构时收到警告消息_C - Fatal编程技术网

C 尝试初始化“employee”结构时收到警告消息

C 尝试初始化“employee”结构时收到警告消息,c,C,正在获取以下警告消息: 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-c

正在获取以下警告消息:

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;

                      ^
我尝试了几种不同的方法使用指针,但我不太理解它们,也不知道如何避免这种情况

#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]);
}
我想用初始值初始化一个由20名雇员组成的数组,这样数值就被设置为-1,并且字符串包含null字符作为第0个字符。 相反,我得到了上面的警告,如果我用一个字母替换空赋值,它表示分段错误核心已转储。

NULL是一个空指针常量,而不是空字符。使用:

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

编译器警告消息足够清楚,可以告诉您做错了什么。这里

ptr->LastName[0]  = NULL; /* NULL is equivalent of (void*)0 not \0 */
LastName[0]不是字符指针,而是字符。你可能想要

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

宏NULL是无效指针,不是ASCII NUL字符,不应将其用作字符常量。NUL字符是\0:

或者简单地使用文字零也是有效的:

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

NULL是指针常量,您正试图将其分配给LastName或FirstMiddleName字段的元素。相反,将0指定给每个字符的第一个字符,这使它们成为空字符串

    ptr->LastName[0] = 0;
    ptr->FirstMiddleName[0] = 0;
这也是无效的:

printf("%s", person.LastName[0]);
因为person.LastName[0]是单个字符,而不是字符串。相反,您希望:

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

若要修复警告,请将NULL替换为空字符“\0”。NULL是一个指针,不是您想要的

ptr->LastName[0] = '\0';
若要修复分段错误,请在printf中将person.LastName[0]替换为person.LastName。person.LastName[0]是单个字符。printf%s需要以null结尾的字符串的地址

printf("%s", person.LastName);
如果不在字符串中的某个位置放置空终止符,printf语句仍可能失败:

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

可能重复的
 ptr->LastName[0] = 'a';   //may still fail in printf without termination.
ptr->LastName[0] = 'a';   
ptr->LastName[1] = '\0';