printf("\nElements of array in reverse order ...\n");
for(i = 0; i < N; i++)
printf("%d\n", *ptr--);
return 0;
}
#include<stdio.h>
#define N 5
int main()
{
int a[N], i, *ptr;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
ptr = &a[N - 1];
printf("\nElements of array in reverse order ...\n");
for(i = 0; i < N; i++)
printf("%d\n", *ptr--);
return 0;
}
Output 1: Enter 5 integer numbers 1 2 3 4 5
Elements of array in reverse order … 5 4 3 2 1 Output 2: Enter 5 integer numbers 10 9 8 7 6
Elements of array in reverse order … 6 7 8 9 10
Logic To Display Elements of Array In Reverse Order using Pointers
We ask the user to input N integer numbers and store it inside array variable a[N]. We assign address of (N – 1)th element(last element of any array) to pointer variable ptr.
Inside for loop We iterate through the for loop and for each iteration we print the value present at the address ptr. And for each iteration we decrement the value of ptr by 1.
This prints the array elements in reverse order.
Important Things To Note
1. Array elements are always stored in contiguous memory location. 2. A pointer when incremented always points to an immediately next location of its own type.
Arrays is one of the most important topics in C programming language. In this video tutorial I’ll give you a brief introduction to Arrays.
Declaring Normal/regular Variable
Syntax:
Data_type variable_name;
Ex: int a;
Declaring Array Variable
Syntax:
Data_type variable_name[array_size];
Ex: int a[5];
Here array variable is a, it can hold upto 5 integer values.
Definition of Array
An array is a collection of data items, of same data type, accessed using a common name.
Important Notes About Arrays In C
1. All the elements inside an array MUST be of same data type. 2. If you try to enter more elements than the size allocated to the array, it’ll start throwing error. 3. If you input less number of elements than the size of array, then remaining memory blocks will be filled with zeros. 4. Array variable name(without index) holds the base address or the address of first element of the array. 5. Previous address plus the size of the data type of the array gives the address of next element in the array.
#include<stdio.h>
int main()
{
int a[5] = { 4, 5, 1, 9, 2 }, i;
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 1 9 2
Since a[5] is of type integer, all the array elements must be integers too. we must enclose all the elements inside curly braces and each element must be separated by a comma.
#include<stdio.h>
int main()
{
int a[5] = { 4, 5, 1, 9, 2, 6 }, i;
printf("Array elements are:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: warning: excess elements in array initializer.
In above source code we are trying to insert 6 integer values inside a[5], which can hold only 5 integer numbers. Hence compiler throws error and stops further compilation.
#include<stdio.h>
int main()
{
int a[] = { 4, 5, 2, 6, 1, 2, 4, 5 }, i;
printf("Array elements are:\n");
for(i = 0; i < 8; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 2 6 1 2 4 5
Here we’re not specifying the size of array variable a. Compiler dynamically allocates size to it based on the number of integer numbers assigned to it.
Another method of assigning values to array variable
#include<stdio.h>
int main()
{
int a[3], i;
a[0] = 4;
a[1] = 5;
a[2] = 9;
printf("Array elements are:\n");
for(i = 0; i < 3; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
Output: Array elements are: 4 5 9
We could use the index and insert the value at specified position inside an array.
Note: Indexing starts from 0 in C programming language. For example, if you have an array a[5], then the elements are accessed one by one like this: a[0], a[1], a[2], a[3], a[4].
Here variable a will have base address or the address of first array element. In above program we’re printing the value of a and also the address where the first element of the array is stored. Both display the same address, meaning: a has base address or the address of first element in the array.
If you observe above addresses, there is a difference of 4 between each address. That’s because each memory cell stores integer type data(in above program), which is allocated with 4 bytes of memory(it is machine dependent).
Array of characters is called as string. Observe the output of above program. It has ampersand symbol at the end. To remove this kind of random symbols we need to let the program know the end of a string.
Character type data has 1 byte of allocated memory. Since this array stores characters in each cell, the address of consecutive element is 1 byte apart.
printf("\nGreatest Common Divisor of %d and %d is %d.\n", j, k, gcd);
return 0;
}
void calc_gcd(int numerator, int denominator, int *gcd)
{
int temp, num;
if(denominator == 0)
{
*gcd = numerator;
}
elseif(numerator == 0)
{
*gcd = denominator;
}
else
{
num = numerator / denominator;
temp = numerator - num * denominator;
while(temp)
{
numerator = denominator;
denominator = temp;
num = numerator / denominator;
temp = numerator - num * denominator;
}
*gcd = denominator;
}
}
#include<stdio.h>
void calc_gcd(int, int, int*);
int main()
{
int j, k, gcd;
printf("Enter 2 integer numbers\n");
scanf("%d%d", &j, &k);
calc_gcd(j, k, &gcd);
printf("\nGreatest Common Divisor of %d and %d is %d.\n", j, k, gcd);
return 0;
}
void calc_gcd(int numerator, int denominator, int *gcd)
{
int temp, num;
if(denominator == 0)
{
*gcd = numerator;
}
else if(numerator == 0)
{
*gcd = denominator;
}
else
{
num = numerator / denominator;
temp = numerator - num * denominator;
while(temp)
{
numerator = denominator;
denominator = temp;
num = numerator / denominator;
temp = numerator - num * denominator;
}
*gcd = denominator;
}
}
Output 1: Enter 2 integer numbers 1980 1617
Greatest Common Divisor of 1980 and 1617 is 33.
Output 2: Enter 2 integer numbers 1617 1980
Greatest Common Divisor of 1617 and 1980 is 33.
Output 3: Enter 2 integer numbers 15 20
Greatest Common Divisor of 15 and 20 is 5.
Output 4: Enter 2 integer numbers 20 15
Greatest Common Divisor of 20 and 15 is 5.
Logic To Find GCD using Pointers and Functions, using Euclid’s Algorithm
We ask the user to input integer values for variables j and k. We pass values of j and k and address of variable gcd to a function called calc_gcd().
Inside calc_gcd() function Inside calc_gcd() function we use the following calculations:
Note: We copy the value of j, k and &gcd passed by main method into local variables of calc_gcd() i.e., numerator, denominator and *gcd.
Step 1: We check if denominator is 0. In that case the value present in numerator itself is the GCD. So we copy value present in variable numerator to *gcd.
Step 2: If denominator is not zero. Then, we divide numerator with denominator value and store the result into a variable called num.
num = numerator / denominator;
If j = 1980 and k = 1617, then numerator = 1980 and denominator = 1617.
num = numerator / denominator; num = 1980/ 1617; num = 1;
According to problem statement:
1980 / 1617 = 1
1980 – 1 * 1617 = 363
We’ve formulated the equation for the first part i.e., 1980 / 1617 = 1 Now lets formulate an equation for the second part i.e., 1980 – 1 * 1617 = 363
If you look at 1980 – 1 * 1617 = 363 closely and substitute the values with corresponding variables then you’ll come up with below formula:
temp = numerator – num * denominator;
Look at Step 1 for values of numerator, denominator and num.
Now lets look at the equations given in the problem statement:
1980 / 1617 = 1
1980 – 1 * 1617 = 363
1617 / 363 = 4
1617 – 4 * 363 = 165
In this look at 1617 / 363 = 4. Here the value of denominator has been shifted to numerators place, and the value of temp has been copied to denominator. So lets write the code:
numerator = denominator; denominator = temp;
Step 3: Now lets use these formulate in co-ordination to finish writing our function code.
We need to repeat the code in order to get the results according to the columns present in the problem statement equations:
Here are our formulas:
num = numerator / denominator;
temp = numerator - num * denominator;
numerator = denominator;
denominator = temp;
num = numerator / denominator;
temp = numerator - num * denominator;
numerator = denominator;
denominator = temp;
Here are the steps in problem statement to calculate the GCD:
1980 / 1617 = 1
1980 – 1 * 1617 = 363
1617 / 363 = 4
1617 – 4 * 363 = 165
363 / 165 = 2
363 – 2 * 165 = 33
5 / 33 = 5
165 – 5 * 33 = 0
We need to repeat execution of our code to get above result:
num = numerator / denominator;
temp = numerator - num * denominator;
while(temp)
{
numerator = denominator;
denominator = temp;
num = numerator / denominator;
temp = numerator - num * denominator;
}
*gcd = denominator;
We need to put our code inside a looping construct to repeat the code. Here we are using while loop. So while loop iterates until temp value is positive. Once value of temp is 0, the control exits the while loop.
We have written 2 lines of code before the while loop:
num = numerator / denominator;
temp = numerator - num * denominator;
num = numerator / denominator;
temp = numerator - num * denominator;
that is because we need to have some value inside variable temp before using it as condition for while loop.
Once the value of temp is 0, control exits while loop. Outside while loop we transfer the value present inside denominator to *gcd.
So *gcd will have the Greatest Common Divisor for values input by the user(j = 1980 and k = 1617).
Observe this table from problem statement
1980 / 1617 = 1
1980 – 1 * 1617 = 363
1617 / 363 = 4
1617 – 4 * 363 = 165
363 / 165 = 2
363 – 2 * 165 = 33
5 / 33 = 5
165 – 5 * 33 = 0
When temp value is 0, value of denominator is 33 – which is the Greatest Common Divisor of numbers 1980 and 1617.
#include<stdio.h>
int main()
{
int a, b, c, *ptr1, *ptr2;
ptr1 = &a;
ptr2 = &b;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
c = *ptr1 + *ptr2;
printf("Using *ptr1 + *ptr2 : %d + %d = %d\n", a, b, c);
c = *(&a) + *(&b);
printf("Using *(&a) + *(&b) : %d + %d = %d\n", a, b, c);
return 0;
}
Output: Enter 2 numbers 25 25 Using *ptr1 + *ptr2 : 25 + 25 = 50 Using (&a) + *(&b) : 25 + 25 = 50
Logic To Add Two Numbers using Pointers
Here we are storing the address of variable a in pointer variable ptr1 and address of variable b in pointer variable ptr2.
We know that any address preceded by * would fetch the value present at that address. So we use following code to get the addition of 2 numbers result using pointers.
c = *ptr1 + *ptr2;
Source Code: C Program To Add Two Numbers using Pointer and Function
#include<stdio.h>
void addition(int, int, int*);
int main()
{
int a, b, c;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
addition(a, b, &c);
printf("%d + %d = %d\n", a, b, c);
return 0;
}
void addition(int x, int y, int *z)
{
*z = x + y;
}
Output: Enter 2 numbers 10 40 10 + 40 = 50
Logic To Add Two Numbers using Pointers and Function
We ask the user to enter 2 numbers and store it inside address of variables a and b. Next we pass the values of a and b and address of variable c to a function called addition(). And then print the result in main method itself.
The motive is to change the value present at address of variable c. That way we can print the result directly inside main method itself, without our function addition() returning any value back to calling function(main()).
Inside addition() method, we add the values passed in first 2 arguments and then store the result back in the address location sent to addition() function as third argument. Function addition() doesn’t return any value, so it has void as its return type.
In today’s video tutorial lets learn basics of pointers in C programming language. Think of it as base of a building. If you can hold these basic concepts strong your building will be safer.
Topics Covered
1. What are pointers? 2. How to declare pointers. 3. How to access the value present at the address stored in pointer variable. 4. Using Address of operator. 5. Using Indirection operator.
What are pointers?
A pointer is a variable which refers to or points to an address in your computers memory.
Definition: A pointer variable is a variable which holds the address of another variable.
And since pointer variable is also a variable, it also has a unique address associated with it.
Operators
& is called “address of” operator. * is called Indirection or “value at address” operator.
Video Tutorial: Basics of Pointers In C Programming Language
printf("Access value of num from ptr = %d\n", *ptr);
printf("Access value of num from num = %d\n", *(&num));
return 0;
}
#include<stdio.h>
int main()
{
int num = 5;
int *ptr; // pointer variable
ptr = # // both hold address of num
printf("Value of num = %d\n", num);
printf("Address of num = %d\n", &num);
printf("Address of ptr = %d\n", &ptr);
printf("Value of ptr = %d\n", ptr);
printf("Access value of num from ptr = %d\n", *ptr);
printf("Access value of num from num = %d\n", *(&num));
return 0;
}
Output: Value of num = 5 Address of num = 6356732 Address of ptr = 6356728 Value of ptr = 6356732 Access value of num from ptr = 5 Access value of num from num = 5
Note: Whenever you declare and initialize a variable, for example, int num = 5; Your program does the following tasks: 1. Reserves space in your computers memory depending upon the data type. 2. Associates the name num with this memory location. 3. Stores the number 5 in this memory location.