0% found this document useful (0 votes)
2 views

Pointers

The document explains the concepts of Call by Value and Call by Reference in programming, highlighting their differences in how variables are passed to functions. It also contrasts arrays and structures, detailing their definitions, data types, size, accessing methods, and use cases. Additionally, the document outlines the advantages and disadvantages of pointers, as well as the meanings of the address-of operator (&) and dereference operator (*).

Uploaded by

Dipak Dumbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Pointers

The document explains the concepts of Call by Value and Call by Reference in programming, highlighting their differences in how variables are passed to functions. It also contrasts arrays and structures, detailing their definitions, data types, size, accessing methods, and use cases. Additionally, the document outlines the advantages and disadvantages of pointers, as well as the meanings of the address-of operator (&) and dereference operator (*).

Uploaded by

Dipak Dumbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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

Parameters Call by Value Call by Reference


Passing A copy of the variable is passed to The memory address (reference) of the
Mechanism the function. variable is passed.

Changes inside the function directly affect


Effect on Changes inside the function do not
the original variable.
Original affect the original variable.

Use the address of the variable (&) and


How to Pass Use the variable directly. pass a pointer.

The function receives a reference


Parameter The function receives a copy of the
(address) to the original variable.
Type value (not the variable).
No extra memory is used (just the
Requires extra memory to store the
Memory Usage reference).
copy of the variable.
More efficient for large data, as only the
Can be slower if large data (like
Efficiency address is passed.
large arrays) is passed.
Suitable when you don't want to Suitable when you want to modify the
Use Cases
modify the original data. original data or handle large data.
Q. Difference Between Array and Structure

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

All elements in an array must be of A structure can contain elements of different


Data Type
the same data type. data types.

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]).

Arrays are used when you need to


Structures are used when you need to store
store multiple elements of the
Use Case multiple elements of the different type.
same type.

struct Person
{
Example int arr[3];; int age;
char name[20];
}s;
Advantages of Pointers:

1. Efficient Memory Management


2. Direct Access to Memory
3. Dynamic Data Structures
4. Function Efficiency (Passing by reference)
5. Flexible Memory Usage

Disadvantages of Pointers:

1. Complexity
2. Memory Leaks
3. Pointer Errors (Null/Invalid pointers)
4. Security Risks (Buffer overflow)
5. Difficult Debugging

Q. Meaning of & and * in pointer

1. & (Address-of Operator):


o It is used to get the memory address of a variable.
o It gives the address of the variable in memory, not the value stored in
it.

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)

You might also like