简体   繁体   English

如何为结构体的指针分配null?

[英]How to assign null to pointer of struct?

I am not sure i'f i was specific and clear enough. 我不确定我是否足够明确和明确。 But ill clear the doubt. 但是,我不敢怀疑。 I am trying to assign a NULL value to a pointer. 我正在尝试为指针分配一个NULL值。

given the code : 给定代码:

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

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

and then in main : 然后在主要:

Record *records = malloc(10 * sizeof(Record));
records[0] = NULL;

This just doesn't work, when I define a similliar array of premitive type, for example int I can assign values, or NULL For example for 当我定义类似类型的数组时,这是行不通的,例如int我可以分配值,或者NULL例如

int *avi = malloc(sizeof(int)*10);
avi[0] = 3;
avi [0] = NULL;

It works fine, i've printed the values and seen the changes. 它工作正常,我已经打印了值并看到了更改。 However when I do the same for an array of pointers to struct, as defined above I just can not assign a NULL value.. 但是,当我对结构的指针数组执行相同的操作时,如上面所定义的,我只是不能分配NULL值。

clueless. 毫无章法。 I am using eclipse IDE. 我正在使用Eclipse IDE。 thanks in advance. 提前致谢。

Your problem is that records is a pointer to 10 objects. 您的问题是records是指向10个对象的指针。 Thus, records[0] is the first object. 因此, records[0]是第一个对象。 You cannot set the object to NULL . 您不能将对象设置为NULL

It only appears to work for int because you set the int to zero, not NULL . 它仅适用于int因为将int设置为零,而不是NULL

NULL is a built in constant which has a value of 0. That's why you can assign it to primitive types such as int and char. NULL是一个内置常量,其值为0。这就是为什么您可以将其分配给基本类型(例如int和char)的原因。 It works for pointers due to a specific construct of the C language that tells the compiler that "0" means "point to an invalid memory address", which may not be the actual value "0". 由于使用C语言的特定结构,该函数适用于指针,该结构告诉编译器“ 0”表示“指向无效的内存地址”,该地址可能不是实际值“ 0”。

Your assignment of NULL to a structure type doesn't work because you can't set a structure to an integer type. 您无法将NULL分配给结构类型,因为您无法将结构设置为整数类型。

Here is an example of creating an array of pointers to Record structures. 这是创建指向Record结构的指针数组的示例。 Because the array contains pointers to the Record structures instead of containing the structure directly the pointers can be set to NULL - something you can't do if the array contains the Record structure directly. 因为数组包含指向Record结构的指针,而不是直接包含Record结构,所以可以将指针设置为NULL-如果数组直接包含Record结构,则无法执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM