Professional Documents
Culture Documents
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:
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;
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)
{
65
Complicated Declarations of pointers
int *p; /*p is a pointer to integer*/
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