What is the data type of pointer variables? Suppose that we don‘t know the name of a variable but we do know its address. Can we access the contents of that variable? Explain briefly.
What is the data type of pointer variables?
Pointers are used to operate directly on the memory area. Access of variables using pointers is faster rather than variable access by its declaration. A pointer is defined as below:
int x, *ptr;
ptr = &x;
*ptr = 3;
In above example, a variable x is declared as int type and corresponding a pointer is declared as *ptr. Next, ptr has been assigned the address of variable x as ptr = &x. Next *ptr is assigned as *ptr = 3
*ptr = 3 will assign the value in x as x = 3
*ptr = the value at the address contained in ptr.
Suppose that we don‘t know the name of a variable but we do know its address. Can we access the contents of that variable? Explain briefly.
The data using the pointer is always accessed using its address. For example, in the above case, the variable ptr holds the address of the data. Therefore using the address of the data variable, it can be accessed using *ptr.
Comments
Leave a comment