C Language: Pointers, Numeric Arrays and Functions

Pointer arithmetic

Whenever we perform any increment or decrement with pointer variables, the increment and decrement is performed with respect to the size of the data type of the pointer variable.

P=100

DataType Size p++ p– p=p+2 p=p-2
int(Turboc3) 2 102 98 104 96
int(Codeblock) 4 104 96 108 92
float 4 104 96 108 92
double 8 108 92 116 84
char 1 101 99 102 98

Pointers, Numeric Arrays and Functions

Here we will learn how to pass element by element and the entire numeric array to a function and related programs.

Question:1
C Program to take input for “10”/”N” elements using an array, pass the entire array to a function, and display the array elements.
Sol:

#include <stdio.h> 
void show(int *p,int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("%d\n",*p);
		p++;
	}
}
int main()
{
    int a[10],i;
    /* input  */
    for(i=0;i<10;i++)
    {
    	printf("Enter %d element ",i);
    	scanf("%d",&a[i]);
	}
	/* function calling */
	show(a,10);
	/* or */
	/* show(&a[0],10); */
	
    return(0);
}
/* Output */
Enter 0 element 10
Enter 1 element 20
Enter 2 element 30
Enter 3 element 40
Enter 4 element 50
Enter 5 element 60
Enter 6 element 70
Enter 7 element 80
Enter 8 element 90
Enter 9 element 100
10
20
30
40
50
60
70
80
90
100
#include <stdio.h> 
void show(int *p,int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("%d\n",*p);
		p++;
	}
}
int main()
{
    int a[20],i,n;
    printf("Enter total elements ");
    scanf("%d",&n);
    /* input  */
    for(i=0;i<n;i++)
    {
    	printf("Enter %d element ",i);
    	scanf("%d",&a[i]);
	}
	/* function calling */
	show(a,n);
	/* or */
	/* show(&a[0],n); */
	
    return(0);
}

Question:2
C Program to take input for “N” elements using an array, pass the entire array to a function, display the array elements, and also calculate and print the sum of all the elements.
Sol:

#include <stdio.h> 
void show(int *p,int n)
{
	int i,s=0;
	for(i=0;i<n;i++)
	{
		printf("%d\n",*p);
		s=s+*p;
		p++;
	}
	printf("Sum = %d\n",s);
}
int main()
{
    int a[20],i,n;
    printf("Enter total elements ");
    scanf("%d",&n);
    /* input  */
    for(i=0;i<n;i++)
    {
    	printf("Enter %d element ",i);
    	scanf("%d",&a[i]);
	}
	/* function calling */
	show(a,n);
	/* or */
	/* show(&a[0],n); */
	
    return(0);
}
/* Output */
Enter total elements 5
Enter 0 element 10
Enter 1 element 20
Enter 2 element 30
Enter 3 element 40
Enter 4 element 50
10
20
30
40
50
Sum = 150

Question:3
C Program to take input for “N” elements using an array, pass the entire array to a function, display the array elements, and also calculate and print product of all the elements.
Sol:

#include <stdio.h> 
void show(int *p,int n)
{
	int i,pr=1;
	for(i=0;i<n;i++)
	{
		printf("%d\n",*p);
		pr=pr* *p;
		p++;
	}
	printf("Product = %d\n",pr);
}
int main()
{
    int a[20],i,n;
    printf("Enter total elements ");
    scanf("%d",&n);
    /* input  */
    for(i=0;i<n;i++)
    {
    	printf("Enter %d element ",i);
    	scanf("%d",&a[i]);
	}
	/* function calling */
	show(a,n);
	/* or */
	/* show(&a[0],n); */
	
    return(0);
}
/* Output */
Enter total elements 5
Enter 0 element 1
Enter 1 element 2
Enter 2 element 3
Enter 3 element 4
Enter 4 element 5
1
2
3
4
5
Product = 120