In C language , most of us will meet the pointer variable. The pointer represents a memory location which can be accessed by the ampersand (&) operator .If you want to get the value of this address, you can use the asterisk (*) operator . Some C programmers always said the pointer is the most difficult part during learning C language. The same to me. Sometime, I really confused by the pointer . Especially when I refer to a C book wrote by Chinese professor, some conceptions are really hard to understand.
When i meet a pointer's problem , usually i suppose the pointer variable as an address, and the asterisk pointer(like *p) as address's content. Now stop here,let me draw a schematic as below .
*0x?????? represents an address in the memory
From this schematic , we can declare a variable like this
int *pointer = (int *)0x??????00;
Here , the pointer points to the address of 0x??????00 ,i just take this pointer variable (declared pointer) as an address.This method is rear case in computer's program,but it's common in embedded device program.But here we don't detail this difference. Actually we often declare a pointer variable then initialize it with the same data type variable's address (We can use the ampersand (&)operator) like this.
int a = 10;//declare a integer variable and initialize it
int *pointer = &a;//declare a integer pointer variable then initialize it with a's address
Here i must emphasize that using a initialized variable to initialize the pointer variable.
Sometime we focus on not only the initialization pointer , but also the value which holds in this address. i can use the asterisk (*) to get the value, like below;
int a = 10;
int *pointer =&a;
int value = *pointer;// Actually value equals 10
Actually , if the pointer points to an unknown address, the value called garbage value, this value has nothing means. Some time this garbage value maybe cause a problem which is very difficult to find out when we debug ur program.
Before make clear the pointer ,we must know the memory allocation well. Because the pointer just is the memory address (that's my opinion). So just as the schematic showing below, memory is divided into stack section, heap section, global variables section , constants section and code section. Each section are loaded difference contents which are list below table.
*This schematic really depends on the OS.
Memory | Objects in memory |
Stack | This section is used to storage the local variables |
Heap | Dynamic memory is allocated when the program is running. |
G.Var. | A global variable is created here when the program first runs. |
Constant | Constants are stored this section (Read only) |
Code | The source code (machine code) (Read only) |
Here we go, if we make clear these memory allocation ,we may have found that "all are address". That's the first step, lately we will find we can get the address of a functions just like the variables .
How can we certificate this schematic , i list the program as below. ( gcc compiler with the linux OS). But now how can you get the address of a MACRO like the CONSTANT defined in the below code? That maybe an preprocessor problem. Just tell me if you get it.
/*
*This program is used to watch the memory allocation.
*
*Data Apr. 3 2014
*Author Johnny Han
*Email emailjohnnyhan@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#define CONSTANT 123
//Global Constants
static const int const_a = 2;
const int const_b = 3;
int const const_c = 4;
//Global variables
int global_int_var = 0;
char global_char_var = 0;
double global_db_var = 0;
float global_float_var = 0.0;
static void local_sub_func(int local_a, int local_b);
static void local_sub_func1(int *p,int *q);
int main(void)
{
//local variables
int local_i = 0;
int local_var = 0;
const int const_d = 5;
//Heap
int *p = malloc(5*sizeof(int));
int *q = p;
if(p == NULL)
{
printf("Allocate memory failed!");
exit(1);
}
printf("---------constant----------\n");
printf("const : %p\n",&const_a);
printf("const : %p\n",&const_b);
printf("const : %p\n",&const_c);
printf("----------------------------\n");
printf("---------global varible----------\n");
printf("global_int_var : %p\n",&global_int_var);
printf("glocal_char_var : %p\n",&global_char_var);
printf("glocal_db_var : %p\n",&global_db_var);
printf("glocal_float_var : %p\n",&global_float_var);
printf("----------------------------\n");
printf("--------- heap ----------\n");
for(local_i = 0; local_i < 5; local_i++)
{
*p = local_i + 1;
printf("heap %d : %p\n",local_i,p);
p++;
}//end for
free(q);
printf("-----------------------------\n");
printf("--------- stack----------\n");
printf("locai_i : %p\n",&local_i);
printf("locai_var : %p\n",&local_var);
local_sub_func(local_i,local_var);
local_sub_func1(&local_i,&local_var);
printf("----------------------------\n");
return 0;
}
static void local_sub_func(int local_a, int local_b)
{
printf("locai_a : %p\n",&local_a);
printf("locai_b : %p\n",&local_b);
}
static void local_sub_func1(int *p,int *q)
{
printf("locai_p : %p\n",p);
printf("locai_q : %p\n",q);
}
I'm so sorry that there is less comment in this code. But i think it's easy for u understand. Here I just declare some variables as the comments said. This program just print the address of each declared variable. We can use malloc() function to allocate a block of memory in the heap section. Surely ,don't forget to free this section after exit the program (sometime our OS helps me to free them when the program exit, but i think we should have a good manner to code).
The result of this code is as below.
From the printing result , we know that the memory allocation is the same as the above schematic showing.
This chapter, I just enlarge upon some statements the "C column Pointer<0>" , so i named it Pointer<0.9>. There maybe something wrong about the spelling, the sentences or even the grammar.Please forgive me, i just the newer.But i try my best to express the C language as correctly as possible.
Thankyou! Have a nice day!