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

Pointers

Pointer variables in C store the address of another variable rather than the value. Pointer variables are declared with a data type followed by an asterisk, such as int *p. The ampersand (&) operator returns the address of a variable, while the asterisk (*) operator dereferences a pointer to access the value at that address. Pointers allow passing arguments to functions by reference rather than by value, so any changes made to the variable within the function are reflected back in the calling function.
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)
9 views

Pointers

Pointer variables in C store the address of another variable rather than the value. Pointer variables are declared with a data type followed by an asterisk, such as int *p. The ampersand (&) operator returns the address of a variable, while the asterisk (*) operator dereferences a pointer to access the value at that address. Pointers allow passing arguments to functions by reference rather than by value, so any changes made to the variable within the function are reflected back in the calling function.
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/ 18

3.3.3.

Pointer variables in C

3.3.3.1. Address in C language

Before you get into the concept of pointers, let's first get familiar with addresses
in C.
If you have a variable var in your program, &var will give you its address in the
memory, where & is commonly called the reference operator.

You must have seen this notation while using scanf() function. It was used in the
function to store the user inputted value in the address of var, I know you have

N
SO
already seen the magic “&”.

EL
N
scanf("%d", &var); // values are actually stored in memory locations of variables

LY
#include <stdio.h> SA
int main()
ZA

{
BI

int var = 5;
U
AS

printf("Value: %d\n", var);


IR

printf("Address: %p\n", &var); //Notice, the ampersand(&) before var. return 0;


by

}
ed
ar

Output
ep
pr

Note: You may obtain different values of address while using this code.
In above source code, value 5 is stored in the memory location 0x7ffcac23e854. var
is just the name given to that location.

In C, you can create a special variable that stores the address (rather than the
value). This variable is called a : “pointer variable” or simply a: ”pointer”.

3.3.3.3. How to create a pointer variable?


Data_type *pointer_variable_name;
Example:
int *p;

Above statement defines p as a pointer variable.

Reference operator (&) and dereference operator (*)

As discussed, & is called a reference operator. It gives you the address of a variable.
Likewise, there is another operator that gets you the value from the address, it is
called a dereference operator, which is *.
Below example clearly demonstrates the use of pointers, reference operator and
dereference operator.

N
SO
Note: The * sign when declaring a pointer is not a dereference operator. It is just a

EL
similar notation that creates a pointer.

N
LY
Example: How Pointer Works? SA
ZA

#include <stdio.h>
BI
U

int main()
AS
IR

{
by

int * pc, c;
ed
ar
ep

c = 22;
pr

printf("Address of c: %p\n", &c);


printf("Value of c: %d\n\n", c);

pc = &c;
printf("Address of c: %p\n", pc);
printf("Content pointed by pc: %d\n\n", *pc);

c = 11;
printf("Address of c: %p\n", pc);
printf("Content pointed by pc: %d\n\n", *pc);

// a pointer can change the value of a variable


*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c);
return 0;
}
Output

N
SO
EL
N
LY
SA
ZA

Explanation of the program


BI
U
AS

1. int* pc, c;
IR

Here, a pointer pc and a normal variable c, both of type int, is created. Since
by

pc and c are not initialized at first, pointer pc points to either no address or a


ed
ar

random address. And, variable c has an address but contains a random


ep

garbage value.
pr

2. c = 22;

This assigns 22 to the variable c, i.e., 22 is stored in the memory location of


variable c.
Note that, when printing &c (address of c), we use %p since we are outputting
the address/pinter. (https://www.freecodecamp.org/news/format-specifiers-
in-c/)
N.B:
- The address is output in Hexadecimal format in this case.
- Some compilers use %u modifier and output the address in decimal format,
as a unsigned integer

3. pc = &c;
This assigns the address of variable c to the pointer pc.
You see the value of pc is same as the address of c and the value pointed by pc
is 22 as well.
4. c = 11;

This assigns 11 to variable c.


Since, pointer pc points to the same address, value pointed by pointer pc is
11 as well.

5. *pc = 2;

This changes the value at the memory location pointed by pointer pc to 2. Since

N
SO
pc always points to c, the value of c is also changed to 2. This is now more

EL
Interesting! How a pointer can easily modify the value of the variable it points

N
LY
to.
SA
Call a function by value and Call by reference in
ZA

There are two methods to pass the data into the function in C language, i.e.,
BI
U

call by value and call by reference.


AS
IR

Call by value
by
ed
ar

- In the call by value method, the value of the actual parameters is copied into
ep

the formal parameters. In other words, we can say that the value of the
pr

variable is used in the function call in the call by value method.


- In the call by value method, we can not modify the value of the actual
parameter by the formal parameter.
- In call by value, different memory is allocated for actual and formal
parameters since the value of the actual parameter is copied into the formal
parameter.
- The actual parameter is the argument which is used in the function call
whereas the formal parameter is the argument which is used in the function
definition.

Call by reference
When we call a function by passing the addresses of actual parameters then this
way of calling the function is known as call by reference. In call by reference,
the operation performed on formal parameters affects the value of actual
parameters because all the operations performed on the value stored in the
address of actual parameters. It may sound confusing first but the following
example would clear your doubts.

Example 1:

#include <stdio.h>

N
void increment(int *var)

SO
EL
N
{

LY
SA
/* Although we are performing the increment on variable
ZA
BI
U

* var, however the var is a pointer that holds the address


AS
IR
by

* of variable num, which means the increment is actually done


ed
ar
ep

* on the address where value of num is stored.


pr

*/

*var = *var+1;

int main()

int num=20;
/* This way of calling the function is known as call by

* reference. Instead of passing the variable num, we are

* passing the address of variable num

*/

increment(&num);

printf("Value of num is: %d", num);

N
SO
return 0;

EL
N
LY
} SA
ZA

When the code above is compiled and executed, it produces the following result:
BI
U
AS
IR
by
ed

Example 2: Swapping Numbers


ar
ep
pr

#include<stdio.h>

void swapnum ( int *var1, int *var2 )

int tempnum ;

tempnum = *var1 ;

*var1 = *var2 ;

*var2 = tempnum ;
}

int main( )

int num1 = 35, num2 = 45 ;

printf("Before swapping:");

printf("\nnum1 value is %d", num1);

N
SO
printf("\nnum2 value is %d", num2);

EL
N
LY
/*calling swap function*/ SA
ZA

swapnum( &num1, &num2 );


BI
U
AS

printf("\nAfter swapping:");
IR
by
ed

printf("\nnum1 value is %d", num1);


ar
ep
pr

printf("\nnum2 value is %d", num2);

return 0;

The following is the output of the program:


3.3.3.3. Pointer access and errors

However, initializing pointers unnecessarily could hinder program analysis, thereby


hiding bugs.
In any case, once a pointer has been declared, the next logical step is for it to point
at something:

int a = 5;
int *ptr = NULL;

N
SO
EL
ptr = &a; // address to pointer assignment

N
LY
This assigns the value of the address of a to ptr. For example, if a is stored at
SA
memory location of 0x8130 then the value of ptr will be 0x8130 after the
ZA

assignment. To dereference the pointer, an asterisk is used again:


BI
U

*ptr = 8;
AS
IR
by
ed
ar
ep
pr
This means take the contents of ptr (which is 0x8130), "locate" that address in
memory and set its value to 8. If a is later accessed again, its new value will be
8. Which means :”A pointer can change the value of a variable!”
This example may be clearer if memory is examined directly. Assume that a is located
at address 0x8130 in memory and ptr at 0x8134; also assume this is a 32-bit
machine such that an int is 32-bits wide. The following is what would be in memory
after the following code snippet is executed:
int a = 5;
int *ptr = NULL;

Address Contents

N
0x8130 0x00000005

SO
0x8134 0x00000000

EL
N
LY
SA
(The NULL pointer shown here is 0x00000000.) By assigning the address of a to ptr:
ZA

ptr = &a;
BI

yields the following memory values:


U
AS

Address Contents
IR
by

0x8130 0x00000005
ed

0x8134 0x00008130
ar
ep

Then by dereferencing ptr by coding:


pr

*ptr = 8;
the computer will take the contents of ptr (which is 0x8130), 'locate' that address,
and assign 8 to that location yielding the following memory:
Address Contents
0x8130 0x00000008
0x8134 0x00008130

Clearly, accessing a will yield the value of 8 because the previous instruction
modified the contents of a by way of the pointer ptr.
Common mistakes when working with pointers
Suppose, you want a pointer pc to point to the address of c. Then,
int c, *pc;
// Wrong! pc is address whereas,
// c
is not
an
addre
ss. pc
= c;
// Wrong! *pc is the value pointed by address whereas,
// &c is an address.

N
SO
pc = &c;

EL
// Correct! pc is an address and,

N
LY
// &c is SA
also an
ZA

address
BI
U

. ptr =
AS
IR

&a;
by

// Correct! * ptr is the value pointed by address and,


ed

// c is also a value (not address).


ar
ep

*pc = c;
pr

3.3.3.4. Strings and pointer

Similarly like arrays, string names are "decayed" to pointers.


Hence, you can use a pointer with the same name as string to
manipulate elements of the string.

Example : Strings and Pointers

#include <stdio.h>

int main(void) {
char name[] = "Harry Potter"; // A string initialization as a array of
characters

printf("\n%c", *name);// Output: H


printf("\n%c", *(name+1));// Output: a
printf("\n%c", *(name+7));// Output: o

char *namePtr;

namePtr = name;

printf("\n%c", *namePtr);// Output: H


printf("\n%c", *(namePtr+1));// Output: a

N
printf("\n%c\n",*(namePtr+7));// Output: o

SO
}

EL
Output:

N
LY
SA
ZA
BI
U
AS
IR
by

3.3.3.5 Dynamic memory allocation in C


ed

In C, memory is allocated dynamically using standard library functions malloc(),


ar
ep

calloc(), free() and realloc() defined in the stdlib.h header file.


pr

As you know, an array is a collection of a fixed number of values. Once the size
of an array is declared, you cannot change it.

Sometimes the size of the array you declared may be insufficient. To solve this
issue, you can allocate memory manually during run-time. This is known as
dynamic memory allocation in C programming.

C malloc()

The name "malloc" stands for memory allocation.


The malloc() function reserves a block of memory of the specified number of
bytes. And, it returns a pointer of void which can be casted into pointers of any
form.

Syntax of malloc()

ptr = (castType*) malloc(size);

Example

ptr = (float*) malloc(100 * sizeof(float));

N
The above statement allocates 400 bytes of memory. It's because the size of the

SO
float is 4 bytes. And, the pointer ptr holds the address of the first byte in the

EL
N
allocated memory.

LY
SA
The expression results in a NULL pointer if the memory cannot be allocated.
ZA
BI

C calloc()
U
AS

The name "calloc" stands for contiguous allocation.


IR
by

The malloc() function allocates memory and leaves the memory uninitialized,
ed

whereas the calloc() function allocates memory and initializes all bits to zero.
ar
ep
pr

Syntax of calloc()

ptr = (castType*)calloc(n, size);

Example:

ptr = (float*) calloc(25, sizeof(float));

The above statement allocates contiguous space in memory for 25 elements of


type float.
C free()

Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on their own. You must explicitly use free() to release the space.

Syntax of free()

free(ptr);

This statement frees the space allocated in the memory pointed by ptr.

N
SO
EL
Example 1: malloc() and free()

N
LY
// Program to calculate the sum of n numbers entered by the user
SA
ZA
BI

#include <stdio.h>
U
AS

#include <stdlib.h>
IR
by

int main() {
ed
ar

int n, i, *ptr, sum = 0;


ep
pr

printf("Enter number of elements: ");


scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);
return 0;
}
Sample Output

N
SO
EL
N
LY
SA
Here, we have dynamically allocated the memory for n number of int.
ZA
BI
U
AS

Example 2: calloc() and free()


IR
by

// Program to calculate the sum of n numbers entered by the user


ed
ar
ep

#include <stdio.h>
pr

#include <stdlib.h>

int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int*) calloc(n, sizeof(int));


if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);


free(ptr);

N
return 0;

SO
EL
}

N
Sample Output

LY
SA
ZA
BI
U
AS
IR
by

C realloc()
ed
ar

If the dynamically allocated memory is insufficient or more than required, you


ep

can change the size of previously allocated memory using the realloc() function.
pr

Syntax of realloc()

ptr = realloc(ptr, x);

Here, ptr is reallocated with a new size x.

Example 3: realloc()

#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);

ptr = (int*) malloc(n1 * sizeof(int));

printf("Addresses of previously allocated memory:\n");


for(i = 0; i < n1; ++i)
printf("%pc\n",ptr + i);

N
SO
EL
printf("\nEnter the new size: ");

N
scanf("%d", &n2);

LY
SA
// rellocating the memory
ZA
BI

ptr = realloc(ptr, n2 * sizeof(int));


U
AS
IR

printf("Addresses of newly allocated memory:\n");


by

for(i = 0; i < n2; ++i)


ed
ar

printf("%pc\n", ptr + i);


ep
pr

free(ptr);

return 0;
}
Sample Output
The corresponding memory addresses in decimal are

N
SO
1504083730344972

EL
N
1504083730345036

LY
SA
(Previously allocated )
ZA
BI

and
U
AS

1504083730344972
IR
by

1504083730345036
ed
ar
ep

1504083730345100
pr

(Newly allocated)

The presentation on this topic is here:


https://docs.google.com/presentation/d/1BamaqhnWuwhTwsuKhwEpDTAdd
LBuyuROBbe0jZRXg_w/edit#slide=id.g230e362be33_0_199

3.3.3.6. Benefits of using pointers


Below we have listed a few benefits of using pointers:
1. Pointers are more efficient in handling Arrays and Structures.
2. Pointers allow references to function and thereby
help in passing of function as arguments to other
functions.
3. They reduce the length of the program and its execution time as well.
4. They allow C language to support Dynamic Memory management.

N
SO
EL
N
LY
SA
ZA
BI
U
AS
IR
by
ed
ar
ep
pr

You might also like