C Programming - Structures, Unions, Enums
Exercise "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe."
- Albert Einstein
1.
If the following structure is written to a file using fwrite() , can fread() read it back successfully?
struct emp
{
char *n;
int age;
};
struct emp e={"IndiaBIX", 15};
FILE *fp;
fwrite(&e, sizeof(e), 1, fp);
Answer: Option C
Explanation:
Since the structure contain a char pointer while writing the structure to the disk using fwrite() only the value stored in pointer n will get written. so fread() fails to read.
2.
size of union is size of the longest element in the union
Answer: Option A
Explanation:
No answer description available for this question. Let us discuss .
3.
The elements of union are always accessed using & operator
Answer: Option D
Explanation:
No answer description available for this question. Let us discuss .
4.
Will the following code work?
#include<stdio.h>
#include<alloc.h>
struct emp
{
int len;
char name[1];
};
int main()
{
char newname[] = "Rahul";
struct emp *p = (struct emp *) malloc(sizeof(struct emp) -1 +
strlen(newname)+1);
p->len = strlen(newname);
strcpy(p -> name, newname);
printf("%d %s\n", p->len, p->name);
return 0;
}
Answer: Option A
Explanation:
The program allocates space for the structure with the size adjusted so that the name field can hold the requested name.
5.
A pointer union CANNOT be created
Answer: Option A
Explanation:
No answer description available for this question. Let us discuss .