Structures
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;
int main()
{
sample.a = 100;
printf("%d", sample.a);
return 0;
}
5) #include <stdio.h>
int main()
{
struct tag{
int a;
float b;
};
6) #include <stdio.h>
int main()
{
struct person{
char *name;
int age;
};
printf("%s, %d\n",p.name,p.age);
return 0;
}
7) #include <stdio.h>
int main()
{
struct person{
char name[30];
int age;
};
//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;
}
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;
}
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;
}
struct student
char *name;
};
struct student s;
s.name = "newton";
printf("%s\n", s.name);
s.name = "alan";
return s;
void main()
printf("%s\n", m.name);
m.name = "turing";
printf("%s\n", s.name);
char *name;
};
void main()
struct student s, m;
s.name = "st";
m = s;
struct temp
int a;
} 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 s;
s.name = "alan";
return s;
void main()
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);
#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;
}
39) #include <stdio.h>
struct p
int k;
char c;
};
int p = 10;
int main()
struct p x;
x.k = 10;
struct p
int k;
char c;
float f;
};
int p = 10;
int main()
int k;
char c;
float f;
};
int main()
printf("%f\n", x.f);
struct p
int k;
char c;
float f;
};
int main()
}
43) #include <stdio.h>
struct p
int k;
char c;
float f;
};
int main()
printf("%f\n", x.f);
}
44) #include <stdio.h>
struct point
int x;
int y;
};
int main()
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;
};
int main()
p1 = foo();
printf("%d\n", p1.x);
return temp;
}
46) #include<stdio.h>
struct point
int x;
int y;
};
struct notpoint
int x;
int y;
};
int main()
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;
};
int main()
foo(p1);
printf("%d\n", p.x);
48) #include<stdio.h>
struct point
int x;
int y;
};
int main()
foo(&p1);
printf("%d\n", *p.x++);
}
49) #include<stdio.h>
struct point
int x;
int y;
};
int main()
foo(&p1);
printf("%d\n", *p->x++);
}
50) #include <stdio.h>
struct student
char *name;
};
struct student s;
s.name = "alan";
return s;
void main()
printf("%s", m.name);
}
51) #include<stdio.h>
struct student
char *name;
};
struct student s;
s.name = "alan";
return s;
void main()
printf("%s", m.name);
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);
}
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");
}
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);
}
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);
}
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);
}
101) #include<stdio.h>
struct student
{
char *c;
};
void main()
{
struct student m;
struct student *s = &m;
(*s).c = "hello";
printf("%s", m.c);
}
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]);
}
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);
}
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;
}
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;
}
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;
}
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"} };
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;
}
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;
}
156) #include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {10, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d,%d",sizeof(sample),sizeof(t.s));
return 0;
}
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;
}
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;
struct test {
int x;
char y;
} test;
int main()
test.x = 10;
test.y = 'A';
return 0;
}
165) #include <stdio.h>
struct result{
char sub[20];
int marks;
};
void main()
{"Science",90},
{"English",85}
};
printf("%d\n", (*(res+2)).marks);
struct demo{
int a;
char b;
float c;
}
167) #include<stdio.h>
void main()
struct demo{
char * a;
int n;
};
struct demo q = p;
printf("%d", printf("%s",q.a));
168) #include<stdio.h>
int main(){
struct simp
int i = 6;
};
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;
return 0;
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()
e.empname = "Sridhar";
e1 = e;
return 0;
}
173) #include <stdio.h>
struct student
int no = 5;
char name[20];
};
void main()
struct student s;
s.no = 8;
printf("hello");
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()
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));
}
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?
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;
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};
187) #include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point arr[10];
arr[0].x = 10;
arr[0].y = 20;
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
char c;
};
a) Multiple of integer size
b) integer size+character size
c) Depends on the platform
d) Multiple of word size
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