Sunteți pe pagina 1din 6

#include <stdio.

h>
int main()
{
printf("Hello c pointer World\n_____________________\n\n");
printf("25 = %d\n",25);
//printf("&25 = %d\n",&25);/*error: lvalue required as unary '&' operand */
//printf("*25 = %d\n",*25);/*error: invalid type argument of unary '*' (have 'in
t')*/
//printf("*&25 = %d\n",*&25);/*error: lvalue required as unary '&' operand */
//printf("&*25 = %d\n",&*25);/*error: invalid type argument of unary '*' (have '
int') */
int a=25;
printf("a = %d\n",a);
printf("&a = %d\n",&a);
//printf("*a = %d\n",*a);/*error: invalid type argument of unary '*' (have 'int'
) */
printf("*&a = %d\n",*&a);
//printf("&*a = %d\n",&*a);/*error: invalid type argument of unary '*' (have 'in
t') */
/*
& is an "unary operator"
It takes variable name as an operand
It gives the address number of the variable
& is known as "address of operator"
& is also known as "referencing operator" bcoz we tell address as reference
* is known as indirection operator, looks like a multiplication operator
* is also an unary operator
It takes address as an operand
It gives the value of the variable
We call it as dereferencing operator
So * operator is opposite of & operator
*/
/*
int x=5;
&x=7;
we can't store anything in &x as &x is not a variable,
it is the way to represent address of the block x
In the left of assignment operator no constant should be present
and writing &x is same as writing address number of x
We can't store anything in the address of x,
bcoz address of x is not a variable
but we can store the address inside another variable just like,
p=&x;
So we have to declare the variable first, before the above statement
but we can't write as

int p;
Then how can we know that 5 is an ordinary integer but not an address,
and value of p is an address not an integer
So we have to tell the compiler that x and p are two different types of variable
So we write as below to indicate the compiler that the p variable is an special
variable,
which is made to hold an address
int *p;
Similarly we can store integer value in variable x, not an address
In the declaration of p don't think * as an indirection operator;
now it is a symbol which is used for the declaration of p,
which is used to differentiate between x and p
If this was an operator then it should give the result by some operations
There are no such things here
Here p is a pointer variable
*/
/*
Pointer is a variable which contains the address of another variable
It always contains 2 bytes in the memory according to the dos based architecture
Then which would be the biggest address?
in decimal 65535
To store biggest address we need 16 bits, that means 2 bytes
*/
int x=5, *p;
p=&x;
printf("x = %d\n",x);
printf("p = %u\n",p);
printf("&x = %u\n",&x);
printf("&p = %u\n",&p);
//printf("*x = %d\n",*x);//error: invalid type argument of unary '*' (have 'int'
)
printf("*p = %d\n",*p);
printf("*&x = %d\n",*&x);
printf("*&p = %u\n",*&p);
//printf("&*x = %u\n",&*x);//error: invalid type argument of unary '*' (have 'in
t')
printf("&*p = %u\n",&*p);
/*
Extended concept of pointers
---------------------------Kisi bhi pointer variable ko banate wact hum jitne asterisk uske upar lagate hei
n
hum kehte hein wo pointer utni level of indirection ka he
Below level of pointer q is 2
above level of p is 1 and level of x is 0
Pointer jitne level ka hota he, usse exactly ek kum level wale ka hi address rak
h pata he
x is an int

p is a pointer to an int
q is a pointer to a pointer to an int
r is a pointer to a pointer to a pointer to an int
*/
int **q, ***r;
/*
Address can't have another address. So statements containing following are error
:
&&x
&&p
&&q
&&r
Value can't have another value. So following are error:
*x
**p
***q
****r
*/
//x=p;//warning: assignment makes integer from pointer without a cast [enabled b
y default]
//x=&p;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=*&p;//warning: assignment makes integer from pointer without a cast [enabled
by default]
x=*p;
//x=&*p;//warning: assignment makes integer from pointer without a cast [enabled
by default]
x=*&*p;
//x=q;//warning: assignment makes integer from pointer without a cast [enabled b
y default]
//x=&q;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=*&q;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=*q;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=&*q;//warning: assignment makes integer from pointer without a cast [enabled
by default]
x=**q;
//x=&**q;//warning: assignment makes integer from pointer without a cast [enable
d by default]
//x=r;//warning: assignment makes integer from pointer without a cast [enabled b
y default]
//x=&r;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=*&r;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=*r;//warning: assignment makes integer from pointer without a cast [enabled

by default]
//x=&*r;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=**r;//warning: assignment makes integer from pointer without a cast [enabled
by default]
//x=&**r;//warning: assignment makes integer from pointer without a cast [enable
d by default]
x=***r;
//x=&***r;//warning: assignment makes integer from pointer without a cast [enabl
ed by default]
//p=x; //warning: assignment makes pointer from integer without a cast [enabled
by default]
p=&x;
//p=*&x;//warning: assignment makes pointer from integer without a cast [enabled
by default]
//p=q;//warning: assignment from incompatible pointer type [enabled by default]
//p=&q;//warning: assignment from incompatible pointer type [enabled by default]
//p=*&q;//warning: assignment from incompatible pointer type [enabled by default
]
p=*q;
//p=&*q;//warning: assignment from incompatible pointer type [enabled by default
]
//p=**q;//warning: assignment makes pointer from integer without a cast [enabled
by default]
p=&**q;
//p=*&**q;//warning: assignment makes pointer from integer without a cast [enabl
ed by default]
//p=r;//warning: assignment from incompatible pointer type [enabled by default]
//p=&r;//warning: assignment from incompatible pointer type [enabled by default]
//p=*&r;//warning: assignment from incompatible pointer type [enabled by default
]
//p=*r;//warning: assignment from incompatible pointer type [enabled by default]
//p=&*r;//warning: assignment from incompatible pointer type [enabled by default
]
p=**r;

//p=&**r;//warning: assignment from incompatible pointer type [enabled by defaul


t]
//p=***r;//warning: assignment makes pointer from integer without a cast [enable
d by default]
p=&***r;
//p=*&***r;//warning: assignment makes pointer from integer without a cast [enab
led by default]
//q=x;//warning: assignment makes pointer from integer without a cast [enabled b
y default]
//q=&x;//warning: assignment from incompatible pointer type [enabled by default]
//q=*&x;//warning: assignment makes pointer from integer without a cast [enabled
by default]
//q=p;//warning: assignment from incompatible pointer type [enabled by default]
q=&p;
//q=*p;//warning: assignment makes pointer from integer without a cast [enabled
by default]
//q=*&p;//warning: assignment from incompatible pointer type [enabled by default
]
//q=&*p;//warning: assignment from incompatible pointer type [enabled by default
]
//q=r;//warning: assignment from incompatible pointer type [enabled by default]
//q=&r;//warning: assignment from incompatible pointer type [enabled by default]
q=*r;
//q=*&r;//warning: assignment from incompatible pointer type [enabled by default
]
//q=&*r;//warning: assignment from incompatible pointer type [enabled by default
]
//r=x;//warning: assignment makes pointer from integer without a cast [enabled b
y default]
//r=&x;//warning: assignment from incompatible pointer type [enabled by default]
//r=*&x;//warning: assignment makes pointer from integer without a cast [enabled
by default]
//r=p;//warning: assignment from incompatible pointer type [enabled by default]
//r=&p;//warning: assignment from incompatible pointer type [enabled by default]
//r=*&p;//warning: assignment from incompatible pointer type [enabled by default
]

//r=*p;//warning: assignment makes pointer from integer without a cast [enabled


by default]
//r=&*p;//warning: assignment from incompatible pointer type [enabled by default
]
//r=q;//warning: assignment from incompatible pointer type [enabled by default]
r=&q;
//r=*&q;//warning: assignment from incompatible pointer type [enabled by default
]
//r=*q;//warning: assignment from incompatible pointer type [enabled by default]
//r=&*q;//warning: assignment from incompatible pointer type [enabled by default
]
//r=**q;warning: assignment makes pointer from integer without a cast [enabled b
y default]
//r=&**q;//warning: assignment from incompatible pointer type [enabled by defaul
t]
/*
pointer DECLARATION
-------------------*/
float* i,*j,**k;
printf("type of i = %u\n",typeOf(i));
return (0);
}

S-ar putea să vă placă și