A: Imagine that we have an int called i. Its address could be represented by the symbol &i. If the pointer is to be stored as a variable, it should be stored like this.
int *pi = &i;
Two step process will be
int *pi;
pi = &i;
2) How this program is interpreted by the C compiler?
1:#include 2:int main()
3:{
4: float fl=3.14;
5: std::cout <<>grabs the address reserved for fl.
b) The contents stored at that address are retrieved.
To generalize, whenever any variable is accessed, the above two distinct steps occur to retrieve the contents of the variable.
3) Can we seperate the above two steps in C?
A: Two operators are provided that, when used, cause these two steps to occur separately.
operator meaning example
& do only step 1 on a variable &fl
* do step 2 on a number(address) *some_num
Try this code to see what prints out:
1: #include
2: int main()
3: {
4: float fl=3.14;
5: printf("fl's address=%u\n", (unsigned int) &fl);
6: return 0;
7: }
On line (5) of the example, The & operator is being used on fl. On line (5), only step 1 is being performed on a variable:
1. The program finds and grabs the address reserved for fl...
It is fl's address that is printed to the screen. If the & operator had not been placed in front of fl, then step 2 would have occurred as well, and 3.14 would have been printed to the screen.
Now let's test the other operator, the * operator that retrieves the contents stored at an address:
1: #include
2: int main()
3: {
4: float fl=3.14;
5: unsigned int addr=(unsigned int) &fl;
6: printf("fl's address=%u\n", addr);
7: printf("addr's contents=%.2f\n", * (float*) addr);
8: return 0;
9: }
In line (7), step 2 has been performed on a number:
2. The contents stored at that address [addr] are retrieved.
4) What are the uses of pointers?
A: 1) First, they are used to create dynamic data structures: data structures built up from blocks of memory allocated from the heap at run-time. This is the only visible way that Pascal uses pointers.
2) Second, C uses pointers to handle variable parameters passed to functions.
Normally functions in C and C++ are used by pass by value. Value of the variables passed through the function cannot be modified from inside the function. Any modification inside the function doesn't effect the variable that is passed through the function.
If we want the modifications inside the funtion to reflect in the main program we call the funtion with variables by pass by reference. This pass by value behavior and change values passed into functions can be done by using the & and * operators
1: #include
2: void somefunc(unsigned int fptr)
3: {
4: *(float*)fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: unsigned int addr=(unsigned int) &fl;
11: somefunc(addr);
12: printf("%.2f\n", fl);
13: return 0;
14: }
Quite simply, the two steps that normally occur when accessing a variable are being separated to allow us to change the variable's value in a different function.
1: The floating point variable fl is created at line (9) and given the value 3.14
2: The & operator is used on fl at line (10) (do only step 1, get the address). The address is stored in the integer variable addr.
3: The function somefunc is called at line (11) and fl's address is passed as an argument.
4: The function somefunc begins at line (2), fptr is created and fl's address is copied into fptr.
5: The * operator is used on fptr at line (4) -- do step 2, the contents stored in an address are retrieved.
6: The contents at this address are assigned the value 99.9.
7: The function finishes. Control returns to line (12).
8: The contents of fl are printed to the screen.
Putting it all together we have the following program.
1: #include
2: void somefunc(float* fptr)
3: {
4: *fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: float* addr = &fl;
11: somefunc(addr);
12: printf("%.2f\n", fl);
13: return 0;
14: }
C uses pointers in three main ways.
3) And third, pointers in C provide an alternative means of accessing information stored in arrays, which is especially valuable when you work with strings.