Hello merry christmas !!!
Code:
#include<stdio.h>
int main()
{
int *piVPtr=NULL;
int *piVPtr2=NULL;
int aiV[5] = { 10, 20, 30, 40, 50 };
int iTemp;
int *piP, *piQ;
piVPtr = aiV;
printf("Address of piVPtr : %p Contents of piVPtr : %p\n", &piVPtr, piVPtr );
printf("Address of aiV[0] : %p\n", &aiV );
piVPtr +=2;
printf("Address of piVPtr +2 : %p\n", piVPtr );
piVPtr +=2;
printf("Address of piVPtr +4 : %p\n", piVPtr );
piVPtr2 = &aiV[2];
piVPtr = &aiV[0];
iTemp = piVPtr2 - piVPtr;
printf("Content of iTemp : %d\n", iTemp);
piP = piQ;
printf("Content of piP : %p piQ : %p\n", piP , piQ);
return 0;
}
Here is what you want to have without compiler warnings. So this code is not suitable though.
You really need some key points in order to use pointers with the good way. First of all you don't initialize the pointer piQ
assuming the line :
Code:
piP = piQ;
printf("Content of piP : %p piQ : %p\n", piP , piQ);
Where both piQ and piP do you think that they point? THe answer is you don't. That is dangerous you must initialize a pointer with a valid address before you use it.
The second one is that you should use %p instead of %d when you want to print an address for example if you have a pointer like:
now p points to x... and *p is an alias for x since *p=x=1 . But p is not an integer value is an address of that integer value.
The third one is.... when you try to write a program you really need to give good names in the variables.... for example what is piVptr and aiV names? I can't understand with the first sight
for now it is not bad... but if you have a program which implements an algorithm you really give suitable names to variables.
For example arr for 1D array and arr2d for a 2D array
Mr.Lnx