Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Computer Programming

Pratik Narang
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

Structures
Why another data structure?
• We have studied Arrays
• All elements are of the same type
• Structure
• Individual elements can differ in type
• A single structure may contain
• integer
• Float
• char
• Pointers
• Arrays
• Other structures
Defining a structure
struct tag {
member 1;
member 2;

};

[storage-class] struct tag var1;

Examples…
static struct account c = {9, ‘R’, “John”, 33.3, 4, 5, 19};
Processing a struct
• variable.member
• Members are processed individually.
• The . operator is a member of the highest
precedence group.

• What about:
• ++variable.member
• &variable.member

• Examples…
typedef
• Allows users to define new data-types equivalent to
existing ones.
• typedef type new-type
• Examples…
Size of a structure
struct test_struct {
char a[3];
a= 3
short int b; b= 2
int c; c= 4
d= 3
char d[3]; 16

} test;
printf("a= %lu\n", sizeof(test.a));
printf("b= %lu\n", sizeof(test.b));
printf("c= %lu\n", sizeof(test.c));
printf("d= %lu\n", sizeof(test.d));
printf("%lu\n", sizeof(test));
Examples
Passing structs to functions
• Entire structure or individual members can be
passed as function arguments and can be returned
from function calls

• We can pass the entire structure by value or by


reference (passing a pointer to the structure)

• Passing large structures by value -> copies involved


-> waste of memory and compute
Example
typedef struct {
int numer;
int denom;
} FRAC;

typedef struct {
int whole;
FRAC f;
} MIXED;

FRAC addFrac(FRAC f1, FRAC f2);


MIXED addMixed(FRAC f, MIXED m);
Example
struct dice { int face ;};
typedef struct dice Dice;
void passByValue(Dice c) { c.face = 5; }

int main(void) {
Dice c ;
c.face = 1;
printf("c.face before = %d\n", c.face);
passByValue(c);
printf("c.face after = %d\n",c.face);
}
Array of structures
typedef struct {
char[15] idno;
char[50] name;
char gender;
int age;
float cgpa;
} Student;

Student arr[N];
for (i = 0; i < N; ++i)
{
scanf("%[^,]", arr[i].idno);
getchar(); // dissipating the field separ. ,
scanf("%[^,]", arr[i].name);
getchar(); // dissipating the field separ. ,
arr[i].gender = getchar();
getchar(); // dissipating the field separ. ,
scanf("%d", &arr[i].age);
getchar(); // dissipating the field separ. ,
scanf("%f", &arr[i].cgpa);
getchar(); // dissipating the \n
}
Data:

2018A1PS0056P,Anmol,M,18,6.00
2016B2A70345P,Sarita,F,20,8.78
Pointer to a structure
typedef struct {
char[15] idno;
char[50] name;
char gender;
int age;
float cgpa;
} Student;

Student* s1;
Example
struct dice { int face ;};
typedef struct dice Dice;
void passByReference(Dice *c) {c->face = 4;}

int main(void) {
Dice c ;
c.face = 1;
Dice *cptr = &c ; // ptr to c
printf("c.face before = %d\n", c.face);
printf("cptr before = %d\n",cptr->face);
passByReference(cptr);
printf("c.face after = %d\n",c.face); }
Structure on heap memory
Dice* allocateMem() {
Dice* x = malloc(sizeof(Dice));
x->face = 5;
printf(“x->face: %d\n",x->face);
return x; }

int main() {
Dice* s = allocateMem();
printf(“s->face: %d\n", s->face);
free(s);
return 0; }

You might also like