Important Concept of Pointer
#include
void main ()
{
char i=’a’;
//case 1
char *j;
j=&i;
//case 2
//char *j=&i; this one is correct, which is shorthand of case 1;
//char *j=i; this one is wrong, because j is address, but i is char.
#include
void main ()
{
char i=’a’;
//case 1
char *j;
j=&i;
//case 2
//char *j=&i; this one is correct, which is shorthand of case 1;
//char *j=i; this one is wrong, because j is address, but i is char.
//case 3
//char *j;
//*j=&i; //wrong!!! *j is char, but &i is address of ‘a’
// also, you need to define the pointer(give the address to it) before you can use it.
printf(“j= %cn”, *j);
*j=’y’;
printf(“i was changed to %c”,i);
}
Pointer in Array
Whe the pointer is pointed to the begining address of a array, we can use pointer the way as array.
#include
main ()
{
int ary[4]={2,3,4,5};
int *i;
int j;
//i=&ary[0]; we also can say i=ary; //or i=&ary;
for (j=3; j>=0; j-)
//printf(“%3d”,*(i+j));
// printf (“%3d”, i[j]);
// printf (“%3d”, ary[i]);
printf (“%3d”, *(ary+j));