Reputation: 167
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.
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 *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..
clueless. I am using eclipse IDE. thanks in advance.
Upvotes: 0
Views: 4021
Reputation: 9618
Here is an example of creating an array of pointers to Record structures. 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.
#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;
}
Upvotes: 0
Reputation: 593
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. 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".
Your assignment of NULL to a structure type doesn't work because you can't set a structure to an integer type.
Upvotes: 1
Reputation: 179779
Your problem is that records
is a pointer to 10 objects. Thus, records[0]
is the first object. You cannot set the object to NULL
.
It only appears to work for int
because you set the int
to zero, not NULL
.
Upvotes: 1