General Questions
Practice and master this topic with our carefully crafted questions.
Normal variable means it stores the values directly. But if you want to change those values without touching those variables we are using pointers.
Pointer is a one type of variable it holds the address of the variable.
int a=5; // suppose a variable address is 100
int *p;
p=&a;// here p(pointer variable)holdes the address of the a variable.
The answer can be summarized as follows...
[A] A keyword used to create variables -> Wrong. A pointer is NOT a keyword.
[B]. A variable that stores address of an instruction -> Wrong. Though a pointer may hold the address of an instruction (i.e. function pointer), it is no required to
[C]. A variable that stores address of other variable -> Correct. A pointer is a variable that stores the address of any other variable be it a value or another address.
[D]. All of the above -> Wrong as A and B are incorrect.
For example;
int *p; // creates a pointer to integer
int a=10//variable a has value as 10 and address as 2000
p=&a//here address of a(2000) is stored in p ie p is pointing to a
int b;//a variable
b=*p;//the pointer penetrates into a to fetch the value and stores it in b;
printf("%d",b);
output:10
Because you can get value stored at any address only through *.& is used to retrieve the address value.!
"." is used to access elements of a class via an object of class
eg-
class cls
{
public int a;
//some other work in this class
}
void main()
{
cls cls1=new cls();
cls1.a=20;//using "." operator here
}
&(ampersand) = points the address of the variable declared
*(as-trick) = points the value of the variable declared.
->(spangles) = accesses the pointer variable that contains the address of another variable.
Note:
". " : used to access structure members with normal structure variable.
"->" : used to access structure members with pointer to structure.
"*" : used to dereference a pointer.
"&" : used to obtain address of variable.
It is Null Pointer because, Pointer stores the addresses of another variable but when variable type is not known then it will be (void) and to store address it is consist(*) also,so (void *) and initially it points to 0th address, Means Null.
The main difference between NULL pointer and Void pointer is, Null pointer doesn't represent any memory location.Its value is 0. But void pointer can represents to memory location of its own type.
In the example (Void*)0(zero). it means a pointer type void represents to the location of zero.so its NULL(as mentioned 0 in the example) so syntax for the NULL pointer can be used as (Void*)0
This is the definition for VOID pointer. we can assign this to any data type.
Example:
#include
int main()
{
int a;
a=(void*)0;
printf("%d",a);
return 0;
}
Can you combine the following two statements into one? |
Prototype of malloc is
ptr = (data type *)malloc(size);
- where ptr is pointer of type datatype.
So in the above example as we need to allocate memory for char it can be done with the following statement:
char *p = (char*)malloc(1000); // here p is a pointer of type char
near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long.
The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.
If we want to refer one dimensional we just use single pointer ie like *p, if we need to refer two dimensional we have to use double pointer reference ie *(*p),,if we have to refer 4 at a time just use 4 pointer statement i.e. *(*(*(*(p))))and so on.....
ptr[0]------>*(ptr+0);
ptr[1]------>*(ptr+1);
Like that:
a[i]---->*(a+i);
a[i][j]---->*(*(a+i)+j);
a[i][j][k]---->*(*(*(a+i)+j)+k);
32 bit compiler will take 2 bytes to store address of integer.
Comment on the following pointer declaration? |
ptr is a pointer to integer, p is not