You are on page 1of 68

Unit-3

Arrays and Pointers


Dereference of Array Name
Pointer Arrays

• int a[10];//array of 10 integers


• float x[10];//array of 10 real numbers
• char ch[10];// array of 10 characters
 Array of pointers[Pointer arrays]- nothing but collection of
addresses. Addresses of the individual variables are stored into
an array or address of the array elements.
 Syntax
Data_type *array_name[size];
Example
int *arr[10];// array of 10 integer pointers
float *x[10];// array of 10 real number pointers

57
Pointer Arrays
int main( )
{
int *arr[4] ; /* array of integer pointers */
int i = 31, j = 5, k = 19, l = 71, m ;
arr[0] = &i ;
arr[1] = &j ;
arr[2] = &k ;
arr[3] = &l ;
for ( m = 0 ; m <= 3 ; m++ )
printf ( "%d ", * ( arr[m] ) ) ;
return 0;
}

58
See the differences below:

• int a[10]; //it is an array of 10 integers


• int *a[10]; // it is an array of 10 integer pointers
• int (*a)[10]; // it is a pointer to an array of 10 integers.
Command Line arguments
Syntax:
int main(int argc, char *argv[])
• It is possible to pass some values from the command line to {
your C programs when they are executed. These values are ……
called command line arguments. }
or
int main(int argc, char **argv)
• Formal Arguments of main() function that include {
• int argc -argc stores the number of arguments including ……
the name of program. }
• char **argv - It is a character array of pointers which
stores argument values as strings.
Note: argv[0] is the name of the program , After that till
argv[argc-1] every element is command -line arguments. At the output:
./a.out String1 String2

60
Command Line arguments-Example
// C program to illustrate command line arguments
#include<stdio.h>
int main(int argc,char* argv[])
{
printf("Program Name Is: %s",argv[0]);
if(argc==1)
printf("\nNo Extra Command Line Argument Passed Other Than
Program Name");
if(argc>=2)
Output:
{
./a.out String1 String2
printf("\nNumber Of Arguments Passed: %d",argc);
Program Name is a.out
printf("\n----Next two Command Line Arguments:");
Number of arguments passed:3
printf("\nargv[%d]: %s",1,argv[1],2, argv[2]);
Next two command line arguments:
}
argv[1]:String1
return 0;
argv[2]:String2
}

61
Command Line arguments-Example
// C program to illustrate command line arguments
#include<stdio.h>
int main( int argc, char *argv[] ) {

if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
Output:
}
./a.out Firstfile
else {
The argument supplied is Firstfile
printf("One argument expected.\n");
}
}

62
• Pointers to Functions

• In C, like normal data pointers (int *, char *, etc), we can have pointers to functions.
• Syntax: return_type *function_pointer(int)=functionname;
• Example: int *addp(int,int)=add;

• Unlike normal pointers,


• a function pointer points to code, not data. Typically a function pointer stores the start of
executable code.
• we do not allocate or de-allocate memory using function pointers.
• A function’s name can also be used to get functions’ address.

https://www.geeksforgeeks.org/function-pointer-in-c/

63
Pointers to Functions: Example

#include<stdio.h>
void add(int a, int b)
{
printf("Sum:%d",a+b);
} Output:
int main() Sum:23
{
void (*fun_ptr)(int,int)=add;
fun_ptr(12,11);
return 0;
}

64
Pointers to Functions: Example
#include<stdio.h>
int add(int a, int b)
{

return a+b;
}
int sub(int a, int b)
{

return a-b; Output:


}
Sum:23
int main()
{ Difference:1
int (*fptr)(int,int)=add;
int sum=fptr(12,11);
printf("\nSum:%d",sum);
int (*fptr2)(int,int)=sub;
printf("\nDifference:%d",fptr2(12,11));
return 0;
}

65
Complicated Declarations of pointers
 int *p; /*p is a pointer to integer*/

 int *p[10]; /*p is an array of 10 pointers to integers*/

 int (*p)[10]; /*p is a pointer to 10-element integer array*/

 int p(char *a); //p is a function that returns integer

 int *p(char *a); //p is a function that returns integer pointer

 int (*p(char *a))[10]; /*p is a function that returns a pointer to 10-element integer array*/
 int (*p[10])(void); /*p is a 10-element array of pointers to functions, each function returns integer*/
Find the output:
#include<stdio.h>
int main()
{
static int x[8]={10,20,30,40,50,60,70,80};
printf("%p\n",x);
printf("%p\n",x+2);
printf("%d\n",*x);
printf("%d\n",(*x+2));
printf("%d\n",*(x+2));
return 0;
Output:
}
0x558398201020
0x558398201028
10
12
30

67
Find the output:
#include<stdio.h>
int *add(int x, int y)
{
int *p;
int z;
z=x+y;
p=&z;
return p;
}
int main()
Output:
{
9
int *sum,x=4,y=5;
sum=add(x,y);
printf("%d\n",*sum);
return 0;
}

68

You might also like