Pointers are what? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Pointers are what?

int doses[]= {1,3,2,1000}; printf("Issue doses %d", 3[doses]); //outputs Issue doses 1000. Explain this- doses[3]==*(doses+3)==*(3+doses)== 3[doses] that last one specially.

8th Apr 2018, 5:36 AM
Akib
Akib - avatar
4 Answers
+ 2
Pointers are special variables that holds the memory address of other variables. So, we can access the value stored in the variable using the pointer which holds the address of that variable. Pointers are declared with asterisk (*) operator. for example: int a=2; int *p; //p is a pointer which will holds the address of an integer variable. So, how to access the value in the memory pointed by this pointer? Answer is by dereferencing this pointer. printf("%d",*p); //* is the dereference operator //This program will crash because p is not pointing to any valid memory address. So, how we made it point to a valid address? Answer is using reference (&) operator. p=&a; // & operator returns the address of the variable a. Now, if we try to dereference the pointer, we'll get value stored in variable a. printf("%d",*p); will give us 2. There are pointer variables for each data type (primitive or user defined). You have to mention the data type which it will point to during declaration. Example: float *p; double *p; etc. Now, the question is why do we need pointers if we can access the memory just by variable name? There are many applications of pointers. Some of them are: 1) To modify/access a local variable of a function in another function. For this you need to know about the scope and life of variables which i am assuming you are aware of. Scope of a variable is local to the function in which it is declared. So, to access and change the value of the variable in different function we need pointers. 2) For dynamic (run time) memory allocation. For your main question, you need to know about pointer arithmetics and arrays & pointers.
8th Apr 2018, 6:22 AM
Nikhil Sharma
Nikhil Sharma - avatar
+ 1
Pointer arithmetics: If p is a integer pointer variable pointing to memory address 1024, then what will be the result of p++? printf("%d",p) // 1024 printf ("%d",p+1) // 1028 It gives 1028 because integer takes 4 bytes so p+1 gives the immediate next location of the integer variable. It may vary according to the size of each data type
8th Apr 2018, 6:24 AM
Nikhil Sharma
Nikhil Sharma - avatar
+ 1
Now, for second part, you need to know that name of the array is a constant pointer (constant pointer and pointer to constant is another whole topic). That means, In int arr[3]={1,2,3}; arr is an integer pointer variable (but you can't do arr++ because it is a constant pointer). so, *arr(dereferencing arr) will give you 1. arr+1 will point to next integer and *(arr+1) will give you value 2. arr[i]=*(arr+i) And, arr[i]=*(arr+i)=*(i+arr)=i[arr] Hope it helps..
8th Apr 2018, 6:25 AM
Nikhil Sharma
Nikhil Sharma - avatar
0
thanks for that ton of knowledge but i just needed to know about last bit" variable[array_name]" *i know what pointer is. Just the last part i don't get.
8th Apr 2018, 6:35 AM
Akib
Akib - avatar