转自:jianchi88
http://blog.csdn.net/jianchi88/article/details/6876405
#include<stdio.h>
int main()
{
char a[] = "hello world1";
char *p = "hello world2";
*(a+)='q';
*(p+)='q';
printf("%s\n",a);
printf("%s\n",p);
}

运行结果

段错误。

注释 *(p+1)='q';

运行结果

hqllo world1

hello world2

原因:

一、a与p类型不同:p为指向字符串的指针;a为保存字符串的数组。

   char a[] = "hello world1";   是个赋初值的字符数组。
  char *p = "hello world2";  是个字符串常量指针;

指针变量p在栈里面

字符串常量"hello world2"在全局数据区,数据段,只读,不可写

二、"hello world2"和"hello world1"字符串保存的位置不同。"hello world1"保存在栈
    中,可用*(a+1)='q'修改,"hello world2"保存在全局数据
    区,位置是在.rodata中,不能修改*(p+1)='q'

三、只有2中情况下,数组名不可等同视为数组指针,&与sizeof操作符
    “&”的区别:
    &p取得是指针变量的地址,而非字符串"hello world2"的首地址,p指向字符串的首地址;&a取得
    就是字符串"hello"的首地址。
    “siziof”的区别:
    下面假设在ia32平台上运行,保存变量的内存空间不同。
    sizeof(p); // return 4, pointer size
    sizeof(a); // return 13, array size

               char a[] = "hello world1";
(gdb) p a
$ = "\000<\006޿�\005޿�?�"//空
(gdb) s
char *p = "hello world2";
(gdb) p a
$ = "hello world1"
(gdb) p &a
$ = (char (*)[]) 0xbfde0583 //在栈
(gdb) s
*(a+)='q';
(gdb) p p
$ = 0x80484c0 "hello world2" //在数据段
(gdb) p &p
$ = (char **) 0xbfde0590 //在栈

————————————————————————————————————————————————————————————————————————————————————————————

面试题知识点:

  当指针赋值给相同的常量字符串时,它们实际上会指向相同的内存地址,但是常量内存初始化数组,情况确不一样。

char str1[] = "hello world";
char str2[] = "hello world";
string* str3 = "hello world";
string* str4 = "hello world";
问str1 == str2是否成立?str3 == str4是否成立?
(提示,前一个不成立,后一个成立)
    char a[] = "hello world";
char b[] = "hello world";
char *str1="hello world";
char *str2="hello world"; 前两个不同,后两个相同,即a!=b;str1==str2;

char a[] = "hello world1"和char *p = "hello world2";的区别(转)的更多相关文章

  1. 理解extern char s[100]与extern char *s

    在x.c中定义了一个字符数组 char s[100],在l.c中进行引用extern char s[200], 有些c程序新手经常把它写成extern char *s. 这两种写法的含义一样吗? 首先 ...

  2. char * p = "abc"与const char *p = "abc"

    char * p = "abc"与const char *p = "abc"的区别是什么呢? 第一个语句会产生问题: warning: deprecated c ...

  3. char、varchar、nchar、nvarchar、text的区别

    char.varchar.nchar.nvarchar.text的区别 1.有var前缀的,表示是实际存储空间是变长的,varchar,nvarchar 所谓定长就是长度固定的,当输入的数据长度没有达 ...

  4. 20130324 LBP CSLBP 全局存储区 局部存储区 char c[]=”hello world”和char *str=”hello world”的区别

    1.LBP and CSLBP 2.再论char c[]=”hello world”和char *str=”hello world”的区别 /**************代码1************ ...

  5. char s[]字串和char *s字串有什麼区别?

    C語言有兩種字串宣告方式char s[]和char *s,兩者有什麼差異呢? Introduction char s[] = "Hello World"; (只是用字符串常量初始化 ...

  6. C语言char*字符串数组和unsigned char[]数组的相互转换

    #include <iostream> #include <string> using namespace std; void convertUnCharToStr(char* ...

  7. Delphi7中的Char和XE中的Char

    我用FillChar()函数时,发现两个版本中的Char不一样. 在delphi7中 procedure TForm2.Button1Click(Sender: TObject); var s: ar ...

  8. char * const p和const char *p的区别

    1. 前者定义P为常量,即只能单向赋值一次,P++展开为p=p+1,重复赋值给常量,出错,后者P为地址变量,地址变量是指向该变量的存储地址值如:4B3F6A,不能赋给一个字符值(字符相当于ascii表 ...

  9. C/C++中char* p = "hello" 和 const char* p = "hello"的区别

    在写代码常常都会写char * p ="hello";这样的代码,虽然不是错误,但却不建议这样用.应该加const修饰.这句话背后的内涵是什么?下面就刨根问底一下:) 这个行为在不 ...

随机推荐

  1. Server Tomcat v9.0 Server at localhost failed to start.

    最近老是出现这样的问题,在网上找了很多方法都不行,试着把Tomcat重新配置了一下就好了,事后找到一个博客,试了一下也可以使用

  2. java代码-----------逻辑运算符、 &&逻辑与 ||或

    总结: && :两者均满足.是true ||: 两者中有一个满足就为true,不然就是false package com.sads; public class shou { publi ...

  3. MariaDB主从半同步复制详解

    半同步复制(Semisynchronous replication) 介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到relay lo ...

  4. json data 解析demo

    json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...

  5. hdu-1052-Tian Ji -- The Horse Racing(经典)

    /* hdu-1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...

  6. B. T-primes

    /* PROBLEMSSUBMITSTATUSSTANDINGSCUSTOM TEST B. T-primes time limit per test2 seconds memory limit pe ...

  7. 优化笔记: jxrsfxrxx_D_20140916.gz

    表的重复扫描.   ----------------------------------想进一步研究,继续往下看------------------------------ 1.       所有相似 ...

  8. 初识python(python的安装与运行)

    python--“优雅”.“明确”.“简单”的哲学定位 一.python的安装(Windows环境下) 1.在python官网下载安装文件 python的官方网址:https://www.python ...

  9. swoole学习

    <?php ini_set('default_socket_timeout', -1); class serverEmail { public $serv = null; public func ...

  10. Tkinter tkMessageBox

            Tkinter tkMessageBox: tkMessageBox模块用于显示在您的应用程序的消息框.此模块提供了一个功能,您可以用它来显示适当的消息  tkMessageBox模块 ...