Pointers
Pointers
Q. Call By Value
Call by value means passing a copy of the actual argument to the function.
When you call a function and pass a variable to it, the value of the variable is copied into
the function's parameter.
The function works with this copy, and any changes made inside the function will not
affect the original variable.
Example:
#include <stdio.h>
include<conio.h>
void modifyValue(int num);
void main()
{
int x = 5;
modifyValue(x);
getch();
}
void modifyValue(int num)
{
num = num + 10;
printf("\n Value of num : %d ", num);
}
Output:
Value of num: 15
Explanation:
The function modifyValue takes the value of x (which is 5) and modifies it inside the
function.
However, the original value of x in main () remains unchanged because the function
works with a copy of x, not the actual variable.
Call by Value passes a copy of the argument to the function.
Changes made inside the function do not affect the original variable.
Q. Call By Reference
Call by reference means passing a address of the actual argument to the function.
When you call a function and pass a address of variable to it, the value of the variable is
copied into the function's parameter.
The function works with original copy of variable, and any changes made inside the
function will affect the original variable.
Example:
#include <stdio.h>
include<conio.h>
void modifyValue(int *ptr);
void main()
{
int x = 5;
modifyValue(&x);
getch();
}
void modifyValue(int *ptr)
{
*ptr=*ptr+10;
printf("\n Value of x : %d ",x);
}
Output:
Value of x: 15
Explanation:
The function modifyValue takes the address of x and modifies it inside the function.
However, the original value of x in main gets changed because the function works with
a original copy of x
Call by reference passes address of the argument to the function.
Changes made inside the function will affect the original variable.
Q. Difference Between Call by value and Call by
reference
Structure
parameter Array
An array is a collection of A Structure is a collection of elements of the
elements of the same name and different name and different datatype
Definition
same datatype datatype
The size of an array is fixed and The size of a structure is determined by the
Size
determined at the time of sum of the sizes of its members.
declaration.
Structure members are accessed using the dot
Accessing Array elements are accessed using
(.) operator (e.g., tructVar.member)
Elements an index (e.g., arr[0]).
struct Person
{
Example int arr[3];; int age;
char name[20];
}s;
Advantages of Pointers:
Disadvantages of Pointers:
1. Complexity
2. Memory Leaks
3. Pointer Errors (Null/Invalid pointers)
4. Security Risks (Buffer overflow)
5. Difficult Debugging
Example:
int x = 10;
int *ptr = &x; // &x gives the address of variable x
2. * (Dereference Operator):
o It is used to access the value stored at the memory address
o It is used to get the value at the address stored in the pointer.
Example:
int x = 10;
int *ptr = &x; // ptr holds the address of x
int value = *ptr; // *ptr gives the value stored at the address (which is 10)