Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
98 views83 pages

Structures

The document contains multiple C code snippets demonstrating the use of structures, including defining structures, initializing them, and accessing their members. It covers various aspects such as bit fields, typedefs, nested structures, and memory allocation. The examples illustrate different scenarios of structure manipulation and their output in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views83 pages

Structures

The document contains multiple C code snippets demonstrating the use of structures, including defining structures, initializing them, and accessing their members. It covers various aspects such as bit fields, typedefs, nested structures, and memory allocation. The examples illustrate different scenarios of structure manipulation and their output in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Structures

___________________________________________________________________________________

1) #include <stdio.h>
struct sample {
int a = 0;
char b = 'A';
float c = 10.5;
};
int main()
{
struct sample s;
printf("%d, %c, %f", s.a, s.b, s.c);
return 0;
}

2) #include <stdio.h>
int main()
{
struct bitfield {
signed int a : 3;
unsigned int b : 13;
unsigned int c : 1;
};
struct bitfield bit1 = { 2, 14, 1 };
printf("%d", sizeof(bit1));
return 0;
}

3) #include <stdio.h>
int main()
{
typedef struct tag {
char str[10];
int a;
} har;

har h1, h2 = { "IHelp", 10 };


h1 = h2;
h1.str[1] = 'h';
printf("%s, %d", h1.str, h1.a);
return 0;
}
4) #include <stdio.h>
struct sample {
int a;
} sample;

int main()
{
sample.a = 100;
printf("%d", sample.a);
return 0;
}

5) #include <stdio.h>
int main()
{
struct tag{
int a;
float b;
};

struct tag t={10,10.23f};


printf("%d, %.02f\n",t.a,t.b);
return 0;
}

6) #include <stdio.h>
int main()
{
struct person{
char *name;
int age;
};

struct person p={"Mani",21};

printf("%s, %d\n",p.name,p.age);

return 0;
}
7) #include <stdio.h>
int main()
{
struct person{
char name[30];
int age;
};

struct person p={"ashok",21};

//edit values
p.name="ankita";
p.age=27;
printf("%s, %d\n",p.name,p.age);
return 0;
}

#include‹stdio.h›
8) int main()
{
struct site
{
char name[] = "india";
int no_of_pages = 100;
};
struct site *ptr;
printf("%d ", ptr->no_of_pages);
printf("%s", ptr->name);
getchar();
return 0;
}

9) #include<stdio.h>
struct st
{
int x;
struct st next;
};

int main()
{
struct st temp;
temp.x = 10;
temp.next = temp;
printf("%d", temp.next.x);
return 0;
}
10) #include<stdio.h>
struct Point
{
int x, y, z;
};

int main()
{
struct Point p1 = {.y = 0, .z = 1, .x = 2};
printf("%d %d %d", p1.x, p1.y, p1.z);
return 0;
}

11) #include <stdio.h>

int main()
{
struct {int a[2];} arr[] = {{1},{2}};

printf("%d %d %d %d",arr[0].a[0],arr[0].a[1],arr[1].a[0],arr[1].a[1]);

return 0;
}

12) int main()


{
struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

printf("%d %d %d\n",arr[0].a[0],arr[0].a[1],arr[0].b);
printf("%d %d %d\n",arr[1].a[0],arr[1].a[1],arr[1].b);

return 0;
}

13) int main()


{
struct {int i; char c;} myVar = {.c ='A',.i = 100};
printf("%d %c",myVar.i, myVar.c);
return 0;
}
14) int main()
{
structure hotel
{
int items;
char name[10];
}a;
strcpy(a.name, "TAJ");
a.items=10;
printf("%s", a.name);
return 0;
}

15) int main()


{
struct book
{
int pages;
char name[10];
}a;
a.pages=10;
strcpy(a.name,"Cbasics");
printf("%s=%d", a.name,a.pages);
return 0;
}

16) int main()


{
struct ship
{
int size;
char color[10];
}
boat1, boat2;
boat1.size=10;
boat2 = boat1;
printf("boat2=%d",boat2.size);
return 0;
}
17) int main()
{
struct ship
{
char color[10];
}
boat1, boat2;
strcpy(boat1.color,"RED");
printf("%s ",boat1.color);
boat2 = boat1;
strcpy(boat2.color,"YELLOW");
printf("%s",boat1.color);
return 0;
}

18) int main()


{
struct tree
{
int h;
}
struct tree tree1;
tree1.h=10;
printf("Height=%d",tree1.h);
return 0;
}

19) int main()


{
struct tree
{
int h; int w;
}; struct tree tree1={10};
printf("%d ",tree1.w);
printf("%d",tree1.h);
return 0;
}

20) int main()


{
struct tree
{
int h;
int rate;
}; struct tree tree1={0};
printf("%d ",tree1.rate);
printf("%d",tree1.h);
return 0;
}
21) int main()
{
struct laptop
{
int cost;
char brand[10];
};
struct laptop L1={5000,"ACER"};
struct laptop L2={6000,"IBM"};
printf("Name=%s",L1.brand); return 0; }

22) int main()


{
struct forest { int trees; int animals; }F1,*F2;
F1.trees=1000;
F1.animals=20;
F2=&F1;
printf("%d ",F2.animals);
return 0;
}

23) int main()


{
struct bus
{
int seats;
}F1, *F2;
F1.seats=20;
F2=&F1;
F2->seats=15;
printf("%d ",F1.seats);
return 0;
}

24) int main()


{
struct pens
{
int color;
}p1[2];
struct pens p2[3];
p1[0].color=5;
p1[1].color=9;
printf("%d ",p1[0].color);
printf("%d",p1[1].color); return 0;
}
25) int main()
{
struct car
{
int km;
}*p1[2];
struct car c1={1234};
p1[0]=&c1;
printf("%d ",p1[0]->km);
return 0;
}

26) int main()


{
struct car
{
int color;
};
struct garage
{
struct car mycar[10];
}gar;
struct car c1={5};
gar.mycar[0]=c1;
printf("%d",gar.mycar[0]); return 0;
}

27) int main()


{
struct books
{
int pages;
char str[4];
}b;
printf("%d",sizeof(b));
return 0;
}

28) int main()


{
struct books
{
int pages;
char str[4];
}*ptr;
printf("%d",sizeof(ptr));
return 0;
}
29) void show(int,int);
int main()
{
struct paint
{
int type;
int color;
}p;
p.type=1;
p.color=5;
show(p.type,p.color);
return 0;
}
void show(int a,int b)
{
printf("%d %d",a,b);
}

30) int main()


{
struct paint
{
int type;
int color;
}p1, p2;
p1.type=1;
p1.color=5;
if(sizeof(p1)==sizeof(p2))
{
printf("SAME");
}
else
{
printf("DIFFERENT");
}
return 0;
}

31) #include <stdio.h>

struct student

char *name;

};
struct student s;

struct student fun(void)

s.name = "newton";

printf("%s\n", s.name);

s.name = "alan";

return s;

void main()

struct student m = fun();

printf("%s\n", m.name);

m.name = "turing";

printf("%s\n", s.name);

32) struct student

char *name;

};
void main()

struct student s, m;

s.name = "st";

m = s;

printf("%s%s", s.name, m.name);


}
33) include <stdio.h>

struct temp

int a;

} s;

void func(struct temp s)

s.a = 10;

printf("%d\t", s.a);

main()

func(s);

printf("%d\t", s.a);

}
34) #include <stdio.h>

struct student

char *name;

};

struct student fun(void)

struct student s;

s.name = "alan";

return s;

void main()

struct student m = fun();

s.name = "turing";

printf("%s", m.name);

}
35) #include <stdio.h>

void main()

struct student

int no;

char name[20];

};

struct student s;

no = 8;

printf("%d", no);

36) How many bytes in memory taken by the following C structure?

#include <stdio.h>

struct test

int k;

char c;

};
37) #include <stdio.h>

struct

int k;

char c;

};

int main()

struct p;

p.k = 10;

printf("%d\n", p.k);

38) struct

int k;

char c;

} p;

int p = 10;

int main()

p.k = 10;

printf("%d %d\n", p.k, p);

}
39) #include <stdio.h>

struct p

int k;

char c;

};

int p = 10;

int main()

struct p x;

x.k = 10;

printf("%d %d\n", x.k, p);

40) #include <stdio.h>

struct p

int k;

char c;

float f;

};

int p = 10;

int main()

struct p x = {1, 97};

printf("%f %d\n", x.f, p); }


41) struct p

int k;

char c;

float f;

};

int main()

struct p x = {.c = 97, .f = 3, .k = 1};

printf("%f\n", x.f);

42) #include <stdio.h>

struct p

int k;

char c;

float f;

};

int main()

struct p x = {.c = 97, .k = 1, 3};

printf("%f \n", x.f);

}
43) #include <stdio.h>

struct p

int k;

char c;

float f;

};

int main()

struct p x = {.c = 97};

printf("%f\n", x.f);

}
44) #include <stdio.h>

struct point

int x;

int y;

};

int main()

struct point p = {1};

struct point p1 = {1};

if(p == p1)

printf("equal\n");

else

printf("not equal\n");

}
45)#include<stdio.h>

struct point

int x;

int y;

};

struct notpoint

int x;

int y;

};

struct point foo();

int main()

struct point p = {1};

struct notpoint p1 = {2, 3};

p1 = foo();

printf("%d\n", p1.x);

struct point foo()

struct point temp = {1, 2};

return temp;

}
46) #include<stdio.h>

struct point

int x;

int y;

};

struct notpoint

int x;

int y;

};

int main()

struct point p = {1};

struct notpoint p1 = p;

printf("%d\n", p1.x);

}
47) #include <stdio.h>

struct point

int x;

int y;

};

struct notpoint

int x;

int y;

};

void foo(struct point);

int main()

struct notpoint p1 = {1, 2};

foo(p1);

void foo(struct point p)

printf("%d\n", p.x);

48) #include<stdio.h>
struct point

int x;
int y;

};

void foo(struct point*);

int main()

struct point p1 = {1, 2};

foo(&p1);

void foo(struct point *p)

printf("%d\n", *p.x++);

}
49) #include<stdio.h>
struct point

int x;

int y;

};

void foo(struct point*);

int main()

struct point p1 = {1, 2};

foo(&p1);

void foo(struct point *p)

printf("%d\n", *p->x++);

}
50) #include <stdio.h>

struct student fun(void)

struct student

char *name;

};

struct student s;

s.name = "alan";

return s;

void main()

struct student m = fun();

printf("%s", m.name);

}
51) #include<stdio.h>

struct student

char *name;

};

struct student fun(void)

struct student s;

s.name = "alan";

return s;

void main()

struct student m = fun();

printf("%s", m.name);

52) #include <stdio.h>


struct student
{
int no;
char name[20];
}
void main()
{
struct student s;
s.no = 8;
printf("hello");
}
53) #include <stdio.h>
struct student
{
int no = 5;
char name[20];
};
void main()
{
struct student s;
s.no = 8;
printf("hello");
}

54) #include <stdio.h>


struct student
{
int no;
char name[20];
};
void main()
{
student s;
s.no = 8;
printf("hello");
}

55) #include <stdio.h>


void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
}
56) #include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = '9', .f = .9, .k = 0};
printf("%d\n", x.c);
}

57) #include <stdio.h>


void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
no = 8;
printf("%d", no);
}

58) #include <stdio.h>


struct
{
int k;
char c;
};
int main()
{
struct p;
p.k = 100;
printf("%d\n", p.k);
}
59) #include <stdio.h>
struct
{
int k;
char c;
} p;
int p = 10;
int main()
{
p.k = 10;
printf("%d %d\n", p.k, p);
}

60) #include<stdio.h>
struct
{
int k;
char c;
} p;
int p = 10;
int main()
{
p.k = 10;
printf("%d %d\n", p.k, p);
}

61) #include<stdio.h>
struct p
{
int k;
char c;
float f;
};
int p = 12;
int main()
{
struct p x = {1, 97};
printf("%f %d\n", x.f, p);
}
62) #include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97, .f = 3, .k = 1};
printf("%f\n", x.f);
}

63) #include <stdio.h>


struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97, .k = 1, 3};
printf("%f \n", x.f);
}

64) #include <stdio.h>


struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = {.c = 97};
printf("%f\n", x.f);
}
65) #include <stdio.h>
struct student
{
char *name;
};
void main()
{
struct student s, m;
s.name = "At";
m = s;
printf("%s:%s", s.name, m.name);
}

67) #include <stdio.h>


struct temp
{
int a;
} s;
void func(struct temp)
{
s.a = 10;
printf("%d\t", s.a); s
}
main()
{
func(s);
printf("%d\t", s.a);
}

68) #include<stdio.h>
struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = "ashok";
return s;
}
void main()
{
struct student m = fun();
s.name = "bhai";
printf("%s", m.name);
}
69) #include <stdio.h>
struct point
{
int x;
int y;
};
int main()
{
struct point p = {1};
struct point p1 = {1};
if(p == p1)
printf("if\n");
else
printf("else\n");
}

70) #include <stdio.h>


struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
struct point foo();
int main()
{
struct point p = {1};
struct notpoint p1 = {2, 3};
p1 = foo();
printf("%d\n", p1.x);
}
struct point foo()
{
struct point temp = {1, 2};
return temp;
}
71) #include<stdio.h>
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
int main()
{
struct point p = {1};
struct notpoint p1 = p;
printf("%d\n", p1.x);
}

72) #include <stdio.h>


struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
void foo(struct point);
int main()
{
struct notpoint p1 = {10, 20};
foo(p1);
}
void foo(struct point p)
{
printf("%d\n", p.x);
}
73) #include <stdio.h>
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
void foo(struct point);
int main()
{
struct notpoint p1 = {1, 2};
foo(p1);
}
void foo(struct point p)
{
printf("%d\n", p.x);
}

74) #include <stdio.h>


struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1 = {1, 2};
foo(&p1);
}
void foo(struct point *p)
{
printf("%d\n", *p.x++);
}
75) #include <stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1 = {10, 200};
foo(&p1);
}
void foo(struct point *p)
{
printf("%d\n", *p->x++);
}

76) #include <stdio.h>


struct student fun(void)
{
struct student
{
char *name;
};
struct student s;
s.name = "alan";
return s;
}
void main()
{
struct student m = fun();
printf("%s", m.name);
}

77) #include <stdio.h>


struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = "ashok";
return s;
}
void main()
{
struct student m = fun();
printf("%s", m.name); }
78) #include <stdio.h>
struct temp
{
int a;
int b;
int c;
};
main()
{
struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d\n",sizeof(p));
}

79) #include <stdio.h>


struct temp
{
int a;
int b;
int c;
} p[] = {0};
main()
{
printf("%d", sizeof(p));
}

80) #include <stdio.h>


struct student
{
char *name;
};
struct student s[2];
void main()
{
s[0].name = "123";
s[1] = s[0];
printf("%s%s", s[0].name, s[1].name);
s[1].name = "123";
printf("%s%s", s[0].name, s[1].name);
}
81) #include<stdio.h>
struct student
{
char *name;
};
struct student s[2], r[2];
void main()
{
s[0].name = "alan";
s[1] = s[0];
r = s;
printf("%s%s", r[0].name, r[1].name);
}

82) #include<stdio.h>
struct student
{
char *name;
};
void main()
{
struct student s[2], r[2];
s[1] = s[0] = "alan";
printf("%s%s", s[0].name, s[1].name);
}

83) #include<stdio.h>
struct student
{
};
void main()
{
struct student s[2];
printf("%d", sizeof(s));
}
84) #include <stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {1, 2, 3, 4};
foo(p1);
}
void foo(struct point p[])
{
printf("%d\n", p[1].x);
}

85) #include<stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {1, 2, 3, 4};
foo(p1);
}
void foo(struct point p[])
{
printf("%d\n", p->x);
}

86) #include<stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {1, 2, 3, 4};
foo(p1);
}
void foo(struct point p[])
{
printf("%d %d\n", p->x, ++p->x); }
87) #include<stdio.h>
struct point
{
int x;
int y;
} p[] = {1, 2, 3, 4, 5};
void foo(struct point*);
int main()
{
foo(p);
}
void foo(struct point p[])
{
printf("%d %d\n", p->x, p[2].y);
}

88) #include <stdio.h>


struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {1, 2, 3, 4, 5};
foo(p1);
}
void foo(struct point p[])
{
printf("%d %d\n", p->x, p[3].y);
}

89) #include <stdio.h>


struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {1, 2, 3, 4, 5};
foo(p1);
}
void foo(struct point p[])
{
printf("%d %d\n", p->x, (p + 2).y);}
90) #include <stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1[] = {10, -10, 3, 4, 5};
foo(p1);
}
void foo(struct point p[])
{
printf("%d %d\n", p->x, (p + 2)->y);
}

91) #include <stdio.h>


struct student
{
char *c;
};
void main()
{
struct student s[5];
printf("%d", sizeof(s));
}

92) #include<stdio.h>
struct p
{
int x;
char y;
};
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 3);
if (x == sizeof(int) + sizeof(char))
printf("%d\n", ptr1->x);
else
printf("falsen");
}
93) #include<stdio.h>
struct p
{
int x;
char y;
};
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / sizeof(ptr1));
if (x == 1)
printf("%d\n", ptr1->x);
else
printf("false\n");
}

94) #include<stdio.h>
struct p
{
int x;
char y;
};
typedef struct p* q*;
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
q ptr1 = p1;
printf("%d\n", ptr1->x);
}

95) #include <stdio.h>


struct p
{
int x;
char y;
};
typedef struct p* q*;
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
q ptr1 = p1;
printf("%d\n", ptr1->x);
}
96) #include<stdio.h>
struct temp
{
int a;
} s;
void change(struct temp);
main()
{
s.a = 10;
change(s);
printf("%d\n", s.a);
}
void change(struct temp s)
{
s.a = 1;
}

97) #include <stdio.h>


struct p
{
int x;
int y;
};
int main()
{
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 5);
if (x == 3)
printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
else
printf("false\n");
}

98) #include<stdio.h>
struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
s->c = "hello";
printf("%s", s->c);
}
99) #include<stdio.h>
struct student
{
char *c;
};
void main()
{
struct student *s;
s->c = "hello";
printf("%s", s->c);
}

100) #include <stdio.h>


struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
s->c = "hello";
printf("%s", m.c);
}

101) #include<stdio.h>
struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
(*s).c = "hello";
printf("%s", m.c);
}

102) #include <stdio.h>


struct student
{
char *c;
};
void main()
{
struct student n;
struct student *s = &n;
(*s).c = "hello";
printf("%p\n%p\n", s, &n);}
103) #include<stdio.h>
struct p
{
int x[2];
};
struct q
{
int *x;
};
int main()
{
struct p p1 = {1, 2};
struct q *ptr1;
ptr1->x = (struct q*)&p1.x;
printf("%d\n", ptr1->x[1]);
}

104) #include<stdio.h>
struct p
{
int x[2];
};
struct q
{
int *x;
};
int main()
{
struct p p1 = {1, 2};
struct q *ptr1 = (struct q*)&p1;
ptr1->x = (struct q*)&p1.x;
printf("%d\n", ptr1->x[0]);
}

105) #include <stdio.h>


struct p
{
int x;
int y;
};
int main()
{
struct p p1[] = {1, 2, 3, 4, 5, 6};
struct p *ptr1 = p1;
printf("%d %d\n", ptr1->x, (ptr1 + 2)->x);
}
105) #include<stdio.h>
struct p
{
int x;
char y;
};
int main(){
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / sizeof(struct p));
printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
}

106) #include<stdio.h>
struct p
{
int x;
char y;
};
int main(){
struct p p1[] = {1, 92, 3, 94, 5, 96};
struct p *ptr1 = p1;
int x = (sizeof(p1) / sizeof(struct p));
printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
}

107) #include<stdio.h>
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
struct student m;
s.c = m.c = "hi";
m.point = &s;
(m.point)->c = "hey";
printf("%s\t%s\t", s.c, m.c);
}
108) #include<stdio.h>
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
struct student m;
m.point = s;
(m.point)->c = "hey";
printf("%s", s.c);
}

109) #include <stdio.h>


struct student
{
char *c;
struct student point;
};
void main()
{
struct student s;
s.c = "hello";
printf("%s", s.c);
}

110) #include <stdio.h>


struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
printf("%d", sizeof(s));
}
111) #include <stdio.h>
struct student
{
char *c;
struct student *point;
};
void main()
{
struct student s;
struct student *m = &s;
printf("%d", sizeof(student));
}

112) #include <stdio.h>


struct p
{
int x;
char y;
struct p *ptr;
};
int main()
{
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}

113) #include <stdio.h>


typedef struct p *q;
struct p
{
int x;
char y;
q ptr;
};
typedef struct p *q;
int main()
{
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}
114) #include<stdio.h>
typedef struct p *q;
int main()
{
struct p
{
int x;
char y;
q ptr;
};
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}

115) #include<stdio.h>
int main()
{
typedef struct p *q;
struct p
{
int x;
char y;
q ptr;
};
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->x);
return 0;
}

116) #include <stdio.h>


typedef struct p *q;
struct p
{
int x;
char y;
q ptr;
};
int main()
{
struct p p = {1, 2, &p};
printf("%d\n", p.ptr->ptr->x);
return 0;
}
117) #include <stdio.h>
struct student
{
char a[5];
};
void main()
{
struct student s[] = {"hi", "hey"};
printf("%c", s[0].a[1]);
}

118) #include <stdio.h>


struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main()
{
struct p p;
p->name = "xyz";
p->next = NULL;
ptrary[0] = &p;
printf("%s\n", p->name);
return 0;
}

119) #include <stdio.h>


struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main()
{
struct p p;
p.name = "xyz";
p.next = NULL;
ptrary[0] = &p;
printf("%s\n", ptrary[0]->name);
return 0;
}
120) #include <stdio.h>
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main()
{
struct p p, q;
p.name = "xyz";
p.next = NULL;
ptrary[0] = &p;
strcpy(q.name, p.name);
ptrary[1] = &q;
printf("%s\n", ptrary[1]->name);
return 0;
}

121) #include <stdio.h>


int main()
{
struct p
{
char *name;
struct p *next;
};
struct p p, q;
p.name = "xyz";
p.next = NULL;
ptrary[0] = &p;
strcpy(q.name, p.name);
ptrary[1] = &q;
printf("%s\n", ptrary[1]->name);
return 0;
}

122) #include <stdio.h>


struct student
{
char a[];
};
void main()
{
struct student s;
printf("%d", sizeof(struct student));
}
123) #include <stdio.h>
int main()
{
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
struct p p, q;
p.name = "abc";
p.next = NULL;
ptrary[0] = &p;
q.name = (char*)malloc(sizeof(char)*3);
strcpy(q.name, p.name);
q.next = &q;
ptrary[1] = &q;
printf("%s\n", ptrary[1]->next->next->name);
}

124) #include <stdio.h>


typedef struct student
{
char *a;
}stu;
void main()
{
struct stu s;
s.a = "hi";
printf("%s", s.a);
}

125) #include<stdio.h>
typedef struct student
{
char *a;
}stu;
void main()
{
struct student s;
s.a = "hey";
printf("%s", s.a);
}
126) #include <stdio.h>
typedef int integer;
int main()
{
int i = 10, *ptr;
float f = 20;
integer j = i;
ptr = &j;
printf("%d\n", *ptr);
return 0;
}

127) #include <stdio.h>


typedef struct p
{
int x, y;
};
int main()
{
p k1 = {1, 2};
printf("%d\n", k1.x);
}

128) #include <stdio.h>


typedef struct p
{
int x, y;
}k;
int main()
{
struct p p = {1, 2};
k k1 = p;
printf("%d\n", k1.x);
}

129) #include <stdio.h>


typedef struct student
{
char *a;
}stu;
void main()
{
stu s;
s.a = "hi";
printf("%s", s.a);
}
130) #include<stdio.h>
int main()
{
struct value
{
int bit1:0;
int bit3:2;
int bit4:5;
}bit={2, 1, 15};

printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);


return 0;
}

131) #include<stdio.h>

int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}

132) #include <stdio.h>


struct p
{
char x : 2;
int y : 2;
};
int main()
{
struct p p;
p.x = 2;
p.y = 1;
p.x = p.x & p.y;
printf("%d\n", p.x);
}
133) #include <stdio.h>
struct p
{
unsigned char x : 2;
unsigned int y : 2;
};
int x;
int main()
{
printf("%d\n", p.x);
}

134) #include <stdio.h>


struct p
{
unsigned int x : 2;
unsigned int y : 2;
};
int main()
{
struct p p;
p.x = 3;
p.y = 1;
printf("%d\n", sizeof(p));
}

135) #include <stdio.h>


struct p
{
unsigned int x : 2;
unsigned int y : 2;
};
int main()
{
struct p p;
p.x = 3;
p.y = 4;
printf("%d\n", p.y);
}
136) #include <stdio.h>
struct p
{
unsigned int x : 2;
unsigned int y : 2;
};
int main()
{
struct p p;
p.x = 3;
p.y = 4;
printf("%d\n", p.y);
}

137) #include <stdio.h>


struct p
{
unsigned int x : 1;
unsigned int y : 1;
};
int main()
{
struct p p;
p.x = 1;
p.y = 2;
printf("%d\n", p.y);
}

138) #include<stdio.h>
struct xx
{
int x=3;
char name[] = "hello";
};
struct xx *s = malloc(sizeof(struct xx));
printf("%d", s->x);
printf("%s", s->name);
}
139) #include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
emp int xx;
int a;
printf("%d\n", &a);
return 0;
}

140) #include<stdio.h>

int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}

141) #include<stdio.h>

int main()
{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
strupr(e2.n);
printf("%s\n", e1.n);
return 0;
}
142) #include<stdio.h>

int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d\n", sizeof(p), sizeof(q));
return 0;
}

143) #include<stdio.h>

int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}

144) #include<stdio.h>

struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };

printf("%d ", c[1].courseno);


printf("%s\n", (*(c+2)).coursename);
return 0;
}
145) #include<stdio.h>

int main()
{
struct a
{
float category:5;
char scheme:4;
};
printf("size=%d", sizeof(struct a));
return 0;
}

146) #include<stdio.h>

int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0; i<=9; i++)
scanf("%s %f", e[i].name, &e[i].sal);
return 0;
}

147) #include<stdio.h>
#include<string.h>
void modify(struct emp*);
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
148) #include<stdio.h>

int main()
{
struct bits
{
int i:40;
}bit;

printf("%d\n", sizeof(bit));
return 0;
}

149) #include<stdio.h>

int main()
{
struct emp
{
char n[20];
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
if(e1 == e2)
printf("The structure are equal");
return 0;
}

150) #include<stdio.h>

int main()
{
struct bits
{
float f:2;
}bit;

printf("%d\n", sizeof(bit));
return 0;
}
151) #include<stdio.h>

int main()
{
struct emp
{
char name[25];
int age;
float bs;
};
struct emp e;
e.name = "Suresh";
e.age = 25;
printf("%s %d\n", e.name, e.age);
return 0;
}

152) #include<stdio.h>

struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d={10,12,1990};
struct date *pdt;
pdt=&d;
printf("%d\n",pdt->month);
return 0;
}

153) static struct


{ unsigned a:5;
unsigned b:5;
unsigned c:5;
unsigned d:5%:
}v=(1,2,3,4);
main()
{
printf("%d\n",sizeof(v));
}
154) #include<stdio.h>

int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d\n", sizeof(p), sizeof(q));
return 0;
}

155) int main()


{
struct byte
{
int one:0;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}

156) #include<stdio.h>

struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {10, "Java"},
{103, "PHP"},
{104, "DotNet"} };

printf("%d ", c[1].courseno);


printf("%s\n", (*(c)).coursename);
return 0;
}
157) struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
}

158) #include <stdio.h>


int main()
{
struct sample{
int a;
int b;
sample *s;
}t;

printf("%d,%d",sizeof(sample),sizeof(t.s));
return 0;
}

159) #include <stdio.h>


#include <string.h>

struct student
{
char name[20];
}std;
char * fun(struct student *tempStd)
{
strcpy(tempStd->name,"Thomas");
return tempStd->name;
}

int main()
{
strcpy(std.name,"Mike ");
printf("%s%s",std.name,fun(&std));
return 0;
}
160) #include <stdio.h>

struct sample
{
int a;
}sample;

int main()
{
sample.a=100;
printf("%d",sample.a);
return 0;
}

161) #include <stdio.h>


struct employee{
int empId;
char *name;
int age;
};
int main()
{
struct employee emp []={ {1,"Mike",24}, {2,"AAA",24}, {3,"BBB",25}, {4,"CCC",30} };

printf("Id : %d, Age : %d, Name : %s", emp[2].empId,3[emp].age,(*(emp+1)).name);


return 0;
}

162) #include <stdio.h>


int main()
{
typedef struct tag{
char str[10];
int a;
}har;

har h1,h2={"IHelp",10};
h1=h2;
h1.str[1]='h';
printf("%s,%d",h1.str,h1.a);
return 0;
}
163) #include <stdio.h>
int main()
{
struct std
{
char name[30];
int age;
};
struct std s1={"Mike",26};
struct std s2=s1;

printf("Name: %s, Age: %d\n",s2.name,s2.age);


}

164) #include <stdio.h>

struct test {

int x;

char y;

} test;

int main()

test.x = 10;

test.y = 'A';

printf("%d %c", test.x,test.y);

return 0;

}
165) #include <stdio.h>

struct result{

char sub[20];

int marks;

};

void main()

struct result res[] = { {"Maths",100},

{"Science",90},

{"English",85}

};

printf("%s ", res[1].sub);

printf("%d\n", (*(res+2)).marks);

166) what will be the size of the following structure?

struct demo{

int a;

char b;

float c;

}
167) #include<stdio.h>

void main()

struct demo{

char * a;

int n;

};

struct demo p = {"hello", 2015};

struct demo q = p;

printf("%d", printf("%s",q.a));

168) #include<stdio.h>

int main(){

struct simp

int i = 6;

char city[] = "chennai";

};

struct simp s1;

printf("%d",s1.city);

printf("%d", s1.i);

return 0;

}
169) #include<stdio.h>

struct

int i;

float ft;

}decl;

int main(){

decl.i = 4;

decl.ft = 7.96623;

printf("%d %.2f", decl.i, decl.ft);

return 0;

170) void main()


{
struct bitfields {

int bits_1: 2;

int bits_2: 9;

int bits_3: 6;

int bits_4: 1;

}bit;

printf("%d", sizeof(bit));

}
171) struct bitfields {

int bits_1: 2;

int bits_2: 9;

int bits_3: 6;

int bits_4: 1;

}bit;

printf("%d", sizeof(bit));

172) #include<stdio.h>

struct employee

char *empname;

int salary;

};

int main()

struct employee e, e1;

e.empname = "Sridhar";

e1 = e;

printf("%s %s", e.empname, e1.empname);

return 0;

}
173) #include <stdio.h>

struct student

int no = 5;

char name[20];

};

void main()

struct student s;

s.no = 8;

printf("hello");

174) #include <stdio.h>

void main()

struct number

int no;

char name[20];

};

struct number s;

s.no = 50;

printf("%d", s.no);

}
175) #include<stdio.h>

struct student

char *c;

};

void main()

struct student s[2];

printf("%d", sizeof(s));

176) #include<stdio.h>
int main()
{
struct num
{
int i, j, k, l;
};
struct num n = {10, 20, 30};
printf("%d %d %d %d", n.i, n.j, n.k, n.l);
}

177) #include<stdio.h>
int main()
{
struct st
{
int i;
static int si;
};
struct st s = {0.1, 2.0};
printf("%d %d", s.i, s.si);
return 0;
}
178) #include<stdio.h>
int main()
{
struct alphabets
{
char firstLetter;
struct alphabets a;
}al;
al.firstLetter = 'a';
printf("%c", al.firstLetter);
}

179) #include<stdio.h>
int main()
{
struct check
{
}ck;
printf("%d", sizeof(ck));
}

180) #include<stdio.h>
int main()
{
struct check
{
}ck;
printf("%d", sizeof(ck));
}

181) struct marks{


int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
}
182) main()
{
struct student
{
char name[20];
int roll;
};
struct student s1 = { "adam", 101 };
struct student s2 = s1;
printf("%s", s2.name);
}

183)
point out the error in the code?

struct Student
{
char[20] name;
int rollno;
struct Student s2;
};

184) Dot(.)perator can be used to access structure elements using a structure variable. True or False?

185) #include <stdio.h>

struct student{
char name[30];
int rollNo;

struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB;/*created structure varoable DOB*/
};

int main()
{
struct student std;

printf("Enter name: "); gets(std.name);


printf("Enter roll number: "); scanf("%d",&std.rollNo);
printf("Enter Date of Birth [DD MM YY] format: ");
scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
printf("\nName : %s \nRollNo : %d \nDate of birth : %02d/%02d/%02d\
n",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy);

return 0; }
186) #include<stdio.h>

struct Point
{
int x, y, z;
};

int main()
{
struct Point p1 = {.y = 0, .z = 1, .x = 2};
struct Point p2 = {.x = 20};

printf ("x = %d, y = %d, z = %d\n", p1.x, p1.y, p1.z);


printf ("x = %d", p2.x);
return 0;
}

187) #include<stdio.h>

struct Point
{
int x, y;
};

int main()
{
struct Point arr[10];

arr[0].x = 10;
arr[0].y = 20;

printf("%d %d", arr[0].x, arr[0].y);


return 0;
}
188) #include<stdio.h>

struct Point
{
int x, y;
};

int main()
{
struct Point p1 = {1, 2};

// p2 is a pointer to structure p1
struct Point *p2 = &p1;

// Accessing structure members using structure pointer


printf("%d %d", p2->x, p2->y);
return 0;
}

189) which of the following operation is illegal in structures?


a) Typecasting of structure
b) Pointer to a variable of the same structure
c) Dynamic allocation of memory for structure
d) All of the mentioned

190) which of the following is not possible under any scenario?


a) s1 = &s2;
b) s1 = s2;
c) (*s1).number = 10;
d) None of the mentioned

191) presence of code like “s.t.b = 10” indicates


a) Syntax Error
b) Structure
c) double data type
d) An ordinary variable name
192)
how many bytes in memory taken by the following C structure?
#include<stdio.h>
struct test
{
int k;

char c;
};
a) Multiple of integer size
b) integer size+character size
c) Depends on the platform
d) Multiple of word size

193) which of the following is not possible under any scenario?


a) s1 = &s2;
b) s1 = s2;
c) (*s1).number = 10;
d) None of the mentioned

194) which of the following operation is illegal in structures?


a) Typecasting of structure
b) Pointer to a variable of the same structure
c) Dynamic allocation of memory for structure
d) All of the mentioned

195) can we declare function inside structure of C Programming?

196) what is important difference between structure & union?


A) There is no difference
(B) Union takes less memory
(C) Union is faster
(D) Structure is fasteri

197) Is it necessary that all elements of structure should be different in size?

198) Are self referential structures are possible in C?

199) can we compare two structures using any built in operator?


200) #include <stdio.h>
int main()
{
struct tag{
int a;
float b;
};

struct tag t={10,10.23f};


printf("%d, %.02f\n",t.a,t.b);
return 0;
}

Answers:
------------------------------

1)compile error
2)4
3)Ihelp, 10
4)100
5)10, 10.23
6)Mani, 21
7)compile error
8)compile error
9)compile error
10)2 0 1
11)1 0 2 0
12)1 0 1,2 0 2
13)100 A
14)compile error
15)Cbasics=10
16)boat2=10
17)RED RED
18)compile error
19)0 10
20)0 0
21)Name=ACER
22)compile error
23)15
24)5 9
25)1234
26)5
27)8
28)4
29)1 5
30)SAME
31)newton alan alan
32)stst
33)10 0
34)compile error
35)compile error
36)None
37)compile error
38)compile error
39)10 10
40)0.000000 10
41)3.000000
42)0.000000
43)0.000000
44)compile error
45)compile error
46)compile error
47)compile error
48)compile error
49)compile error
50)compile error
51)alan
52)compile error
53)compile error
54)compile error
55)8
56)57
57)compile error
58)compile error
59)compile error
60)compile error
61)0.000000 12
62)3.000000
63)0.000000
64)0.000000
65)compile error
66)At:At
67)compile error
68)compile error
69)compile error
70)compile error
71)compile error
72)compile error
73)compile error
74)compile error
75)compile error
76)compile error
77)ashok
78)36
79)12
80)123123123123
81)compile error
82)compile error
83)0
84)3
85)1
86)2 2
87)1 0
88)1 Garbage
89)compile error
90)10 0
91)20
92)false
93)false
94)compile error
95)compile error
96)10
97)false
98)hello
99)hello
100)hello
101)hello
102)0xbfe4be78 0xbfe4be78
103)2
104)Garbage
105)1 5
106)1 5
107)hey hi
108)compile error
109)compile error
110)8
111)compile error
112)1
113)1
114)compile error
115)1
116)1
117)i
118)compile error
119)xyz
120)runtime error
121)compile error
122)compile error
123)abc
124)compile error
125)hey
126)10
127)compile error
128)1
129)hi
130)compile error
131)4
132)0
133)compile error
134)4
135)0
136)0
137)0
138)compile error
139)compile error
140)4
141)compile error
142)4,4
143)-1
144)103 Dotnet
145)compile error
146)infinite
147)compile error
148)compile error
149)compile error
150)compile error
151)compile error
152)12
153)compile error
154)4 4
155)compile error
156)103 java
157)compile error
158)compile error
159)thomas thomas
160)100
161)Id : 3, Age : 30, Name : AAA
162)Ihelp,10
163)Name: Mike, Age: 26
164)10 A
165)Science 85
166)12
167)hello 5
168)compile error
169)4 7.97
170)4
171)compile error
172)Sridhar Sridhar
173)compile error
174)50
175)8
176)10 20 30 0
177)compile error
178)compile error
179)0
180)0
181)2 2 1
182)adam
183)error in structure declartion
184)true
186)x = 2, y = 0, z = 1 x = 20
187)10 20
188)1 2
189)a
190)d
191)b
192)a
193)d
194)a
195)no
196)B
197)no
198)yes
199)no
200)10 10.23

You might also like