V
vashwath
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
typedef union
{
int i;
char *s;
}union_t;
typedef struct
{
union_t union1;
int j;
}struct_t;
struct_t struct1;
struct1.union1.s = malloc(5);
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';
printf("%s\n",struct1.union1.s);
return 0;
}
I am getting the expected output(i.e. ASHW) for the above program.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.
The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.
The question is, Can we allocate memory for a pointer declared inside a
union?
#include <string.h>
#include <stdlib.h>
int main()
{
typedef union
{
int i;
char *s;
}union_t;
typedef struct
{
union_t union1;
int j;
}struct_t;
struct_t struct1;
struct1.union1.s = malloc(5);
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';
printf("%s\n",struct1.union1.s);
return 0;
}
I am getting the expected output(i.e. ASHW) for the above program.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.
The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.
The question is, Can we allocate memory for a pointer declared inside a
union?