A lot of beginning programers consider that pointers are difficult to use. But there is nothing conceptually challenging in understanding pointers. It is true that careless uses of pointers (or memory management) can cause head-aches in debugging. But pointers are useful and powerful tools.
int x = 111;
float y = 0.23;
Let's think about the departmental mail boxes, each of you have your own box. In addition to individual boxes, there may be a box for the ``organizer of biobites seminar series''. Inside of this box, there is a piece of paper, saying who is the current organizer. So the person who is distributing the mails will take a look at this paper, and put the thank-you letter for the organizer to the person's mailbox. The piece of paper in the organizer's box was a pointer to another mailbox. Pointer variables have this kind of functionality.
int *xPtr;
Notice ``*''. This is saying xPtr is a variable, which
stores the address of int variable.
You should associate * with int, and read this as xPtr
is a type (int *).
If you want to declare 2 pointer variables and 1 simple variable:
double *ptr1, y, *ptr2;
int x = 111;
xPtr = &x;
``&'' means ``address-of''. So in this case address of x is created by ``&x'' (in our case ``3''), this value is stored in the space allocated for the pointer variable (xPtr)
printf("%d\n", *xPtr);In this context, ``*'' means ``contents-of''. This statement will print ``111''.
*xPtr = 6;
Now the value of x become 6.
\
n", i, *ptr1);
#include <stdio.h> int main (void) { int *ptr; *ptr = 11; printf ("%d\n", *ptr); }