Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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/3/arrays/12.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 如何将null赋值给结构的指针?_C_Arrays_Eclipse_Pointers_Struct - Fatal编程技术网

C 如何将null赋值给结构的指针?

C 如何将null赋值给结构的指针?,c,arrays,eclipse,pointers,struct,C,Arrays,Eclipse,Pointers,Struct,我不确定我是否足够具体和清楚。 但我会澄清这个疑问。 我试图给指针分配一个空值 鉴于代码: typedef struct Date { int year; int month; }Date; typedef struct Record { char name[20]; char cdate[20]; float quanitity; int barcode; Date date; }Record; 然后主要是: Record *records = ma

我不确定我是否足够具体和清楚。 但我会澄清这个疑问。 我试图给指针分配一个空值

鉴于代码:

typedef struct Date
{
int year;
int month;
}Date;

typedef struct Record
{
    char name[20];
    char cdate[20];
    float quanitity;
    int barcode;
    Date date;
}Record;
然后主要是:

Record *records = malloc(10 * sizeof(Record));
records[0] = NULL;
当我定义一个前导类型的类似数组时,例如int,这就行不通了 我可以赋值,也可以为空 例如

int *avi = malloc(sizeof(int)*10);
avi[0] = 3;
avi [0] = NULL;
它工作得很好,我已经打印了值并看到了变化。 但是,当我对指向struct的指针数组执行相同操作时,如上所述 我就是不能分配空值

毫无头绪。 我正在使用EclipseIDE。
提前感谢。

您的问题是
记录
是指向10个对象的指针。因此,
记录[0]
是第一个对象。不能将对象设置为
NULL


它似乎只适用于
int
,因为您将
int
设置为零,而不是
NULL

NULL是一个内置常量,其值为0。这就是为什么可以将其分配给int和char等基本类型。由于C语言的特定构造告诉编译器“0”意味着“指向无效内存地址”,这可能不是实际值“0”,因此它适用于指针


将NULL赋值给结构类型无效,因为无法将结构设置为整数类型。

以下是创建指针数组以记录结构的示例。因为数组包含指向记录结构的指针,而不是直接包含结构,所以指针可以设置为NULL—如果数组直接包含记录结构,则不能这样做

#include <stdlib.h>

typedef struct Date
{
    int year;
    int month;
}Date;

typedef struct Record
{
    char name[20];
    char cdate[20];
    float quanitity;
    int barcode;
    Date date;
}Record;

int main()
{
    // We start by creating an array of 10 Record pointers.
    // records is a pointer to the array we create
    Record **records = malloc(10 * sizeof(Record*));
    // First thing is first - did it work?
    // There is no point in doing anything else if the array didn't get created
    if (records != NULL) {
        // The Record pointers are uninitialized
        // Arrays don't do that automatically
        // So we need to initialize all the pointers
        // in this case we set them all to NULL
        for (int i=0;i<10;i++)
            records[i] = NULL;
        // Later on in the program we might want to
        // create one Record structure and assign it to records[5]
        records[5] = malloc(sizeof(Record));
        // Again, we need to check if the Record structure got created
        if (records[5] != NULL) {
            // Do something with this Record structure
            // Fill it in, print it out, etc.
        }
        // Then at the end of the program
        // we want to deallocate all the Record structures we created
        for (int i=0;i<10;i++)
            if (records[i] != NULL)
                free(records[i]);
        // And finally we need to deallocate the array of pointers
        free(records);
    }
    return 0;
}
#包括
类型定义结构日期
{
国际年;
整月;
}日期;
类型定义结构记录
{
字符名[20];
半焦cdate[20];
浮动量;
国际条码;
日期;
}记录;
int main()
{
//我们首先创建一个包含10个记录指针的数组。
//records是指向我们创建的数组的指针
记录**记录=malloc(10*sizeof(记录*);
//第一件事是第一件事——它起作用了吗?
//如果没有创建数组,那么做任何其他事情都没有意义
if(记录!=NULL){
//记录指针未初始化
//数组不会自动执行此操作
//所以我们需要初始化所有指针
//在本例中,我们将它们全部设置为NULL

对于(int i=0;i您不能这样做,因为您不能将
NULL
分配给整个结构。您必须为每个元素分配
NULL
。您创建了一个记录数组,而不是指向记录的指针数组。将NULL分配给数组中的单个元素是没有意义的。
*records[0].name=0;
好的,那么我如何定义一个指针数组来正确记录?那么我能执行这个命令吗?