C Pointer Explanation

A.Understand of Operations
1. Operation &
-The operation & to get the memory location value of a variable. The location where a variable is stored.
-The operation $ can use for both pointer and non-pointer variables
For example:
int a=5;
printf(“a is located at $%x.\n”, &a);//This will print the hex value of location where a is stored
int *p;
printf(“p is located at $%p.\n”, &p);//This will print the hex value of location where p is stored
printf(“p is pointed at which location $%p.\n”, p);//This will print the hex value of where p is pointed to. We will explain detail later.

2. Operation *
-The operation * to get the “surface value” of the memory location that pointer is pointing to
-The operation * can use for pointer only
For example:
int a=5;
int *p;
p = &a; //This will let pointer p to point to the memory location of var a
printf(“a is %d.\n”, a);//Will print “a is 5”
printf(“p has the value %d.\n”, *p);//Will print “p has the value 5”

3. Operation ->>
-This is the operation used in the struct object to iterate the values of struct object attributes
-We can learn about this at Struct and Pointer

B. C Pointer Explaination
1. Declare a pointer
int *p;
-A pointer will be declared with a datatype and the syntax as above
-The datatype will indicate that which datatype variable that a pointer can point to

2. What members a pointer have?
-A hex memory value for the location where this pointer variable is STORED (p).
-A hex memory value for the location where this pointer variable is POINTING TO (&p).
-A value at the location where this pointer variable is POINTING TO (*p).

3 Point Pointer to an Address Location
int a=5;
p = &a;//Pointer will point to the memory location of a

C. Why we C Language Need Pointer?
-Java is Pass By Value language (some cases pass by reference). But, C is Pass By Reference language
-Pointer is a mechanism to let C can have a Pass by Value. We can point a pointer to a memory location to get the “surface value” to use without effect to the origin value. For example:
int a=5;
int *p;
p = &a;
int b=0;
b = increase(*p);//b=6; a=5
b = increase(a);//b=6; a=6
Assume that the increase method will increase the int by 1.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.