In the below example of pointer to structure
#include<stdio.h>
#include<stdlib.h>
struct siva {
int a;
};
typedef struct siva *s1;
void main()
{
s1 b=(s1)malloc(sizeof(s1*));
b->a=8;
printf("\n The vlue is s1->a is %d",b->a);
free(b);
}
In the above code the variable b is not declared, but how this code works
In this line
s1 b=(s1)malloc(sizeof(s1*));
How the memory is allocated for b and why s1* is used in sizeof().
what is the difference between s1 and s1
The following is a declaration of
s1‘s type as a pointer tostruct sivaThe following is a pointer to
s1(e.g. pointer to pointer tostruct siva)The size given in malloc is wrong, it should be
As
s1points tostruct siva. It works only because the address of the first element in the struct is the address of the struct, but this is basically a dangerous thing to do.bis declared, in the same line it is assigned to:Note that in C you are not allowed to initialize a variable from a function’s return value.