linux c二级指针的内存分配和使用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    /* 这个是给str分配存储字符串地址的空间 */
    char **str = (char**)malloc(sizeof(char*)*256);
    /* 这个是给str分配str中的地址所指向的字符串存储空间的大小 */
    *str = (char*)malloc(sizeof(char)*256);
    /* 备份二级指针的首地址 */
    char **strbak = str;

    *str = "aaa";
    /* 打印地址 */
    printf ("addr-str=%p ", str);
    /* 打印aaa*/
    printf ("*str=%s ", *str++);

    *str = "bbb";
    printf ("addr-str=%p ", str);
    printf ("*str=%s ", *str++);

    *str = "ccc";
    printf ("addr-str=%p ", str);
    printf ("*str=%s ", *str++);

     *str = malloc(32);
    printf ("*str=%s ", *str);
    /* 直接拷贝会报段错误 */

  strcpy (*str, "ddd");
    printf ("sizeof *str=%d ", sizeof(*str));
    printf ("addr-str=%p ", str);
    printf ("*str=%s ", *str++);

    int i;
    for (i=0; i<4; i++)
    {
        printf ("*strbak=%s ", *strbak++);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/etangyushan/p/3728770.html