Structured Programming using C
Unit VI
Pointer
Contents
Pointers
6.1 Introduction, Definition and uses of Pointers, Address Operator, Pointer Variables
6.2 Pointer Arithmetic
6.3 Call by value, call by Reference
R.C.Patel Information of Technology,Shirpur
Introduction
• The pointer in C language is a variable which stores the address of another
variable.
• The size of the pointer depends on the architecture. (32-bit architecture the
size of a pointer is 2 byte).
• Every variable stored at a specific memory location and every memory
location has its address.
• The address can be accessed using ampersand (&) operator.
R.C.Patel Information of Technology,Shirpur
Introduction
#include <stdio.h>
int main() {
int var1;
char var2[10];
printf("Address of var1 variable: %xn",&var1); // Print the memory address of the integer variable var1
printf("Address of var2 variable: %xn",&var2); // Print the memory address of the character array var2
return 0;
}
the format specifier %x is used to display hexadecimal integers in lowercase(unsigned integer)
Whenever a variable is declared in a program, system allocates a location i.e an address to
that variable in the memory, to hold the assigned value.
This location has its own address number.
Let us assume that system has allocated memory location 80F for a variable a.
We can access the value 10 either by using the variable name a or by using its address 80F.
Concept of Pointer
Advantages of Pointers
• Pointers in C programming are helpful to access a memory location
• Pointers are an effective way to access the array structure elements
• Pointers are used for the allocation of dynamic memory and the distribution
• Pointers are used to build complicated data structures like a linked list, graph, tree, etc
R.C.Patel Information of Technology,Shirpur
Usage of Pointers
• For passing the argument by using references.
• For accessing the elements of an array.
• For dynamic memory allocation by using malloc() and calloc() functions .
• Used in arrays, functions to improve the performance of code.
• Implementing a data structure.
• For doing system-level programming.
R.C.Patel Information of Technology,Shirpur
Null Pointer Example
#include <stdio.h>
int main() {
int *p = NULL; // Initialize the pointer as NULL.
printf("The value of the pointer is: %un", p); // Use %p to display pointer value.
return 0;
}
The format specifier %u is used to print unsigned integer values
R.C.Patel Information of Technology,Shirpur
#include<stdio.h>
int main(){
int number=50; // Declare an integer variable 'number' and initialize it with the value 50
int *p; // Declare a pointer variable 'p' of type int
p=&number; //stores the address of number variable
printf("Address of p variable is %x n",p); / Print the address stored in the pointer 'p'
printf("Value of p variable is %d n",*p); // Dereference the pointer 'p' to access the value stored at the
address it points to
return 0;
}
R.C.Patel Information of Technology,Shirpur
Pointer Program to swap two numbers without using the 3rd variable.
#include<stdio.h>
int main(){
int a=10,b=20; // Declare and initialize two integer variables 'a' and 'b'.
int *p1=&a,*p2=&b; // Declare two pointers 'p1' and 'p2' pointing to 'a' and 'b', respectively.
// Print the initial values of 'a' and 'b' using dereferenced pointers.
printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2; // Step 1: Add the values pointed to by 'p1' and 'p2' and store the sum in *p1.
*p2=*p1-*p2; // Step 2: Subtract the new value of *p1 by *p2 to get the original value of *p1,
and store it in *p2.
*p1=*p1-*p2; // Step 3: Subtract the new value of *p2 from *p1 to get the original value of *p2,
and store it in *p1.
printf("nAfter swap: *p1=%d *p2=%d",*p1,*p2);
// Print the swapped values of 'a' and 'b' using dereferenced pointers
return 0;
}
Output
Before swap:
*p1=10 *p2=20
After swap:
*p1=20 *p2=10
R.C.Patel Information of Technology,Shirpur
Pointer Arithmetic in C
We can perform arithmetic operations on the pointers like addition, subtraction, etc.
Following arithmetic operations are possible on the pointer in C language:
• Increment
• Decrement
• Addition
• Subtraction
• Comparison
R.C.Patel Information of Technology,Shirpur
Example of incrementing pointer variable on 64-bit architecture.
#include<stdio.h>
int main(){
int number=50;
int *p; // Declare a pointer variable 'p' of type int
p=&number; // Assign the address of the variable 'number' to the pointer 'p'
printf("Address of p variable is %u n",p); // Print the address stored in the pointer 'p' (the address of 'number')
p=p+1; // Increment the pointer 'p' by 1. This moves the pointer to the next memory location based on the size of the data type (int in this
case, which is typically 4 bytes)
// Print the updated address stored in 'p' after incrementing
printf("After increment: Address of p variable is %u n",p); // in our case, p will get incremented by 4 bytes.
return 0; // %u format specifier is used to print unsigned decimal integers
}
R.C.Patel Information of Technology,Shirpur
Traversing an array by using pointer
#include<stdio.h>
void main ()
{
int arr[5] = {1, 2, 3, 4, 5}; // Declare and initialize an integer array 'arr' with 5 elements
int *p = arr; // Declare a pointer 'p' and initialize it with the base address of the array 'arr'
int i; // Declare a loop variable 'i’
// Print a message indicating that the array elements will be printed
printf("printing array elements...n");
for(i = 0; i< 5; i++) // Loop through each element of the array
{// Use pointer arithmetic to access the array elements and print their values
printf("%d ",*(p+i));
}
}
Pointer to Array
// Comparing both the strings using pointers
int stringcompare(char *a,char *b)
{
int flag=0; // Loop through both strings until
one of them reaches the null character ('0')
while(*a!='0' && *b!='0') // while loop
{
if(*a!=*b) // Compare the characters at the
current position
{
flag=1; // Set the flag to 1 if the characters
are not equal
}
a++; // Move the pointer `a` to the next
character
b++; // Move the pointer `b` to the next
character
}
if(flag==0) // If the flag is still 0, the strings are
equal; otherwise, they are not
return 0;
else
return 1;
}
#include <stdio.h> //accepts two arguments pointer to a character
int stringcompare(char*,char*); // Function to compare two strings using pointers
int main()
{
char str1[20]; // Declaration of a character array to store the first string
char str2[20]; // Declaration of a character array to store the second string
printf("Enter the first string : ");
scanf("%s",str1);
printf("nEnter the second string : ");
scanf("%s",str2);
int compare=stringcompare(str1,str2); // Call the `stringcompare()`
function to compare the two strings if(compare==0)
printf("strings are equal");
else
printf("strings are not equal");
return 0;
}
Call by Value
#include <stdio.h>
// Function prototype for call-by-value
void swap(int a, int b); //takes two arguments
int main()
{
int a = 10; // Initialize variable a
int b = 20; // Initialize variable b
// Display initial values of a and b in main
printf("nBefore swapping the values in mainn a = %d, b = %d", a, b);
// Call swap function (call-by-value)
swap(a, b);
// Display values of a and b in main after swap (no change expected)
printf("nAfter swapping values in mainn a = %d, b = %dn", a, b);
return 0; // Exit the program
}
// Function to swap two numbers using call-by-value
void swap(int a, int b)
{
printf("nInside the swap function (Call By Value)");
int temp; // Temporary variable to hold one value during swap
temp = a;
a = b;
b = temp;
// Display swapped values inside the function
printf("nAfter swapping in functionn a = %d, b = %dn", a, b);
// Changes will not affect the variables in the main function
}
R.C.Patel Information of Technology,Shirpur
Call by Reference // Function to swap two numbers using call-by-reference
void swap1(int *a, int *b)
{
printf("n Swapping inside function using Call By Reference");
int temp; // Temporary variable to hold one value during swap
temp = *a; // Dereference pointer a to get its value
*a = *b; // Dereference pointer b to assign its value to a
*b = temp; // Assign temp value (initial value of a) to b
// Display swapped values inside the function
printf("n After swapping in function: n a = %d, b = %d", *a,
*b);
}
#include <stdio.h>
// Function prototype for call-by-reference
void swap1(int *, int *); function accepts pointers to integers as its
arguments.
int main()
{
int a = 10; // Initialize variable a
int b = 20; // Initialize variable b
// Display initial values of a and b in main
printf("n Before swapping in main: n a = %d, b = %d", a, b);
// Call swap1 function (call-by-reference)
swap1(&a, &b);
// Display values of a and b in main after swap
printf("n After swapping in main: n a = %d, b = %d", a, b);
return 0; // Indicate successful program termination
}
R.C.Patel Information of Technology,Shirpur
Call by value and call by Reference
#include <stdio.h>
// Function prototypes
void swap(int, int); // Function prototype for call-by-value
void swap1(int *, int *); // Function prototype for call-by-reference
int main()
{
int a = 10; // Initialize variable a
int b = 20; // Initialize variable b
// Display initial values of a and b in main
printf("n Before swapping the values in main n a = %d, b = %d", a, b);
// Call swap function (call-by-value)
swap(a, b);
// Display values of a and b in main after swap (no change expected)
printf("n After swapping values in main n a = %d, b = %d", a, b);
printf("n *********************************************** ");
// Display values of a and b before calling swap1
printf("n Before swapping the values in main n a = %d, b = %d", a, b);
R.C.Patel Information of Technology,Shirpur
// Call swap1 function (call-by-reference)
swap1(&a, &b);
// Display values of a and b in main after swap1 (change expected)
printf("n After swapping values in main n a = %d, b = %d", a, b);
printf("n *********************************************** ");
return 0;
}
// Function to swap two numbers using call-by-value
void swap(int a, int b)
{
printf("n Output Using Call By Value");
int temp; // Temporary variable to hold one value during swap
temp = a;
a = b;
b = temp;
// Display swapped values inside the function
printf("n After swapping values in function n a = %d, b = %d", a, b);
// Here, the changes will not affect the actual variables in main
}
// Function to swap two numbers using call-by-reference
void swap1(int *a, int *b)
{
printf("n Output Using Call By Reference");
int temp; // Temporary variable to hold one value during swap
temp = *a;
*a = *b;
*b = temp;
// Display swapped values inside the function
printf("n After swapping values in function n a = %d, b = %d", *a,
*b);
// Here, the changes will reflect in the actual variables in main
}
R.C.Patel Information of Technology,Shirpur

UNIT 6structureofcprogrammingforppt.pptx

  • 1.
  • 2.
    Contents Pointers 6.1 Introduction, Definitionand uses of Pointers, Address Operator, Pointer Variables 6.2 Pointer Arithmetic 6.3 Call by value, call by Reference
  • 3.
    R.C.Patel Information ofTechnology,Shirpur
  • 4.
    Introduction • The pointerin C language is a variable which stores the address of another variable. • The size of the pointer depends on the architecture. (32-bit architecture the size of a pointer is 2 byte). • Every variable stored at a specific memory location and every memory location has its address. • The address can be accessed using ampersand (&) operator. R.C.Patel Information of Technology,Shirpur
  • 5.
    Introduction #include <stdio.h> int main(){ int var1; char var2[10]; printf("Address of var1 variable: %xn",&var1); // Print the memory address of the integer variable var1 printf("Address of var2 variable: %xn",&var2); // Print the memory address of the character array var2 return 0; } the format specifier %x is used to display hexadecimal integers in lowercase(unsigned integer)
  • 6.
    Whenever a variableis declared in a program, system allocates a location i.e an address to that variable in the memory, to hold the assigned value. This location has its own address number. Let us assume that system has allocated memory location 80F for a variable a. We can access the value 10 either by using the variable name a or by using its address 80F. Concept of Pointer
  • 7.
    Advantages of Pointers •Pointers in C programming are helpful to access a memory location • Pointers are an effective way to access the array structure elements • Pointers are used for the allocation of dynamic memory and the distribution • Pointers are used to build complicated data structures like a linked list, graph, tree, etc
  • 8.
    R.C.Patel Information ofTechnology,Shirpur Usage of Pointers • For passing the argument by using references. • For accessing the elements of an array. • For dynamic memory allocation by using malloc() and calloc() functions . • Used in arrays, functions to improve the performance of code. • Implementing a data structure. • For doing system-level programming.
  • 12.
    R.C.Patel Information ofTechnology,Shirpur Null Pointer Example #include <stdio.h> int main() { int *p = NULL; // Initialize the pointer as NULL. printf("The value of the pointer is: %un", p); // Use %p to display pointer value. return 0; } The format specifier %u is used to print unsigned integer values
  • 14.
    R.C.Patel Information ofTechnology,Shirpur #include<stdio.h> int main(){ int number=50; // Declare an integer variable 'number' and initialize it with the value 50 int *p; // Declare a pointer variable 'p' of type int p=&number; //stores the address of number variable printf("Address of p variable is %x n",p); / Print the address stored in the pointer 'p' printf("Value of p variable is %d n",*p); // Dereference the pointer 'p' to access the value stored at the address it points to return 0; }
  • 16.
    R.C.Patel Information ofTechnology,Shirpur Pointer Program to swap two numbers without using the 3rd variable. #include<stdio.h> int main(){ int a=10,b=20; // Declare and initialize two integer variables 'a' and 'b'. int *p1=&a,*p2=&b; // Declare two pointers 'p1' and 'p2' pointing to 'a' and 'b', respectively. // Print the initial values of 'a' and 'b' using dereferenced pointers. printf("Before swap: *p1=%d *p2=%d",*p1,*p2); *p1=*p1+*p2; // Step 1: Add the values pointed to by 'p1' and 'p2' and store the sum in *p1. *p2=*p1-*p2; // Step 2: Subtract the new value of *p1 by *p2 to get the original value of *p1, and store it in *p2. *p1=*p1-*p2; // Step 3: Subtract the new value of *p2 from *p1 to get the original value of *p2, and store it in *p1. printf("nAfter swap: *p1=%d *p2=%d",*p1,*p2); // Print the swapped values of 'a' and 'b' using dereferenced pointers return 0; } Output Before swap: *p1=10 *p2=20 After swap: *p1=20 *p2=10
  • 17.
    R.C.Patel Information ofTechnology,Shirpur Pointer Arithmetic in C We can perform arithmetic operations on the pointers like addition, subtraction, etc. Following arithmetic operations are possible on the pointer in C language: • Increment • Decrement • Addition • Subtraction • Comparison
  • 18.
    R.C.Patel Information ofTechnology,Shirpur Example of incrementing pointer variable on 64-bit architecture. #include<stdio.h> int main(){ int number=50; int *p; // Declare a pointer variable 'p' of type int p=&number; // Assign the address of the variable 'number' to the pointer 'p' printf("Address of p variable is %u n",p); // Print the address stored in the pointer 'p' (the address of 'number') p=p+1; // Increment the pointer 'p' by 1. This moves the pointer to the next memory location based on the size of the data type (int in this case, which is typically 4 bytes) // Print the updated address stored in 'p' after incrementing printf("After increment: Address of p variable is %u n",p); // in our case, p will get incremented by 4 bytes. return 0; // %u format specifier is used to print unsigned decimal integers }
  • 19.
    R.C.Patel Information ofTechnology,Shirpur Traversing an array by using pointer #include<stdio.h> void main () { int arr[5] = {1, 2, 3, 4, 5}; // Declare and initialize an integer array 'arr' with 5 elements int *p = arr; // Declare a pointer 'p' and initialize it with the base address of the array 'arr' int i; // Declare a loop variable 'i’ // Print a message indicating that the array elements will be printed printf("printing array elements...n"); for(i = 0; i< 5; i++) // Loop through each element of the array {// Use pointer arithmetic to access the array elements and print their values printf("%d ",*(p+i)); } }
  • 20.
    Pointer to Array //Comparing both the strings using pointers int stringcompare(char *a,char *b) { int flag=0; // Loop through both strings until one of them reaches the null character ('0') while(*a!='0' && *b!='0') // while loop { if(*a!=*b) // Compare the characters at the current position { flag=1; // Set the flag to 1 if the characters are not equal } a++; // Move the pointer `a` to the next character b++; // Move the pointer `b` to the next character } if(flag==0) // If the flag is still 0, the strings are equal; otherwise, they are not return 0; else return 1; } #include <stdio.h> //accepts two arguments pointer to a character int stringcompare(char*,char*); // Function to compare two strings using pointers int main() { char str1[20]; // Declaration of a character array to store the first string char str2[20]; // Declaration of a character array to store the second string printf("Enter the first string : "); scanf("%s",str1); printf("nEnter the second string : "); scanf("%s",str2); int compare=stringcompare(str1,str2); // Call the `stringcompare()` function to compare the two strings if(compare==0) printf("strings are equal"); else printf("strings are not equal"); return 0; }
  • 21.
    Call by Value #include<stdio.h> // Function prototype for call-by-value void swap(int a, int b); //takes two arguments int main() { int a = 10; // Initialize variable a int b = 20; // Initialize variable b // Display initial values of a and b in main printf("nBefore swapping the values in mainn a = %d, b = %d", a, b); // Call swap function (call-by-value) swap(a, b); // Display values of a and b in main after swap (no change expected) printf("nAfter swapping values in mainn a = %d, b = %dn", a, b); return 0; // Exit the program } // Function to swap two numbers using call-by-value void swap(int a, int b) { printf("nInside the swap function (Call By Value)"); int temp; // Temporary variable to hold one value during swap temp = a; a = b; b = temp; // Display swapped values inside the function printf("nAfter swapping in functionn a = %d, b = %dn", a, b); // Changes will not affect the variables in the main function }
  • 22.
    R.C.Patel Information ofTechnology,Shirpur Call by Reference // Function to swap two numbers using call-by-reference void swap1(int *a, int *b) { printf("n Swapping inside function using Call By Reference"); int temp; // Temporary variable to hold one value during swap temp = *a; // Dereference pointer a to get its value *a = *b; // Dereference pointer b to assign its value to a *b = temp; // Assign temp value (initial value of a) to b // Display swapped values inside the function printf("n After swapping in function: n a = %d, b = %d", *a, *b); } #include <stdio.h> // Function prototype for call-by-reference void swap1(int *, int *); function accepts pointers to integers as its arguments. int main() { int a = 10; // Initialize variable a int b = 20; // Initialize variable b // Display initial values of a and b in main printf("n Before swapping in main: n a = %d, b = %d", a, b); // Call swap1 function (call-by-reference) swap1(&a, &b); // Display values of a and b in main after swap printf("n After swapping in main: n a = %d, b = %d", a, b); return 0; // Indicate successful program termination }
  • 23.
    R.C.Patel Information ofTechnology,Shirpur Call by value and call by Reference #include <stdio.h> // Function prototypes void swap(int, int); // Function prototype for call-by-value void swap1(int *, int *); // Function prototype for call-by-reference int main() { int a = 10; // Initialize variable a int b = 20; // Initialize variable b // Display initial values of a and b in main printf("n Before swapping the values in main n a = %d, b = %d", a, b); // Call swap function (call-by-value) swap(a, b); // Display values of a and b in main after swap (no change expected) printf("n After swapping values in main n a = %d, b = %d", a, b); printf("n *********************************************** "); // Display values of a and b before calling swap1 printf("n Before swapping the values in main n a = %d, b = %d", a, b);
  • 24.
    R.C.Patel Information ofTechnology,Shirpur // Call swap1 function (call-by-reference) swap1(&a, &b); // Display values of a and b in main after swap1 (change expected) printf("n After swapping values in main n a = %d, b = %d", a, b); printf("n *********************************************** "); return 0; } // Function to swap two numbers using call-by-value void swap(int a, int b) { printf("n Output Using Call By Value"); int temp; // Temporary variable to hold one value during swap temp = a; a = b; b = temp; // Display swapped values inside the function printf("n After swapping values in function n a = %d, b = %d", a, b); // Here, the changes will not affect the actual variables in main } // Function to swap two numbers using call-by-reference void swap1(int *a, int *b) { printf("n Output Using Call By Reference"); int temp; // Temporary variable to hold one value during swap temp = *a; *a = *b; *b = temp; // Display swapped values inside the function printf("n After swapping values in function n a = %d, b = %d", *a, *b); // Here, the changes will reflect in the actual variables in main }
  • 25.
    R.C.Patel Information ofTechnology,Shirpur