zoukankan      html  css  js  c++  java
  • 字符数组与与指针保存字符串区别

    #include <stdio.h>
    int main(int argc, char **argv)
    {
            char str[] = "gyu";
            //charr*str = "gg"
            printf("str address is %p
    ", str);
            fun(&str);
            printf("main -- %s p is %p
    ", str, (unsigned int *)str);
            fun1(&str);
            printf("-----%s
    ", *(argv+2));
            printf("main -- %s
    ", str);
    }
    
    int fun(char **str)
    {
            *str = "1234";
            printf("FILE %s---LINE %d---%s--p is %p
    ",__FILE__,__LINE__,*str, *str);
    }
    
    int fun1(char **str)
    {
                    *str = "kkkk";
                    printf("---%s
    ",*str);
                    printf("FILE %s---LINE %d---
    ",__FILE__,__LINE__);
    }

    所示程序,若指针指向一个字符串,这个字符串是保存在数据段常量区的,是不可以修改的,不能strcat使用这个指针。但我们可以让这个指针指向其他的字符串。

    但是所示数组保存字符串的话,是存在栈区的,数组又是常量指针,即数组的这地址是不可以修改的,所以上面程序不会修改字符串的值。

  • 相关阅读:
    Django部署到服务器
    springboot使用Redis缓存
    ubuntu下pip更换国内源
    ubuntu环境变量文件
    python open找不到路径
    centos 8 安装nginx
    centos8 mysql8的远程访问
    centos 8 安装mysql-server 8
    今日收获
    今日收获
  • 原文地址:https://www.cnblogs.com/mcy0808/p/7400114.html
Copyright © 2011-2022 走看看