













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














A block of memory can be allocated using the
function malloc Reserves a block of memory of specified size and returns a pointer of type void The return pointer can be type-casted to any pointer type
General format:
type *p; p = (type *) malloc (byte_size);
cptr = (char *) malloc (20);
Allocates 20 bytes of space for the pointer cptr of type char
sptr = (struct stud ) malloc(10sizeof(struct
stud));
Allocates space for a structure array of 10 elements. sptr points to a structure element of type struct stud
malloc always allocates a block of contiguous bytes
The allocation can fail if sufficient contiguous memory space is not available
If it fails, malloc returns NULL
printf("Input heights for %d students \n",N); for (i=0; i<N; i++) scanf ("%f", &height[i]);
for(i=0;i<N;i++) sum += height[i];
avg = sum / (float) N;
printf("Average height = %f \n", avg); free (height); return 0; }
*int main() { int i,N; float height; float sum=0,avg;
printf("Input no. of students\n"); scanf("%d", &N);
*height = (float ) malloc(N * sizeof(float));
Releasing the Allocated Space:
free
An allocated block can be returned to the
system for future use by using the free function
General syntax:
free (ptr); where ptr is a pointer to a memory block which has been previously created using malloc
Note that no size needs to be mentioned for the
allocated block, the system remembers it for each pointer returned
13
malloc( )-ing array of structures
*typedef struct{ char name[20]; int roll; float SGPA[8], CGPA; } person; int main() { person student; int i,j,n; scanf("%d", &n); student = (person )malloc(nsizeof(person)); for (i=0; i<n; i++) { scanf("%s", student[i].name); scanf("%d", &student[i].roll); for(j=0;j<8;j++) scanf("%f", &student[i].SGPA[j]); scanf("%f", &student[i].CGPA); } return 0; }
Static array of pointers
*#define N 20 #define M 10 int main() { char word[N], w[M]; int i, n; scanf("%d",&n); for (i=0; i<n; ++i) { scanf("%s", word); w[i] = (char ) malloc ((strlen(word)+1)sizeof(char)); strcpy (w[i], word) ; } for (i=0; i<n; i++) printf("w[%d] = %s \n",i,w[i]); return 0; }
Pointers are also variables (storing addresses),
so they have a memory location, so they also have an address
Pointer to pointer – stores the address of a
pointer variable
Dynamic Arrays of pointers
*int main() { char word[20], w; / “w” is a pointer to a pointer array */ int i, n; scanf("%d",&n); w = (char *) malloc (n * sizeof(char )); for (i=0; i<n; ++i) { scanf("%s", word); w[i] = (char ) malloc ((strlen(word)+1)sizeof(char)); strcpy (w[i], word) ; } for (i=0; i<n; i++) printf("w[%d] = %s \n",i, w[i]); return 0; }
Dynamic Arrays of pointers
*int main() { char word[20], w; / “w” is a pointer to a pointer array */ int i, n; scanf("%d",&n); w = (char *) malloc (n * sizeof(char )); for (i=0; i<n; ++i) { scanf("%s", word); w[i] = (char ) malloc ((strlen(word)+1)sizeof(char)); strcpy (w[i], word) ; } for (i=0; i<n; i++) printf("w[%d] = %s \n",i, w[i]); return 0; }
India Australia Kenya NewZealand SriLanka w[0] = India w[1] = Australia w[2] = Kenya w[3] = NewZealand w[4] = SriLanka
Output