C language memory management & pointer & string & array

Preposition_Question:

//指针
int * pz;

//数组
int zippo[4] = { 1, 2, 3, 4};

int zippo[4][2];

//指针和数组

int (*pz)[2];

int *pa[2];

//字符串和数组
char * heart = "I love c language!";
char head[] = "I love c language!";

//字符串和二维数组
const char * mytalents[5];
char yourtalents[5][40];

If you are vague about the above code, then this article will take you to clarify the relationship between them.

=======================================================

C language memory management

To understand the above code, C language memory management knowledge is a point of knowledge that cannot be overcome:

C language memory management

In summary, we know:

Initialized/uninitialized global variables and static variables are respectively stored in the bss section and data section of the static storage area. Uninitialized parameters will be cleared when the program starts

Therefore, all parameters in the static storage area will be assigned;

Character literals are stored in the constant area of ​​the static storage area; variables are stored in the stack area;

 

#include <stdio.h>

int main(void)
{
    int * pz;    //在函数里面定义就会被存放在栈区,程序运行时分配    

    int zippo[4] = { 1, 2, 3, 4};    //一维数组,存放在栈区,程序运行时分配;

    int zippo[4][2];        //二维数组,存放在栈区,程序运行时分配;
                            //从左往右的优先级,(1)先是一个内涵4个元素的整型数组
                            //(2)每个元素又包含两个整型元素

    //数组指针    主语是指针,数组是修饰语;    指针指向数组(指向数组的指针)

    int (*pz)[2];        //注意优先级:先算括号的,(1)pz首先是一个指向整型数的指针
                        //(2) 再跟[2]结合,表示指向的是一个内涵2个元素的数组

    pz = zippo;    //不会报错,pz就是指向内涵两个元素的指针
    
    //指针数组    主语是数组,指针是修饰语,数组里保存指针
    int *pa[2];    //这种形式表示:pa先和[2]结合,表示是一个内涵两个元素的数组,
                    //再和*结合,表示数组存放整型指针

    int a = 10;    //栈区
    int b = 11;
    int *pz2[2];    //这是一个内涵两个元素的数组,数组中保存着整型的指针

    pz2[0] = &a;    //数组
    pz2[1] = &b;
    printf("%d\n", *pz2[0]);
    printf("%d\n", *pz2[1]);
   

    //指针形式的字符串
    char * heart = "I love c language!";    //很重要的一点
    //首先,"I love c language!"是字符串常量,保存在静态存储区的常量区
    //而heart是指针变量,存储在栈区(地址很高),它的值是字符串常量的地址,(很低的位置)
    
    /***重要***/
    //数组形式的字符串
    char head[] = "I love c language!";
    //(1)字符串存储在静态存储区,程序启动,将字符串载入到内存中
    //(2)程序运行时,才会给数组分配内存,将静态存储区的字符串拷贝到栈区的数组中,实际上用的是一个副本
    //(3)此后编译器,便把数组名head识别位数组的首元素(&head[0])的别名,这里的head是地址常量,不可更改,(++head是不允许的)


    //字符串和二维数组
    //指针数组
    const char * mytalents[5];    //mytalents数组是一个内涵5个指针的数组,占用5*8=40个字                                    
                                  //节,    因为里面存储的是指针
    char yourtalents[5][40];        //yourtalents数组是内涵5个数组的数组,每个数组内涵40个 
                                    //char类型的值,共占用200字节

}
//指针数组:保存指针的数组

#include <stdio.h>
 
const int MAX = 3;
 
int main ()
{
   int  var[] = {10, 100, 200};
   int i, *ptr[MAX];
 
   for ( i = 0; i < MAX; i++)
   {
      ptr[i] = &var[i]; /* 赋值为整数的地址 */
   }
   for ( i = 0; i < MAX; i++)
   {
      printf("Value of var[%d] = %d\n", i, *ptr[i] );
   }
   return 0;
}

 

 

 

 

Guess you like

Origin blog.csdn.net/Vast_Wang/article/details/107747059