T
temper3243
Hi,
Many people have used this code in my project. It works because b is
using the extra memory for other 4 variables.I access obj[4] max.
well here are a few questions
1) Where does it fail. I mean is there any exception where it willnot
work assuming malloc works fine.
2) someone told me in C99 we have declar b obj[] , so that it can be
declared at runtime. How do we do that. Do we have to malloc again .
Does this also have unwanted effects.
3)Assuming the below code is a hack and it works , how do i make it
work for n objects of type a with each a having k objects of type b.
4) is b *ptr instead of b obj[1] a better way. Why should one use b
obj[1] instead of b *ptr.
#include<stdio.h>
#include<stdlib.h>
typedef struct b{
int b1;
char bc;
}b ;
typedef struct a{
char a;
int a1;
b obj[1];
}a;
a *ptr;
int main()
{
ptr=malloc(sizeof(struct a ) + (sizeof(struct b)*5));
return 0;
}
Many people have used this code in my project. It works because b is
using the extra memory for other 4 variables.I access obj[4] max.
well here are a few questions
1) Where does it fail. I mean is there any exception where it willnot
work assuming malloc works fine.
2) someone told me in C99 we have declar b obj[] , so that it can be
declared at runtime. How do we do that. Do we have to malloc again .
Does this also have unwanted effects.
3)Assuming the below code is a hack and it works , how do i make it
work for n objects of type a with each a having k objects of type b.
4) is b *ptr instead of b obj[1] a better way. Why should one use b
obj[1] instead of b *ptr.
#include<stdio.h>
#include<stdlib.h>
typedef struct b{
int b1;
char bc;
}b ;
typedef struct a{
char a;
int a1;
b obj[1];
}a;
a *ptr;
int main()
{
ptr=malloc(sizeof(struct a ) + (sizeof(struct b)*5));
return 0;
}