0

Consider the following program:

#include <stdio.h>
#include <stdlib.h>

int main()
{

typedef struct WFC_STRUCT {
int a;
int b;
} WFC_STRUCT;

WFC_STRUCT *ptr = (WFC_STRUCT*) NULL;

ptr->a=10;


return(0);

}

I would like to understand what is happening at instruction:

WFC_STRUCT *ptr = (WFC_STRUCT*) NULL;

Why the program is breaking? In what cases the instruction above is used?.

asad_hussain
  • 1,959
  • 1
  • 17
  • 27
Canatto Filipe
  • 569
  • 7
  • 15

4 Answers4

6

Although the cast in this line

WFC_STRUCT *ptr = (WFC_STRUCT*) NULL;

is useless, the actual problem is in the next line:

ptr->a=10; /* This is equivalent to: (*ptr).a=10; */

where, you dereference a NULL pointer. It's undefined behaviour. Basically, ptr should be pointing at valid/writable memory location. But in your code, it isn't.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
P.P
  • 117,907
  • 20
  • 175
  • 238
2
WFC_STRUCT *ptr = (WFC_STRUCT*) NULL; // cast is unnessesary
//moreover you have no memory alloacted

WFC_STRUCT *ptr = malloc(sizeof *ptr); // allocates memory, if this is done
//then you can do below
ptr->a=10;

Do check [ this ] answer on how to use pointers

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
1

A pointer is a variable that holds the location of, literally the address-of, a variable stored in computer memory. Dereferencing a pointer means getting the value stored in the memory at the address which the pointer “points” to.

So, to access structure individual members using a pointer to that structure, your pointer variable should point to some valid memory address at which memory is allocated to a structure variable.

But ptr pointer to structure WFC_STRUCT in your program doesn't point to a valid structure variable of type WFC_STRUCT in the valid memory address space. So, there is no point to access structure variable using ptr.

Also, WFC_STRUCT *ptr = (WFC_STRUCT*) NULL cast is unnessesary.

If you want to access a structure variable using pointer, you can do this in two ways:

WFC_STRUCT *ptr = malloc(sizeof(WFC_STRUCT));

OR

WFC_STRUCT *ptr;
WFC_STRUCT var;

ptr = &var;
ptr->a = 20;

Remenber, if you allocate some memory space using malloc function call, you need to call free function to deallocate the memory space when it is not needed anymore. However, in the second method, as variables are created over stack, all the automatic variable will be destroyed as function returns.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

WFC_STRUCT *ptr = (WFC_STRUCT*) NULL; is simply assigning address 0 (which is value of NULL) to ptr. It is common practice to assign NULL to a newly created pointer.

Now, accessing values using a pointer which points to 0th address is not allowed on most operating systems, hence the program is breaking.

If you really want to assign value to ptr->a then ptr should point to proper memory location.

WFC_STRUCT *ptr = (WFC_STRUCT*) malloc(sizeof(WFC_STRUCT));
ptr->a = 10;
free(ptr); // remember to free the allocated memory
ptr = NULL;
sameerkn
  • 2,209
  • 1
  • 12
  • 13