160 likes | 335 Views
Today’s Agenda. Array of Structures and pointers Dynamic Arrays Integer Arrays Float Arrays Character Arrays Array of structures Dynamic allocation c alloc () realloc () free() Function pointers. Book Array and Pointers. Static Book Array. typedef struct { int bookid ;
E N D
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
Book Array and Pointers • Static Book Array
typedefstruct { intbookid; 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); }
void readBooks(book *p,int size) { inti=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) { inti=0; printf("\nDisplay details of books\n"); while(i<size) { printf("address of %dth book = <%u> , %d %s\n",i,p,p->bookid,p->bname); i++; p++; } }
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.
[Mayuri@localhost cp2]$ ./a.out 10 20 30 40 50 60 70 80 90 100 10 20 30 40 50 60 70 80 90 100 isum = 550 1.1 1.2 1.3 1.4 1.5 1.100000 1.200000 1.300000 1.400000 1.500000 fsum = 6.500000 abcdefghij abcdefghij length of string <abcdefghij> is 10 #include<stdio.h> #include<stdlib.h> int main() { int *p; float *q; float fsum=0.0; char *c; inti, 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
//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
// 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
//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
Write a program to create dynamic array of book structure for storing 5 books. Write read and print functions for the same.
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 0th book = <29646864> , 1 CP1 address of 1th book = <29646888> , 2 CP2 address of 2th book = <29646912> , 3 MATHS address of 3th book = <29646936> , 4 PHY address of 4th book = <29646960> , 5 CHE 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); }
void readBooks(book *p,int size) { inti=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++; } }
void printBooks(book *p,int size) { inti=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 %dthbook = <%u> , %d %s\n",i,p+i,(p+i)->bookid,(p+i)->bname); i++; } }
Allocating multiple block of memory • calloc() • Altering the size of a block • realloc() • Releasing the used space • free()
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));
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);