The variables in C are classified into ordinary variables and pointer variables.An ordinary variable can take values of its associated type.
eg:
int x;
Here, x is an ordinary variable of type integer and assumes a whole number as its value.
x=10;
A pointer variable is declared as follows:
int *y;
The above declaration is a pointer variable declaration.Here y is a pointer variable whose type is an integer pointer (int*) .
A pointer variable assumes only the address as its value.Each variable takes some locations in the main memory is addressable.
In the above examples,x is an ordinary variables and y is a pointer variable. x is an ordinary integer and y is a pointer to an integer.Hence, we can store the address of x in y.
y=&x;
there are only two operators associated with pointers- an address of (&) operator and an indirection(*) operator.Both operators are unary operators.
since x is an integer it occupies two byte in the memory.
Every byte is addressable in main memory. To obtain the address of the variable we have to use the "address of" operator(&).So in the statement y=&x; the address of x is stored into the pointer variable y.Since y is a pointer variable it can assume only an address of a variable .We can say that y points to x.
Assume the value of x is 10.To retrieve the value of x through pointer variable y( since y already points to x), we can use the "indirection" operator(*)That is *y will give the value contained in the location pointed by y.If the value of x is 10 and the pointer variable y is pointing to x then the value of the expression *y is 10.
In the above example,
eg:
int x;
Here, x is an ordinary variable of type integer and assumes a whole number as its value.
x=10;
A pointer variable is declared as follows:
int *y;
The above declaration is a pointer variable declaration.Here y is a pointer variable whose type is an integer pointer (int*) .
A pointer variable assumes only the address as its value.Each variable takes some locations in the main memory is addressable.
In the above examples,x is an ordinary variables and y is a pointer variable. x is an ordinary integer and y is a pointer to an integer.Hence, we can store the address of x in y.
y=&x;
there are only two operators associated with pointers- an address of (&) operator and an indirection(*) operator.Both operators are unary operators.
since x is an integer it occupies two byte in the memory.
Every byte is addressable in main memory. To obtain the address of the variable we have to use the "address of" operator(&).So in the statement y=&x; the address of x is stored into the pointer variable y.Since y is a pointer variable it can assume only an address of a variable .We can say that y points to x.
Assume the value of x is 10.To retrieve the value of x through pointer variable y( since y already points to x), we can use the "indirection" operator(*)That is *y will give the value contained in the location pointed by y.If the value of x is 10 and the pointer variable y is pointing to x then the value of the expression *y is 10.
In the above example,
- y represents the address of the variable x (&x)
- *y represents the value of the variable x (x)
No comments:
Post a Comment