C - Pointers Concepts
C - Pointers Concepts
PART II
INDEX
1. Pointers and Arrays
1.1.
1.2.
1.3.
1.4.
By
1) Mr. Shaik Masthan Valli
Asst.,Prof..
Dept of CSE
GITAM University
HTP Campus.
2) Mr P.Chakradhar
Asst.,Prof..
Dept of CSE
GITAM University
HTP Campus.
After having this declaration, the compiler creates an array with name num, the elements are stored in
contiguous memory locations, and each element occupies two bytes or four bytes (depends on compiler), since
it is an integer array. The name of the array num gets the base address. Thus by writing *num we would be able
to refer to the 0th element of the array, that is 1. Then *num and *(num+0) both refer to the element 1.and
*(num+2) will refer 3. When we have num[i] , the compiler internally converts it to *(num+i). In this light the
following notations are same.
num [0]
1
1000
1002
num [1]
num [2]
num [3]
num [4]
elements
values
1004
1006
1008
ptr
Base address
Memory Representation of 1 dimensional array
address
Example 1: Write a c program to access an 1 dimensional integer array values using pointer variable .
#include<stdio.h>
int main( )
{
int num[5]={1,2,3,4,5};
int i, * ptr;
ptr = num;
for(i=0 ; i<5 ; i++ )
{
printf(\t a[%d] = %d , , i , *ptr );
ptr ++ ;
}
return 0;
}
OUTPUT:
a[0] = 1, a[1] = 2, a[3] = 3, a[4] = 4, a[5] = 5;
NOTE: Note that the array name num is a constant pointer points to the base address, then the increment of
its value is illegal, num++ is invalid.
A [0] [1]
A [0] [2]
A [ 1 ] A[1] [0]
A [1] [1]
A [1] [2]
A [ 2 ] A [2] [0]
A [2] [1]
A [2] [2]
A [ 3 ] A [3] [0]
A [3] [1]
A [3] [2]
It is possible to access two dimensional array elements using pointers in the same way as in one dimensional
arrays. Each row of the two dimensional array is treated as a one dimensional array. The name of array points to
the starting address of the array. The expression A [ i ] points to the ith row of the array. Therefore , A[ i ] + j
points to the jth element in the ith row of the array. The subscript j actually acts as an offset to the base address of
the ith row.
Let us consider we have a two-dimensional array of integers. When we dereference the array name, we dont
get one integer, we get an array on integers. In other words the dereference of the array name of a twodimensional array is a pointer to a one-dimensional array. Here we require two indirections to refer the
elements.
----->
----->
----->
----->
----->
10
11
12
Example 2 : Write a c program to access an 2 dimensional integer array values using pointer variable .
This declaration tells the compiler arr_ptr is an array of addresses, pointing to the values of data type. Then
initialization can be done same as array element initialization. Example arr_ptr [3] =&var, will initialize 3rd
element of the array with the address of var.
The dereferencing operator is used as follows
*(arr_ptr [index]) will give the value at particular address.
Look at the following code array of pointers to ordinary Variables
Example 4:
#include<stdio.h>
void main ()
{
int i=1, j=2, k=3, l=4, x;
int *arr [4];
//declaration of array of pointers
arr [0] =&i;
//initializing 0th element with address of i
arr [1] =&j;
//initializing 1st element with address of j
arr [2] =&k;
//initializing 2nd element with address of k
arr [3] =&l;
//initializing 3rd element with address of l
for (x=0; x<4; x++)
printf (\n %d,*(arr[x]));
}
OUTPUT
1
2
3
4
Here, arr contains the addresses of int variables i, j, k and l. The for loop is used to print the
values present at these addresses.
A two-dimensional array can be represented using pointer to an array. But, a two-dimensional array can
be expressed in terms of array of pointers also. Using array of pointers a two dimensional array can be defined
as,
data type *arr_ptr [size];
where data type refers to the data type of the array. arr_ptr refers to the name of the array and size is the
maximum number of elements in the row.
Example int *arr [3];
Here, arr_ptr is an array of pointers, and then following notations are used to refer elements.
arr_ptr[i]
Points the address of the element ith row,
arr_ptr[i] +j Points the address of the element ith row and jth column
*(arr_ptr[i] +j) Palue at ith row and jth column.
#include<stdio.h>
void main ()
{
char s [] =hello;
char *ptr;
ptr=s;
while (*ptr! =\0)
{
printf ( %c,*ptr);
ptr++;
}
}
OUTPUT: hello
Example 6 :
# include <stdio.h>
int main ()
{
char *name [] = { "Illegal month","January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
}
Illegal month \0
name
January \0
February\0
March\0
April\0
May\0
June\0
July\0
August\0
September\0
October\0
November\0
December\0
NOTE: When we are using an array of pointers to strings we can initialize the string at the place where we
are declaring the array, but we cannot receive the string from keyword using scanf ().
Allocates space for an array elements, initializes to zero and then returns a pointer to memory
free( )
Deallocate the previously allocated space
realloc( ) Change the size of previously allocated space
malloc( )
The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of
specified size and return a pointer of type void which can be casted into pointer of any form.
Syntax of malloc( )
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte
size. If the space is insufficient, allocation fails and returns NULL pointer.
calloc( )
The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is
that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of
same size and sets all bytes to zero.
Syntax of calloc( )
This statement will allocate contiguous space in memory for an array of n elements. For example:
ptr=(float*)calloc(25,sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4
bytes.
Differences between malloc() and calloc()
S.no
Malloc
calloc
1
It allocates only single block of requested memory It allocates multiple blocks of requested memory
int *ptr;Ptr = calloc( 20, 20 * sizeof(int) );For the
int *ptr;ptr = malloc( 20 * sizeof(int) );For the
above, 20 blocks of memory will be created and each
2
above, 20*4 bytes of memory only allocated in
contains 20*4 bytes of memory.
one block. Total = 80 bytes
Total = 1600 bytes
malloc () doesnt initializes the allocated memory.
3
calloc () initializes the allocated memory to zero
It contains garbage values
type cast must be done since this function returns
Same as malloc () functionint *ptr;ptr = (int*)calloc(
4
void pointerint *ptr;ptr =
20, 20 * sizeof(int) );
(int*)malloc(sizeof(int)*20 );
free ( )
Dynamically allocated memory with either calloc( ) or malloc( ) does not get return on its own. The
programmer must use free( ) explicitly to release space.
syntax of free( ) free(ptr);
This statement causes the space in memory pointer by ptr to be deallocated.
Example 7:
1. #include <stdio.h>
2. #include <stdlib.h>
3. int main()
4. {
5. int n,i,*ptr,sum=0;
6. printf("Enter number of elements: ");
7. scanf("%d",&n);
8. ptr=(int*)malloc(n*sizeof(int));
9. if(ptr==NULL)
10.
{
11.
printf("Error! memory not allocated.");
12.
exit(0);
13.
}
14. printf("Enter elements of array: ");
15. for(i=0;i<n;++i)
16. {
17.
scanf("%d",ptr+i);
18.
sum+=*(ptr+i);
19.
}
20. printf("Sum=%d",sum);
21. free(ptr);
22 . return 0;
23. }
Analysis:
The above Program finds sum of n elements entered by user. To perform this program, allocate
memory dynamically using malloc( ) function.
In line 8, a size of (n*4) bytes will be allocated using malloc( ) and the address of first byte is
stored in ptr. If the allocation is not successful, malloc returns NULL.
Line numbers 15-20 finds and displays the sum of entered values.
In Line 21, free( ) deallocates the memory allocated by malloc( ).
Example 8:
In the above program if line 8 is replaced by the following statement
ptr = (int*) calloc(n,sizeof(int));
then, calloc() allocates contiguous space in memory for an array of n elements each of size of int, i.e,
4 bytes.
realloc( )
If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size
previously allocated using realloc().
Syntax of realloc( )
ptr = realloc(pt,newsize);
In line 8, a size of (n1*4) bytes will be allocated using calloc() and the address of first byte is
stored in ptr. Line numbers 15-20 finds and displays the sum of entered values.
In Line 14, The allocated memory is modified into(n2*4) bytes using realloc() function.
}
}
When the above code is compiled and executed with a single argument, it produces the following result.
$./a.out testing
The argument supplied is testing
When the above code is compiled and executed with a two arguments, it produces the following result.
$./a.out testing1 testing2
Too many arguments supplied.
When the above code is compiled and executed without passing any argument, it produces the following result.
$./a.out
One argument expected
It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first
command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be
one, otherwise and if you pass one argument then argc is set at 2.
You pass all the command line arguments separated by a space, but if argument itself has a space then you can
pass such arguments by putting them inside double quotes "" or single quotes ''. Let us re-write above example
once again where we will print program name and we also pass a command line argument by putting inside
double quotes:
#include <stdio.h>
int main( int argc, char *argv[] )
{
printf("Program name %s\n", argv[0]);
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}
When the above code is compiled and executed with a single argument separated by space but inside double
quotes, it produces the following result.
$./a.out "testing1 testing2"
{
case 'f':
printf("%s\n",&argv[1][2]);
break;
case 'd':
printf("%s\n",&argv[1][2]);
printf("%s\n",&argv[1][2]);
break;
default:
printf("Wrong Argument: %s\n", argv[1]);
usage();
}
++argv;
--argc;
}
return (0);
}
After compiling you can run the program with the following parameters:
# program
# program fhello
# program dbye
# program fhello dbye
# program w
Note: that there is no space between the flag and the flag argument.
First the program name is printed (it will always be printed.)
The while loop looks for the dash sign. In case of the f the flag argument is printed once. If the flag is d
then the flag argument is printed twice.
The argv[ ][ ] array will be used as follows:
For example: f<name>
1. argv[1][0] contains the dash sign
2. argv[1][1] contains the f
3. argv[1][2] contains the flag argument name
If a wrong flag sign is given then the usage is printed onto the screen.
5. REVIEW QUESTIONS
Short Answer Questions:
1.
2.
3.
4.
5.
6.
7.
8.
THE - END