Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum.
Examples:
int *ptr;
int (*ptr)();
int (*ptr)[2];
In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.
(1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)
About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)
(2) Meaning of following pointer declaration and definition:
int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)
About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)
About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)
Cancellation rule of above two operators:
* and & operators always cancel to each other. i.e.
*&p=p
But it is not right to write:
&*p=p
Simple example:
What will be output of following c program?
void main(){
int x=25;
int *ptr=&x; //statement one
int **temp=&ptr; //statement two
printf(“%d %d %d”.x.*ptr,**temp);
}
Output: 25 25 25
Explanation:
As we know value of variable x is 25.
*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25
**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25
at 9:02 PM 0 comments Links to this post
Wednesday
understanding pointer in c
Pointer is a very simple concept. Understand it by simple example: Suppose you have one million dollar and you are going to bank. You take two boxes. In first box you keep money and in the second box you keep key of the first box. In your pocket you keep key of second box.
Variable in c of type int, char, structure are similar to box 1 that is it keeps actual value just like money in the above example while pointers are similar to box 2 which keeps Pointer is just like BOX 2 which keeps memory address of another variable just like key of box 1.
Examples:
int *ptr;
int (*ptr)();
int (*ptr)[2];
In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.
(1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)
About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)
(2) Meaning of following pointer declaration and definition:
int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)
About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)
About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)
Cancellation rule of above two operators:
* and & operators always cancel to each other. i.e.
*&p=p
But it is not right to write:
&*p=p
Simple example:
What will be output of following c program?
void main(){
int x=25;
int *ptr=&x; //statement one
int **temp=&ptr; //statement two
printf(“%d %d %d”.x.*ptr,**temp);
}
Output: 25 25 25
Explanation:
As we know value of variable x is 25.
*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25
**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25
at 9:02 PM 0 comments Links to this post
Wednesday
understanding pointer in c
Pointer is a very simple concept. Understand it by simple example: Suppose you have one million dollar and you are going to bank. You take two boxes. In first box you keep money and in the second box you keep key of the first box. In your pocket you keep key of second box.
Variable in c of type int, char, structure are similar to box 1 that is it keeps actual value just like money in the above example while pointers are similar to box 2 which keeps Pointer is just like BOX 2 which keeps memory address of another variable just like key of box 1.
In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. For high-level programming languages, pointers effectively take the place of general purpose registers in low level languages such as assembly language or machine code - but, in contrast, occupies part of the available memory. A pointer references a location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing the pointer. A pointer is a simple, less abstracted implementation of the more abstracted reference data type (although it is not directly usable as C++ reference). A number of languages support some type of pointer, although some are more restricted than others.
ReplyDeleteThanks Brothr...
ReplyDelete