0% found this document useful (0 votes)
28 views

Dynamic 2D Using Single Pointer 7

The document discusses different approaches for dynamically allocating multi-dimensional arrays in C, including using a single pointer, an array of pointers, and character arrays. It provides code examples for declaring and accessing 2D arrays using each of these approaches as well as examples for 1D dynamic allocation and reallocation. The examples demonstrate how to allocate and access the dynamic arrays, input and output values to them, and free the allocated memory.

Uploaded by

Jonathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Dynamic 2D Using Single Pointer 7

The document discusses different approaches for dynamically allocating multi-dimensional arrays in C, including using a single pointer, an array of pointers, and character arrays. It provides code examples for declaring and accessing 2D arrays using each of these approaches as well as examples for 1D dynamic allocation and reallocation. The examples demonstrate how to allocate and access the dynamic arrays, input and output values to them, and free the allocated memory.

Uploaded by

Jonathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dynamic 2D using single pointer

#include <stdio.h>

#include <stdlib.h>

int main()

{ int *arr = (int *)malloc(r * c * sizeof(int));

int r=3, c=3;

int i, j, count = 0;

/* for (i = 0; i < r; i++)

for (j = 0; j < c; j++)

*(arr + i*c + j) = ++count;

for (i = 0; i < r; i++)

for (j = 0; j < c; j++)

printf("%d ", *(arr + i*c + j)); */

int i,j;

int ci=0;

int *a=(int *)malloc(r * c * sizeof(int)); // int a[i]

for (i=0;i<9;i++)

scanf("%d",&a[i]);

for(i=0;i<r;i++)

{
for(j=0;j<c;j++)

printf("address[%d][%d]%d \t",i,j,*(a+ i*c +j));

return 0;

Dynamic 2D using array of pointer

#include <stdio.h>

#include <stdlib.h>

int r=3;

int c=4;

int main()

// Dynamic 2D using array of pointer

int *a[r] ;

int i,j;

for(i=0;i<r;i++)

a[i]= (int *)malloc(c * sizeof(int));

for(i=0;i<r;i++)

for (j=0;j<c;j++)

scanf("%d",&a[i][j]);

}
for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("address[%d][%d]%d \t",i,j,*(*(a+i)+j));

return 0;

Dynamic character array

#include <stdio.h>

#include <stdlib.h>

int r=4;

int c=4;

int main()

// Dynamic 2D using array of pointer

char *a[4]; //char a[4][5];

//int *a[r] ;

int i,j;

for(i=0;i<r;i++)

a[i]= (char *)malloc(c * sizeof(char));

for(i=0;i<r;i++)

fgets(a[i],sizeof(a[i]),stdin);
for(i=0;i<r;i++)

puts(a[i]);

return 0;

int main()

int *aptr,*valptr;

aptr = (int *) calloc(r*c,sizeof(int));

valptr= aptr;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf(“%d”,valptr);

valptr++;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf(“%d”,*valptr);

valptr++;
}

int main()

int *aptr,*valptr,r=3,c=3,i,j;

aptr = (int *) calloc(r*c,sizeof(int));

valptr= aptr;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf("%d",valptr);

valptr++;

valptr=aptr;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("%d",*valptr);

valptr++;

free(valptr);
free(aptr);

*/

#include<stdio.h>

int main()

int *xptr,i;

xptr=(int *) calloc(10,sizeof(int));

xpt=(int *) malloc(10*sizeof(int));

for(i=0;i<10;i++)

scanf("%d",xptr);

xptr++;

xptr= xptr-10;

for(i=0;i<10;i++)

printf("%d",*xptr);

xptr++;

free(xptr);

}
Dynamic 1D

int *arr;

int main()

Size_t arrsize=200

arr=calloc (arrsize,sizeof(int));

if(arr==NULL)

printf(“Memory is not allocated”);

arrsize *=2;

arr = realloc(arr, arrsize*sizeof(int));

if(arr==NULL)

printf(“Memory is not allocated”);

arr[10]=20;

printf(“%d”,arr[10]);

free(arr);

}
/*#include<stdio.h>

main()

{
1 2 3
//int a[3][3] = {1,2,3,4,5,6,7,8,9};
4 5 6
//printf("%d",sizeof(a));

//} 9 *4 =36 bytes 7 8 9

a[0] =1;

a[2][0] = 7;

int i;

int** arr=malloc(sizeof(int *)*3);

for(i=0;i<3;i++) 1 4 7

{ 5 8
2
arr[i] = malloc(sizeof(int) *3);
3 6 9
}

printf("%d",sizeof(arr)+sizeof(arr[0])*3 +sizeof(int)*3 *3);

return 0;

4*9+8+3*8

68 bytes

Main()

int i;

char sname[80];

char *strptr[4];

for(i=0;i<4;i++)

{
gets(sname);

strptr[i]= (char *) malloc(strlen(sname)+1);

strcpy(strptr[i],sname);

for(i=0;i<4;i++)

puts(sname);

You might also like