Professional Documents
Culture Documents
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.
Basic of Pointer :
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
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
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. 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.
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.
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?
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 .
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
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
int * q ;
Initializing Pointer
Syntax pointer = &variable; Example : ptr_n = &n ; // Assign Address of n to Pointer variable ptr_n Note :
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?
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
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 has address of character data // ptr has address of integer data // ptr has address of float data
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?
Output : 8
Formula : ( After incrementing ) new value = ( current address in Pointer ) + i * size_of ( data type ) Example :
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
Formula : ( After incrementing ) new value = ( current address in Pointer ) - i * size_of ( data type ) Example :
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
Output : 1. It will Increment the pointer by 6 2. For more details : Refer Formulae
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
10:30 AM
Admin
No comments
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
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
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
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 ' * '
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
1. Conceptually we can have Triple ..... n pointers 2. Example : *****n,****b can be another example
" Pointer to Such Object cannot be changed " Illegal Use !!!! *ptr = 20 ; Note : ptr++ is valid in this Case !!!
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
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 ;
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