H
Horst Kraemer
i am having a pointer testptr to a structure test
struct
{
int a;
char b[10];
}test;
test *testptr;
This is illegal sytax. You may use
struct test
{
int a;
char b[10];
};
struct test *testptr;
or
typedef
struct
{
int a;
char b[10];
}test;
test *testptr;
or
typedef
struct anything
{
int a;
char b[10];
}test;
test *testptr;
I am doing malloc for the pointer to have memory for an array of 3
structures like below:
testptr=(testptr)malloc(sizeof(test)*3);
Again illegal syntax.
testptr=(test*)malloc(sizeof(test)*3);
would be legal but the cast (test*) isn't necessary in C.
Now how can i get to know that testptr is allocated memory for an
array of 3 strcture from the testptr variable?
testptr=malloc(sizeof *testptr *3);
if (testptr==0) { /* couldn't allocate memory */ }
or
if (testptr==NULL) { /* couldn't allocate memory */ }
or
if (!testptr) { /* couldn't allocate memory */ }
Which form you use is a matter of style. malloc returns a null pointer
if the requested memory size couldn't be allocated.
Regards
Horst