#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\n", str);
    /* 打印aaa*/
    printf ("*str=%s\n", *str++);

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

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

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

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

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-22
  • 2021-09-26
猜你喜欢
  • 2021-12-17
  • 2021-12-17
  • 2021-07-26
  • 2021-12-05
  • 2021-10-03
  • 2022-02-21
相关资源
相似解决方案