0% found this document useful (0 votes)
64 views27 pages

Teamwork Presentation

This document discusses various C programming concepts related to pointers and memory allocation in C including: 1. Static and dynamic memory allocation at compile time and runtime. Functions like malloc(), calloc(), free(), and realloc() are used for dynamic allocation. 2. Pointers can be used to access array elements and pass arrays to functions. The address of an array element is calculated using its index and data type scale factor. 3. Multi-dimensional arrays can be accessed using pointers by treating them as arrays of arrays. Pointer notation is used to demonstrate accessing 2D and 3D arrays. 4. Arrays can contain pointers as elements, allowing arrays of heterogeneous data types. Pointer to pointer variables

Uploaded by

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

Teamwork Presentation

This document discusses various C programming concepts related to pointers and memory allocation in C including: 1. Static and dynamic memory allocation at compile time and runtime. Functions like malloc(), calloc(), free(), and realloc() are used for dynamic allocation. 2. Pointers can be used to access array elements and pass arrays to functions. The address of an array element is calculated using its index and data type scale factor. 3. Multi-dimensional arrays can be accessed using pointers by treating them as arrays of arrays. Pointer notation is used to demonstrate accessing 2D and 3D arrays. 4. Arrays can contain pointers as elements, allowing arrays of heterogeneous data types. Pointer to pointer variables

Uploaded by

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

Presentation by Aarti

Declaration And Initialization


MEMORY
ALLOCATION

STATIC(AT
DYNAMIC(AT
COMPILE
RUNTIME)
TIME)

Eg. Static
MALLOC CALLOC Int a =5;
(MEMORY (CONTIGUOUS
ALLOCATION.) ALLOCATION.)

FREE(FREES REALLOC(M
PREVIOUSLY ODIFIES SIZE
ALLOCATED OF
SPACE.) PREVIOUSLY
ASSIGNED
SPACE.)
POINTER AND ONE DIMENSIONAL
ARRAY
The array declared as: static int x[5]={1,2,3,4,5};
 Is stored as follows:

Elements x[0] x[1] x[2] x[3] x[4]


1 2 3 4 5
Value
Address 1000 1002 1004 1006 1008
assignment statement: p=x; which is equivalent to
p=&x[0]; now we can access every value of x using
p++ to move from one element to another
P=&x[0] (=1000),p+1=&x[1] (=1002) ,p+2=&x[2]
(=1004),p+3=&x[3] (=1006)
The address of an element is calculated using
its index and the scale factor of the data type.
Eg. address of x[3]=base address+(3*scale
factor of int)
=1000+(3*2)=1006

Example of pointer and one dimensional array :


#include<stdio.h> Output:
The address of a is
#include<conio.h>
=65524
Void main() The address of b is
{ =65522
Int a=10,b=20,*p1,*p2; The value on
Clrscr(); address 65524 is
P1=&a; =10
P2=&b; The value on
Printf(“the address of a is =%u\n”,p1); address 65522 is
=20
Printf(“the address of b is =%u\n”,p2);
The update value
Printf(“the value on address %u is =%d\n”,p1,a); of a and b is
Printf(“the value on address %u is =%d\n”,p2,b); =100,25
*p1=*p1*10;
*p2=*p2+5;
Printf(“the update value of a and b is =%d\n”,*p1,*p2);
Getch();
}
POINTERS AND
MULTIDIMENSIONAL ARRAYS
 Pointer And Two Dimensional Array
 Pointer And Three Dimensional Array

 2-D Array :A Two Dimensional Array Can Be


Defined As A Pointer To A Group Of A
Contiguous One Dimensional Arrays.
2-D Array Declaration :
data_type(*ptvar)[expression 2];
Rather then data_type
array[expression1][expression 2];
Eg.Suppose that x is a 2-D array having 10 rows and 20
columns.
Declare x as int(*x)[20];rather than int x[10][20]
X is defined to be a pointer to a group of contiguous,1-
d,20 element integer arrays.Thus,x points to the first 20-
elements array. Thus, x points to first 20-elements
array,which is actually the first row(row 0) of the original
2-D array.Similarly,(x+1) points to the second 20-
elements array,which is the second row (row 1) of the
original 2-D array and so on,
first 1-D Array
X->
second 1-D Array
X->
nth 1-D Array
X->
#include<stdio.h> Output:
#include<conio.h> The address of 0th
Void main() array =0012FEA8
{ 0012FEA8
10 10 11 11 12 12 13
Int arr[3][4]={{10,11,12,13},{20,21,22,23}, 13
{30,31,32,33}},i,j; The address of 1th
Clrscr(); array =0012FEB8
For(i=0;i<3;i++) 0012FFB8
{ 20 20 21 21 22 22
23 23
Printf(“the address of %dth array =%u %u\n”,i, The address of 2th
arr[i],*(arr+i)); array =0012FEC8
For(j=0;j<4,j++) 0012FEC8
Printf(“%d %d,arr[i][j],*(*(arr+i)+j)); 30 30 31 31 32 32
33 33
Printf(“\n”);
}
Getch();
}
Pointer and 3-D array
Declaration:
int arr[2][3][2]={{{5,10},{6,11},{7,12}},
{{20,30},{21,31},{22,32}}};
We Can Consider A Three Dimensional Array To Be
An Array Of 2-D Arrays I.E. Each Element Of A 3-D
Array Is Considerd To Be A 2-D Array.The 3-D Array
Arr Can Be Considerd As An Array Consisting Of
Two Elements Where Each Element Is A 2-D Array.
0th 2-D array 1st 2-D array
5 10 6 11 7 12 20 30 21 31 22 32
Oth element of 1st 1-D array of 2nd 1-D array of 1st2-D array
oth 1-D array of oth 2-D array oth element of
oth 2-D array oth 1-D array of
1st 2-D array
#include<stdio.h>
#include<conio.h> Output:
Void main() { 5 10
6 11
{ for(k=0;k<2,k++)
7 12
Int arr[2][3][2]={ printf(“%d\t”, 20 30
*(*(*(arr+i)+j)+k)); 21 31
{ printf(“\n”); 22 32
{5,10}, }
{6,11}, getch();
{7,12}, }
},
{
{20,30},
{21,31},
{22,32},
}
},i,j,k;

Clrscr();
For(i=0;i<2;i++)
for(j=0;j<3;j++)
We can declare an array that contains pointers as its ARRAY OF POINTER
Elements. Every element of this array is a pointer variable that can hold address of
any variable of appropriate type.
Syntax: data_type*array name[size];
#include<stdio.h>
#include<conio.h>
Void main()
{ int*a1[5],i,a=10,b=20,c=30,d=40,e=50;
a1[0]=&a;
Output:
a1[1]=&b; 15
a1[2]=&c; 65514
a1[3]=&d; 25
65512
a1[4]=&e; 35
For(i=1;i<5;i++) 65510
{ *a1[i]=*a1[i]+5; 45
65508
printf(“%d”,*a1[i]); 55
printf(“%u”,a1[i]); 65506
}
getch();
Pointer to pointer
pointer is a variable that can contain memory address.this pointer
takes some space in memory and hence it also has an address.we can
store the address of a pointer variable in some other variable,which
is known as a pointerOutput:
to pointer variable.
#include<stdio.h> address of i=65522
#include<conio.h> address of i=65522
Void main() address of i=65522
address of j=65524
{
The value of i=3
Int *j,**k,i=3; The value of i=3
Clrscr();
J=&i;
K=&j;
Printf(“address of i =%u”,&i);
Printf(“address of i =%u”, j);
Printf(“address of i =%u”,*k);
Printf(“address of j =%u”, &j);
Printf(“the value of i =%d”,**k);
Printf(“the value of i =%d”,*j);
getch();
}
Pointer and
functions

You might also like