You are on page 1of 19

Pointers in C

Memory Organization
9:30 AM Admin No comments

Memory Organization

1. When we use variable in program then Compiler keeps some memory for that variable depending on thedata type 2. The address given to the variable is Unique with that variable name 3. When Program execution starts the variable name is automatically translated into the correspondingaddress.

Pointer Basic Concept


9:48 AM Admin No comments

Basic of Pointer :

Consider above Diagram :

1. i is the name given for Particular memory location , Let it's Corresponding address be 65624 . 2. Value stored in that variable is 5 3. The address of the variable 'i' is stored in another integer variable whose name is 'j' and which is having 65522 as its corresponding address .

4. We can write , j = & i ;(i.e j stores address of i ) 5. Here j is not ordinary variable , It is special variable and called pointer variable as it stores the address of the another ordinary variable
Note : 1. Pointer variable stores the address of the Ordinary variable 2. Pointer variables are declared as int *j; 3. j contain the address of i

Address Operator in C Programming


10:58 AM Admin 2 comments

1. It is Denoted by '&' 2. When used as a prefix to a variable name '&' operator gives the address of that variable. Example :

&n

Gives address on n

No 1 2 3

Invalid Use of Address Operator &75 &(a+b) &('a')

view plainprint?

1. // How Address Operator works ? 2. 3. #include<stdio.h> 4. void main() 5. { 6. int a = 10; 7. printf("\n Value of n is : %d ",n); 8. printf("\n Value of &n is : %u ",&n); 9. } 10. </stdio.h>

Output :

view plainprint?

1. Value of n is : 10 2. Value of &n is : 1002

How much Memory required to store Pointer variable?


4:09 PM Admin No comments

1. Pointer Variable Stores the address of the variable 2. Variable may be integer,character,float but the address of always integer so Pointer requires 2 bytes of memory. 3. This requirement is different for different Compilers.

the

variable

is

What is Pointer ?
4:18 PM Admin No comments

Pointer : 1. Pointer is a variable which stores the address of another variable 2. Since it is a variable , the pointer itself will be stored at different memory location.

Pointer Operator in C Program


4:52 PM Admin No comments

1. To Create Pointer to a variable we use '*' , '&' Operator [ we have nothing to do with multiplication and Logical AND Here ] 2. '&' operator is called as address Operator 3. '*' is called as 'Value at address' Operator 4. 'Value at address' Operator gives 'Value stored at Particular address. 5. 'Value at address' is also called as 'Indirection Operator'.

Code :
view plainprint?

1. 2. 3. 4. 5. 6.

#include<stdio.h> #include<conio.h> void main() { int n = 20; printf("\nThe address of n is %u",&n);

7. printf("\nThe Value of n is %d",&n); 8. printf("\nThe Value of n is %d",*(&n)); 9. getch(); 10. } 11. </conio.h></stdio.h>

Output :
view plainprint?

1. The address of n is 1002 2. The Value of n is 20 3. The Value of n is 20

Question : As shown , *(&n) is same as printing the value of n , How ? 1. &n gives address of the memory location whose name is 'n'. 2. ' * ' means value at Operator gives value at address specified by &n. Be Conceptual !!

1. m = &n : Address of n is stored in m , but remember that m is not ordinary variable like n 2. So Compiler must provide space for it in memory
What does this declaration [ int *m ; ] tells compiler ? 1. m is declared as that m will be used only for storing the address of the integer valued variables 2. we can also say that m points to integer 3. Value at the address contained in m is integer .

Declaration 1 2 3 int *p float *q char *ch

What it tells ? p is going to store address of integer value q is going to store address of floating value ch is going to store address of character variable

Memory Required 2 bytes 2 bytes 2 bytes

How to Declare Pointer in C Programming ?


5:34 PM Admin No comments

Syntax : data_type * pointer_name ; Explanation : Syntax

Refers Type of variable that the pointer points to OR data type whose address is stored in pointer_name Indirection Operator Value at address Operator Indicates Variable declared is of Pointer type Any Valid C identifier Must follow all Rules of Variable name declaration

Data Type

Asterisk(*)

Pointer_Name

Valid Writing Styles:

Note : * can appears anywhere between Pointer_name and Data Type

int *p; int *q;

int * q ;

Initialization of Pointer in C : Initializing Pointer


7:15 PM Admin No comments

Initializing Pointer

Syntax pointer = &variable; Example : ptr_n = &n ; // Assign Address of n to Pointer variable ptr_n Note :

Pointers are always initialized before using

De-Referencing Pointer in C Programming


6:21 PM Admin No comments

De-Referencing Pointer in C Programming Language :


Dot(.) indirection operator is used along with pointer variable Dot Operator is also called as value at operator When used with Pointer variable. It refers to variable being pointed to,this is called as Dereferencing of Pointers

Pointer De-referencing :

operation performed to access or manipulate data contained in memory location pointed to by a pointer Any Operation performed on the de-referenced pointer directly affects the value of variable it pointes to.

Demo Example :
view plainprint?

1. // Sample Code for De-referencing of Pointer 2. 3. int n = 50 , x ; 4.

5. int *ptr ; 6. 7. ptr = &n; 8. 9. x = *ptr;

// Un-initialized Pointer // Stores address of n in ptr // Put Value at ptr in x

Evaluation :
view plainprint?

1. // Sample Code for De-referencing of Pointer 2. 3. *(ptr) = value at (ptr) 4. = value at (2001) //Address in ptr is nothing but address of n 5. = 50

Summary : 1. Name of Normal Variable always points to the Value of variable 2. Pointer variable always Stores Address of variable 3. *ptr = n are one and the same

Variable n &n ptr *ptr

Value in it 50 1002 1002 50

Void Pointers in C Programming Language


4:55 PM Admin No comments

Void Pointers in C :

1. In C General Purpose Pointer is Called as void Pointer. 2. It does not have any data type associated with it 3. It can store address of any type of variable

Declaration :
view plainprint?

1. void * pointer_name ;

Example :
view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9.

void *ptr; char cnum; int inum; float fnum; ptr = &cnum; ptr = &inum; ptr = &fnum;

// ptr is declared as Void pointer

// ptr has address of character data // ptr has address of integer data // ptr has address of float data

De-Referencing Void Pointers in C Programming Language


5:26 PM Admin No comments

Void Pointers are

General Purpose Stores the address of any type of variable

Why to use Void Pointers ?


view plainprint?

1. float *ptr; 2. int num; 3. 4. ptr = &num ;

// Illegal Use of Pointer

So in order to increase the re-usability of the Pointer it is declared as void Pointer.

De-Referencing void Pointer : Suppose

ptr is Void Pointer inum is integer cnum is character fnum is float

Whose Address is stored in Void Pointer ? Integer Charcter Floating


view plainprint?

How to De-Reference it ? *((int*)ptr) *((char*)ptr) *((float*)ptr)

1. #include<stdio.h> 2. 3. main() 4. { 5. int inum = 8 , temp ; 6. float fnum = 67.7; 7. void *ptr; 8. 9. ptr = &inum; // Assign address of integer to void pointer 10. 11. temp = *((int*)ptr); </stdio.h>
view plainprint?

1. <stdio.h> </stdio.h>
view plainprint?

1. <stdio.h>printf("\nThe valu of inum = ",temp); 2. 3. } 4. </stdio.h>

Output : 8

Pointer Arithmatics : Incrementing Pointer Variable in C Programming


7:06 PM Admin No comments

Incrementation of Pointer Variable Depends Upon :

data type of the Pointer variable

Formula : ( After incrementing ) new value = ( current address in Pointer ) + i * size_of ( data type ) Example :

Data Type int float char

Older Address stored in pointer 1000 1000 1000

Next Address stored in pointer after incrementing (ptr++) 1002 1004 1001

Explanation :

Incrementing a pointer to an integer data will cause its value to be incremented by 2 . This differs from compiler to compiler as memory required to store integer vary compiler to compiler

Pointer Arithmatics : Decrementing Pointer Variable in C Programming


7:15 PM Admin 3 comments

Decrementation of Pointer Variable Depends Upon :

data type of the Pointer variable

Formula : ( After incrementing ) new value = ( current address in Pointer ) - i * size_of ( data type ) Example :

Data Type int float char

Older Address stored in pointer 1000 1000 1000

Next Address stored in pointer after incrementing (ptr--) 0998 0996 0999

Explanation :

Decrementing a pointer to an integer data will cause its value to be decremented by 2 This differs from compiler to compiler as memory required to store integer vary compiler to compiler

Pointer Addition: Adding integer value with Pointer


6:56 AM Admin No comments

Adding integer value with Pointer ;


view plainprint?

1. int *ptr , n; 2. ptr = &n ; 3. ptr = ptr + 3;

Output : 1. It will Increment the pointer by 6 2. For more details : Refer Formulae

Subtracting integer value with Pointer


5:31 PM
view plainprint?

Admin

No comments

Subtracting integer value with Pointer ;

1. int *ptr , n; 2. ptr = &n ; 3. ptr = ptr - 3;


Output : 1. It will decrement the pointer by 6 2. For more details : Refer Formulae

Differencing or Subtracting Pointer in C Programming Language


6:44 PM Admin No comments

Differencing Pointer in C Programming Language : 1. Differencing Means Subtracting two Pointers. 2. Subtraction gives the Total number of objects between them . 3. Subtraction indicates "How apart the two Pointers are " Example :
view plainprint?

1. int n , *p ,*q ; 2. 3. // P stores the address of Variable n 4. p = &n ; 5. 6. // Value of q is incremented by 4 bytes 7. q = p + 2 ; 8. 9. // Differencing two Pointers 10. printf("%d",q-p);

Output : 2

Observations & Explanation : 1. Numerically Subtraction ( q-p ) differs by 4 2. As both are Integers they are numerically Differed by 4 and Technically by 2 objects 3. Suppose Both pointers of float the they will be differed numerically by 8 and Technically by 2 objects

Comparison of two Pointers

10:30 AM

Admin

No comments

Comparison between two Pointers

1. 2. 3. 4.

Pointer comparison is Valid only if the two pointers are Pointing to same array All Relational Operators can be used for comparing pointers of same type All Equality and Inequality Operators can be used with all Pointer types Pointers cannot be Divided or Multiplied

Precedence of ' * ' and ' & ' Operator in C Programming


10:45 AM Admin No comments

Precedence of ' * ' and ' & ' Operator

1. Both are Unary Operators 2. They have Equal Precedence 3. They Associate from Right --> Left

Meaning of (++*ptr)
10:56 AM Admin No comments

Meaning of (++*ptr) : Consider the Following Example :


view plainprint?

1. int n = 20 , *ptr ; 2. ptr = &n; 3. printf("%d",++*ptr);

Explanation :

1. 2. 3. 4.

'++' and '*' both have Equal Precedence Associativity is from Right to Left ( Expression evaluated from R->L) Both are Unary (Operates on single operand ) So ' * ' is Performed First then ' ++ '

Meaning of (*++ptr)
11:05 AM Admin No comments

Meaning of (*++ptr) : Consider the Following Example :


view plainprint?

1. int n = 20 , *ptr ; 2. ptr = &n; 3. printf("%d",*++ptr);

Explanation :

1. 2. 3. 4.

'++' and '*' both have Equal Precedence Associativity is from Right to Left ( Expression evaluated from R->L) Both are Unary (Operates on single operand ) So ' ++ ' is Performed First then ' * '

Pointer to Pointer in C Programming


11:47 AM Admin No comments

Pointer to Pointer in C Programming

Declaration int **ptr2ptr ; // Double Pointer

Consider the Following Example :


view plainprint?

1. int n = 45 , *ptr , **ptr2ptr ; 2. ptr = &n; 3. ptr2ptr = &ptr;

What is Pointer to Pointer ? 1. Double (**) is used to denote the double Pointer 2. Pointer Stores the address of the Variable 3. Double Pointer Stores the address of the Pointer Variable

Statement *ptr **ptr2ptr ptr ptr2ptr


Notes :

What will be the Output ? 45 45 &n &ptr

1. Conceptually we can have Triple ..... n pointers 2. Example : *****n,****b can be another example

Pointer to Constant Objects


1:01 PM Admin No comments

Pointer to Constant Objects :

Pointer to a Constant Object is called as 'Pointer to Constant Object' :

Syntax const data-type *pointer_name ; Example :


view plainprint?

1. int n = 30 ; 2. const int ptr; 3. ptr = &n;

What does it means ?

" Pointer to Such Object cannot be changed " Illegal Use !!!! *ptr = 20 ; Note : ptr++ is valid in this Case !!!

What is Constant Pointers [ Const Pointer ] in C Programming


10:11 AM Admin No comments

What is Constant Pointers [ Const Pointer ] in C Programming :

As name suggests , Constant Pointers Cannot be modified . Modification in Integer to which it Points to is Allowed Modification made in Pointer is Not Allowed

Syntax Declaration : data_type * const Pointer_name ; Live Example : int i = 20; int * const ptr ; // Declare Constant Pointer ptr = &i; *ptr = 20 ; // Valid Statement ptr ++ ; // Invalid Statement

Explanation : 1. 2. 3. 4. In the Diagram Shown The at 2001 we have Store the value 15 . Constant Pointer is Pointer to that Variable [Pointer Stores address 2001] We cannot Change the Address stored in Pointer Variable i.e 2001 is fixed But we can Change the Value at 2001 i.e we can change value 15 to 20

What is the difference between constant to pointer and pointer to constant?


11:45 AM Admin No comments

What is the difference between constant to pointer and pointer to constant?

No 1

Pointer to Constant

Constant Pointers

*ptr = 20 Statement is *ptr = 20 is Absolutely Valid in Invalid in Pointer to Constant Pointers i.e Assigning Constant i.e Assigning Value is Perfectly legal Value is Illegal 2 ptr ++ Statement is Valid ptr ++ Statement is Valid in in Pointer to Constant Constant Pointers 3 Pointer Can be Pointer Cannot be Incremented Incremented and and Decremented Decremented 4 5 Pointer is Pointing to Constant Data Object Declaration : const int *ptr ; Constant Pointer is Pointing to Data Objects Declaration : int * const ptr ;

Which pointer require more memory space int or character pointer ?


6:09 PM Admin No comments

Which pointer require more memory space int or character pointer ?

1. 2. 3. 4.

Size of Pointer Variable does not depends upon data type Pointer variable only stores the address of the variable . Address of variable is of integer type So Size of Pointers variables of any type is 2 byte

You might also like