(a)
#include<stdio.h>
#include<string.h>
int main()
{
struct employee
{
char name[25];
int age;
float bs;
};
struct employee e;
strcpy(e.name, "Hacker");
age = 25;
printf("\n%s %d", e.name, age);
return 0;
}
Error: age cannot be used without any object reference.
(b)
#include<stdio.h>
int main()
{
struct
{
char bookname[25];
float price;
};
struct book b = { "Go Embedded", 240.00 };
printf("%s %f\n", b.bookname, b.price);
return 0;
}
Error: book
is not any structure name. b
do not belong to any structure.
(c)
#include<stdio.h>
struct virus
{
char signature[25];
char status[20];
int size;
} v[2] = {
"Yankee Doodle", "Deadly", 1813,
"Dark Avenger", "Killer", 1795
};
int main()
{
int i;
for (i = 0; i <= 1; i++)
printf("\n%s %s", v.signature, v.status);
return 0;
}
Error: variable v in printf
statement, should have some index, as it is an array of structure.
(d)
#include<stdio.h>
struct s
{
char ch;
int i;
float a;
};
void f(struct s);
void g(struct s*);
int main()
{
struct s var = { 'C', 100, 12.55 };
f(var);
g(&var);
return 0;
}
void f(struct s v)
{
printf("\n%c %d %f", v->ch, v->i, v->a);
}
void g(struct s *v)
{
printf("\n%c %d %f", v.ch, v.i, v.a);
}
Error:
1. In f() function, the structure is passed by value, so "." operator should be used.
2. In g() function, the structure is passed by its address, so "->" operator should be used.
(e)
#include<stdio.h>
struct s
{
int i;
struct s *p;
};
int main()
{
struct s var1, var2;
var1.i = 100;
var2.i = 200;
var1.p = &var2;
var2.p = &var1;
printf("\n%d %d", var1.p->i, var2.p->i);
return 0;
}
Error: No error but the output as 200 100