C Program To Find Size of Pointer Variables

Lets write a C program to find out the size or the number of bytes occupied by pointer variables of different data type in your computers memory.

Related Read:
Sizeof Operator in C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Size of Pointer Variables


[youtube https://www.youtube.com/watch?v=_EaAZYSvBog]

YouTube Link: https://www.youtube.com/watch?v=_EaAZYSvBog [Watch the Video In Full Screen.]


Source Code: C Program To Find Size of Pointer Variables

#include<stdio.h>

int main()
{
    printf("Size of int pointer = %d bytes.\n", sizeof(int*));
    printf("Size of char pointer = %d bytes.\n", sizeof(char*));
    printf("Size of float pointer = %d bytes.\n", sizeof(float*));
    printf("Size of double pointer = %d bytes.\n", sizeof(double*));
    printf("Size of long int pointer = %d bytes.\n", sizeof(long*));
    printf("Size of short int pointer = %d bytes.\n", sizeof(short*));

    return 0;
}

Output:
Size of int pointer = 4 bytes.
Size of char pointer = 4 bytes.
Size of float pointer = 4 bytes.
Size of double pointer = 4 bytes.
Size of long int pointer = 4 bytes.
Size of short int pointer = 4 bytes.

If you observe the output of above C program you can see that pointer variables, irrespective of their data type, consume 4 bytes of data in the memory.

Note: “But the number of bytes allocated for different data types and pointer variables are machine dependent. 16-bit, 32-bit, 64-bit computers allocate different bytes of memory. But pointer variables of any data type will always have same number of bytes occupied in the memory. For Example, if the computer allocates 2 bytes for pointer variable, then all the pointer variables, irrespective of their data type, will occupy 2 bytes in the memory.”

A pointer variable of type float holds only the address of floating point variable. A char pointer variable holds only the address of char type variable. But still the address of all these type of pointer variable is number. Addresses are always numbers and it can’t be a character, a string or real/floating/double numbers.

Also note that addresses are unique. There can’t be 2 location in your computers memory with same address.

Note: Observe that I’m using %d as format specifier while printing the size of data type, that’s because sizeof() function/method returns integer type data i.e., number of bytes occupied in the computers memory.

Note: sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Area and Circumference of Circle using Pointer

Lets write a C program to calculate area and circumference or perimeter of a Circle using pointer and function.

Related Read:
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Area and Circumference of Circle using Pointer


[youtube https://www.youtube.com/watch?v=ekmC25v6TqU]

YouTube Link: https://www.youtube.com/watch?v=ekmC25v6TqU [Watch the Video In Full Screen.]


Source Code: C Program To Find Area and Circumference of Circle using Pointer

#include<stdio.h>

void area_peri(float, float*, float*);

int main()
{
    float radius, area, perimeter;

    printf("Enter radius of Circle\n");
    scanf("%f", &radius);

    area_peri(radius, &area, &perimeter);

    printf("\nArea of Circle = %0.2f\n", area);
    printf("Perimeter of Circle = %0.2f\n", perimeter);

    return 0;
}

void area_peri(float r, float *a, float *p)
{
    *a = 3.14 * r * r;
    *p = 2 * 3.14 * r;
}

Output 1:
Enter radius of Circle
5

Area of Circle = 78.50
Perimeter of Circle = 31.40

Output 2:
Enter radius of Circle
14

Area of Circle = 615.44
Perimeter of Circle = 87.92

Logic To Find Area and Circumference of Circle using Pointer

We ask the user to enter value for radius of a Circle. We pass this value along with address of variables area and perimeter to the function area_peri().

area_peri(radius, &area, &perimeter);

We copy the value of radius to a local variable r and then we take 2 floating point pointer variables *a and *p. *a represents the value present at address a or &area. *p has value present at address p or &perimeter.

Inside area_peri() function we calculate the area and circumference / perimeter of Circle and store it as value present at addresses a and p. Since a points to address of variable area and p points to address of variable perimeter, the values of variable area and perimeter changes too.

*a = 3.14 * r * r;

*p = 2 * 3.14 * r;

Area and Circumference of Circle

We’ve separate video tutorials to calculate area and circumference of a Circle using radius, and without using pointer and function. You can check them out at these links:

Calculate Area of a Circle without using math.h library: C
C Program To Calculate Circumference of Circle

Note: When * is precedes any address, it fetches the value present at that address or memory location.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Swap Two Numbers using Function

Lets write a C program to swap 2 numbers using function/method.

When we call a function and pass the actual value it’s called as Call by Value method. If we pass the reference or the address of the variable while calling the function, then it’s called Call by Reference.

In today’s video tutorial we’ll be showing you the concept of Call By Value.

Call by Reference Example: Swapping 2 numbers using pointers

We have written the same C program using pointer and function to illustrate the concept of call by reference. To achieve call by reference we need to use pointers concept. Please watch the video tutorial present at C Program To Swap Two Numbers using Pointers to understand the concept of pointers and call by reference.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language

Video Tutorial: C Program To Swap Two Numbers using Function


[youtube https://www.youtube.com/watch?v=EOySeeYYxr4]

YouTube Link: https://www.youtube.com/watch?v=EOySeeYYxr4 [Watch the Video In Full Screen.]


Source Code: C Program To Swap Two Numbers using Function

#include<stdio.h>

void swap(int, int);

int main()
{
    int a, b;

    printf("Enter values for a and b\n");
    scanf("%d%d", &a, &b);

    printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

    swap(a, b);

    return 0;
}

void swap(int x, int y)
{
    int temp;

    temp = x;
    x    = y;
    y    = temp;

    printf("\nAfter swapping: a = %d and b = %d\n", x, y);
}

Output 1:
Enter values for a and b
20
50

Before swapping: a = 20 and b = 50
After swapping: a = 50 and b = 20

Output 2:
Enter values for a and b
80
90

Before swapping: a = 80 and b = 90
After swapping: a = 90 and b = 80

Logic To Swap Two Numbers

We ask the user to enter values for variable a and b. We pass the user entered values to swap() function. Since we are passing the values to the function, its called Call by value method.

Values of a and b are copied into local variables of swap() that is x and y. We take a local variable temp inside swap() function. We assign the value of x to temp. Then we assign the value of y to x. And finally we assign the value of temp to y. This swaps the values of variable x and y.

Since swap() method doesn’t return anything, we print the values of x and y inside swap() method itself.


temp = x;
x = y;
y = temp;

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Swap Two Numbers using Pointers

Lets write a C program to swap 2 numbers using pointers and function.

When we call the function, we pass the reference or address of the variable, so this method is called “Call by Reference“.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Swap Two Numbers using Pointers


[youtube https://www.youtube.com/watch?v=snxBqbRYrhs]

YouTube Link: https://www.youtube.com/watch?v=snxBqbRYrhs [Watch the Video In Full Screen.]


Source Code: C Program To Swap Two Numbers using Pointers and Function

#include<stdio.h>

void swap(int*, int*);

int main()
{
    int a, b;

    printf("Enter values for a and b\n");
    scanf("%d%d", &a, &b);

    printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

    swap(&a, &b);

    printf("\nAfter swapping: a = %d and b = %d\n", a, b);

    return 0;
}

void swap(int *x, int *y)
{
    int temp;

    temp = *x;
    *x   = *y;
    *y   = temp;
}

Output 1:
Enter values for a and b
100
200

Before swapping: a = 100 and b = 200

After swapping: a = 200 and b = 100

Output 2:
Enter values for a and b
30
20

Before swapping: a = 30 and b = 20

After swapping: a = 20 and b = 30

Logic To Swap Two Numbers using Pointers and Function

We ask the user to enter values for variable a and b. We pass the address of variable a and b to function swap().

Inside function swap() we take a local variable temp. Since address of variable a and b are passed to swap() method, we take 2 pointer variables *x and *y. Pointer variable x holds the address of a and pointer variable y holds the address of b. Using below logic we swap the values present at address a( or x ) and b( or y ).


temp = *x;
*x = *y;
*y = temp;

Since we are directly changing the value present at particular address, the value gets reflected throughout the program and not just inside particular function. That’s why we are able to print the result inside main function and the values of variable a and b are swapped, without getting any returned value from function swap().

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Add Two Numbers using Pointers

Lets write a C program to add 2 numbers using pointer and function.

In this video tutorial we will show both ways of adding 2 numbers using pointers: 1. Using function 2. Without using function.

Related Read:
Basics of Pointers In C Programming Language
Function / Methods In C Programming Language

Video Tutorial: C Program To Add Two Numbers using Pointers


[youtube https://www.youtube.com/watch?v=0wBPwxsr6-U]

YouTube Link: https://www.youtube.com/watch?v=0wBPwxsr6-U [Watch the Video In Full Screen.]


Source Code: C Program To Add Two Numbers using Pointers and Without using Function

#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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert