0% found this document useful (0 votes)
28 views16 pages

C Pointers

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)
28 views16 pages

C Pointers

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/ 16

C LANGUAGE

A basic introduction
POINTERS
WHAT IS A POINTER ?
01
Pointers are variables that #include <stdio.h> Here,
store addresses int i is a variable with a
int main() value of 5.
Addresses are memory {
locations. int i = 5; This value is stored at
somewhere in our RAM
Memory locations are int *ptr = &i; (Memory)
where our program stores return 0;
data. } To access it we need an
address.

i 5
ptr 0x003e
This is a pointer ->
f323
look how it’s 0x003ef323 <- This is a memory location
0x003ef500 or simply a address
pointing to address
of int i;
POINTERS
WHAT IS A POINTER ?
01
&(ampersand) is called The A pointer is declared in the
Address Operator . following way :

It returns a address of a int *pointer_name = &x;


variable.
notice * before pointer_name.
#include <stdio.h> This declares that pointer_name
is a pointer.
int main()
Notice &i,
{ And we are assigning the
the address is stored in
int i = 5; Address of x variable into
int *ptr;
int *ptr = &i; pointer_name which is also a
return 0; variable.
}
POINTERS
WHAT IS A POINTER ?
01
We can also do it in the A pointer can also be declared
following way: in the following way :

int *pointer_name;

for multi pointer :


#include <stdio.h>
int *ptr1, *ptr2;
int main()
it’s essentailly the same thing
{ we can assign values to them
int i = 5; later on.
int *ptr;
ptr = &i;
return 0;
}
POINTERS
WHAT IS A POINTER ?
01
#include <stdio.h> Output :
Note : The addresses are
int main() Address of i : 989f409c random, they are not
{ Address in ptr : 989f409c supposed to be same all the
int i = 5; Address of ptr : 989f40a0 time. They reset after each
int *ptr; program ends.
ptr = &i;
printf("Address of i : %x\n", &i);
printf("Address in ptr : %x\n", ptr);
printf("Address of ptr : %x\n", &ptr);
return 0;
}
This shows a proof how the
pointer and what’s it’s
pointing to works.
POINTERS
MORE ABOUT POINTERS:
01
A pointer holds an address
For 32-bit systems sizeof() ANY pointer is 4 bytes
For 64-bit systems sizeof() ANY pointer is 8 bytes

A pointer variable can be pre-fixed or post-fixed with


increment or decrement operators.

A pointer can point another pointer too! Here, p2 is pointing


to a pointer.
int x=5, *p1, **p2;
p2 = &p1;
POINTERS
DEREFERENCE OPERATOR
01
A dereference is simply using *
before a pointer to access the
elements it pointing to.

the * is called the dereference Notice the key difference,


operator
declaration of a pointer needs
#include <stdio.h> a * too. It points to any variable.

int main() but in order to access that


{ variable, let’s say modify the
int i = 5; value
int *ptr = &i;
*ptr = 6; // i = 6 now we need to dereference it
return 0; putting * before the pointer and
} assing a new value
POINTERS
NULL POINTERS
01
The Null Pointer is the pointer that does not point to any location
but NULL.

int *ptr = NULL;

Now this pointer is pointing nothing!!!


This is great for initialization.
POINTERS
VOID POINTERS
01
Pointers is simply a variable containing a address.

Let’s say we declared it and don’t want to just associate it to a address


of certain data types.

That’s where void pointers comes in.

The void pointer in C is a pointer that is not associated with any


data types

void *ptr;

Now it can hold any types of data’s addresses. Be it float, int, char etc.
POINTERS
VOID POINTERS
01
But void pointers cannot be dereferenced. They are type-casted.

#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int i = 5; int i = 5;
void *ptr; void *ptr;
ptr = &i; ptr = &i;
printf("%d\n", *ptr); printf("%d\n", *(int*)ptr);
return 0; return 0;
} }
Output : Output :
Compiler Error: 'void*' is not a pointer-to-object type 5
POINTERS
POINTERS ARITHMETIC
01
Pointer arithmetic are some basic operations we can do on the pointers.

This effects in changes of the memory address the pointer is holding.

It would shift to a different memory address in the process.


POINTERS
POINTERS ARITHMETIC - INCREMENT/DECREMENT
01

#include <stdio.h> Output : Notice the difference,


There’s a jump of 4bytes
int main() 875787964 in the memory location.
{ 875787968
int i = 5; 875787964
int *ptr = &i;
printf("%d\n", ptr);
ptr++;
printf("%d\n", ptr);
ptr--;
printf("%d\n", ptr);
return 0;
}
POINTERS
ARRAY AND POINTERS
01

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

‘H’ ‘E’ ‘L’ ‘O’ ‘W’ ‘O’ ‘R’ ‘L’ ‘D’ ‘\n’

#include <stdio.h> Output :


By default,
int main() H pointers takes the addresses of the first
{ element of an array.
char a[] = “HELOWORLD”;
char *ptr;
ptr = a;
printf(“%c\n", *ptr);
return 0;
}
POINTERS
ARRAY AND POINTERS
01

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

‘H’ ‘E’ ‘L’ ‘O’ ‘W’ ‘O’ ‘R’ ‘L’ ‘D’ ‘\n’

#include <stdio.h> Output :


Now we assigned the address of a[4] in
int main() W the pointer
{
char a[] = “HELOWORLD”;
char *ptr;
ptr = &a[4];
printf(“%c\n", *ptr);
return 0;
}
POINTERS
ARRAY AND POINTERS
01
#include <stdio.h> Output :
pointers of an array by default has the 0th
int main() *ptr = 1 index’s address as their stored data.
{ *(ptr+1) = 2 in this case the x[0]
int x[5] = {1, 2, 3, 4, 5}; *(ptr-1) = 32767
int* ptr; so *ptr = 1
ptr = x; if we add *(ptr + 1) , we go to the next
printf("*ptr = %d \n", *ptr); index which is x[1]
printf("*(ptr+1) = %d \n", *(ptr+1));
printf("*(ptr-1) = %d", *(ptr-1)); so *ptr = 2

return 0;
} x[0] x[1] x[2] x[3] x[4]

1 2 3 4 5
POINTERS
ARRAY AND POINTERS
01
#include <stdio.h> Output :
But, *(ptr-1) is different.
int main() *ptr = 1
{ *(ptr+1) = 2 we can generally assume we’re doing index
int x[5] = {1, 2, 3, 4, 5}; *(ptr-1) = 32767 addition.
int* ptr; &x[0 + 1] = x[1]
ptr = x; but, x[0-1] = x[-1]
printf("*ptr = %d \n", *ptr);
printf("*(ptr+1) = %d \n", *(ptr+1)); there’s no such thing as x[-1] . So we will
printf("*(ptr-1) = %d", *(ptr-1)); either get 0 or a random Garbage value.

return 0;
} x[0] x[1] x[2] x[3] x[4]

1 2 3 4 5

You might also like