First, I wanna say nobody wanna do the homework for somebody else, when this person is lazy to try to do them.
So, I won't do your work. I wanna just to try to explain the work with pointers.....
For Further information, read the www.cprogramming.com tutorials.....
pointers
===================
declaration syntax:
Datatype *pointervariable
Example:
+++++++++++++
int x=5;
address: 0000:1234
name: x
value: 5
code:
Code:
#include <stdio.h>
int main()
{
int x=5;
printf("The address of x is %p \n",&x);
return 0;
}
Something about addresses.......
code:
Code:
#include <stdio.h>
int main()
{
int check;
int chapter1 = 5;
int chapter2 = 60;
int chapter3 = 166;
int *data;
do{
printf("\tChapter overview\n");
printf("\t*******************************\n\n");
printf("\t-1- Chapter 1\n");
printf("\t-2- Chapter 2\n");
printf("\t-3- Chapter 3\n");
printf("\t-4- End\n");
printf("\n");
printf("\tOption: ");
scanf("%d",&check);
printf("\tYou find chapter %d on ",check);
switch(check)
{
case 1 : data=&chapter1;
printf("page %d\n",*data);
break;
case 2 : data=&chapter2;
printf("page %d\n",*data);
break;
case 3 : data=&chapter3;
printf("page %d\n",*data);
break;
default: printf("page ???\n");
break;
}
}
while(check<4);
return 0;
}
This is another example:
Code:
#include <stdio.h>
int main()
{
int x=5;
int *y;
printf("address x=%p, value x=%d\n",&x,x);
printf("address *y=%p, value *y=%d(crap)\n",&y,*y);
printf("\ny=&x;\n\n");
y=&x; /*y has now the address of x */
printf("address x=%p, value x=%d\n",&x,x);
printf("address *y=%p, value *y=%d\n",&y,*y);
printf("\ny points to this address %p\n",y);
printf("this is the address of x = %p\n",&x);
printf("\nWATCH OUT!!!\n\n");
*y=10;
printf("*y=10\n\n");
printf("address x=%p, value x=%d\n",&x,x);
printf("address *y=%p, value *y=%d\n",&y,*y);
printf("\nypoints to this address %p\n",y);
printf("it is still the address of x (%p)\n",&x);
return 0;
}
The next example is imporatant. And never use such crap, it coul be very dangerous:
Code:
#include <stdio.h>
int main()
{
int *y;
*y=10;
printf("the value of *y is %d\n",*y);
return 0;
}
This is dangerous, because y points to a not defined address..........
you could try to prevent such mistakes, when u use:
Code:
#define NULL (void *)0
example:
Code:
#include <stdio.h>
int main()
{
int *y=NULL;
if(y==NULL)
{
printf("The pointer has an illegal/not defined address\n");
}
else
{
*y=10;
}
return 0;
}
Some other things to know:
Code:
int *ptr; /* declaration */
int var;
ptr=&var; /* initialistion : ptr get the address of var */
*ptr=100; /* dereferenciation: var get the value 100 */
*ptr+=100; /* dereferenciation: we add 100 to var */
*ptr++; /* dereferenciation: var has now the value of 201 */
*ptr--; /* var has now the value of 200 again */
printf("%d",*ptr); /* outputs the value of var */
printf("%p",&ptr); /* outputs the address of ptr */
printf("%p",ptr) /* outputs the address of var */