Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Agenda Array of Structures and pointers Dynamic Arrays – Integer Arrays – Float Arrays – Character Arrays – Array of structures Dynamic allocation.

Similar presentations


Presentation on theme: "Today’s Agenda Array of Structures and pointers Dynamic Arrays – Integer Arrays – Float Arrays – Character Arrays – Array of structures Dynamic allocation."— Presentation transcript:

1 Today’s Agenda Array of Structures and pointers Dynamic Arrays – Integer Arrays – Float Arrays – Character Arrays – Array of structures Dynamic allocation – calloc() – realloc() – free() Function pointers

2 Book Array and Pointers Static Book Array

3 typedef struct { int bookid; char bname[20]; }book; int main() { book *pb,b[5]; pb = b; printf("Memory required for one book is %d bytes\n",sizeof(book)); readBooks(pb,5); printBooks(pb,5); }

4 void readBooks(book *p,int size) { int i=0; printf("Enter details of books\n"); while(i<size) { scanf("%d %s",&(p+i)->bookid,(p+i)->bname); i++; } void printBooks(book *p,int size) { int i=0; printf("\nDisplay details of books\n"); while(i<size) { printf("address of %dth book =, %d %s\n",i,p,p->bookid,p->bname); i++; p++; }

5 Dynamic Arrays Write a program to create dynamic integer array, float array and char array. Compute the sum of elements in both integer and float arrays. Compute the length of character array.

6 #include int main() { int *p; float *q; float fsum=0.0; char *c; int i, isum, len; sum = 0; //for sum of ingeters in integer array len = 0; //for computing length of string p = (int *)malloc(sizeof(int)*10); q = (float *)malloc(sizeof(float)*5); c = (char *)malloc(sizeof(char)*50); //main is continued on next slide [Mayuri@localhost cp2]$./a.out 10 20 30 40 50 60 70 80 90 100 isum = 550 1.1 1.2 1.3 1.4 1.5 1.1000001.2000001.3000001.4000001.500000 fsum = 6.500000 abcdefghij length of string is 10

7 //read integers, print them and compute sum. for(i=0;i<10;i++) scanf("%d",p+i); for(i=0;i<10;i++) printf("%d\t",*(p+i)); for(i=0;i<10;i++) isum += *(p+i); printf("\nsum = %d\n",isum); //main is continued on next slide

8 // read float numbers, print them and compute sum. for(i=0;i<5;i++) scanf("%f",q+i); // scanf("%f",&q[i]); for(i=0;i<5;i++) printf("%f\t",*(q+i)); // printf("%f\t",q[i]); for(i=0;i<5;i++) fsum += *(q+i); printf("\nsum = %f\n",fsum); //main is continued on next slide

9 //read a string, print the string and compute length scanf("%s",c); printf("%s\n",c); i=0; while(*(c+i) !='\0') { len++; i++; } printf("length of string %s is %d\n",c,len); } // main ends here

10 Write a program to create dynamic array of book structure for storing 5 books. Write read and print functions for the same.

11 typedef struct { int bookid; char bname[20]; }book; int main() { book *pb; pb = (book *)malloc(sizeof(book)*5); printf("Memory required for one book is %d bytes\n",sizeof(book)); readBooks(pb,5); printBooks(pb,5); } Memory required for one book is 24 bytes Enter details of books 1 CP1 2 CP2 3 MATHS 4 PHY 5 CHE Display details of books address of 0 th book =, 1 CP1 address of 1 th book =, 2 CP2 address of 2 th book =, 3 MATHS address of 3 th book =, 4 PHY address of 4 th book =, 5 CHE

12 void readBooks(book *p,int size) { int i=0; //book *q = p; printf("Enter details of books\n"); while(i<size) { //scanf("%d %s",&q->bookid,q->bname); this also works scanf("%d %s",&(p+i)->bookid,(p+i)->bname); i++; //q++; }

13 void printBooks(book *p,int size) { int i=0; //book *q = p; printf("\nDisplay details of books\n"); while(i<size) { /*printf("%d %s\n",q->bookid,q->bname); //this also works i++; q++;*/ printf("address of %d th book =, %d %s\n",i,p+i,(p+i)->bookid,(p+i)->bname); i++; }

14 Allocating multiple block of memory – calloc() Altering the size of a block – realloc() Releasing the used space – free()

15 Calloc() It can allocate multiple blocks of memory Syntax – ptr = (cast_type *) calloc(n,size of block); Example book *pb; pb = (book *)calloc(5,sizeof(book));

16 Realloc() This function can modify the already allocated size of memory It can reduce the size or increase the size Syntax – ptr = (type_cast*) realloc(ptr,newsize); Example char *p; p = (char *)malloc(5); p = (char *)realloc(p,10);


Download ppt "Today’s Agenda Array of Structures and pointers Dynamic Arrays – Integer Arrays – Float Arrays – Character Arrays – Array of structures Dynamic allocation."

Similar presentations


Ads by Google