initial differece between pointer and array

Both arrays and pointers can be initialized with a literal string in their definition. Although these cases look the same, different things are happening. A pointer definition does not allocate space for what's pointed at, only for the pointer, except when assigned a literal string. For example, the definition below also creates the string literal:

char *p = "breadfruit";
Note that this only works for a string literal. You can't expect to allocate space for, for example, a float literal:
                                   float *pip = 3.141;     /* Bzzt! won't compile */

A string literal created by a pointer initialization is defined as read-only in ANSI C; the program will  exhibit undefined behavior if it tries to change the literal by writing through p. Some implementations put string literals in the text segment, where they will be protected with read-only permission.

eg:          char *s = "hello world";

*s = 'w';

would generate an error when running the program.

An array can also be initialized with a string literal:
                          char a[] = "gooseberry";
     In contrast to a pointer, an array initialized by a literal string is writable. The individual characters can later be changed. The following statement:

strncpy(a, "black", 5);
gives the string in the array the new value "blackberry".

initial pointer [expert c]的更多相关文章

  1. Impala SQL 语言元素(翻译)[转载]

    原 Impala SQL 语言元素(翻译) 本文来源于http://my.oschina.net/weiqingbin/blog/189413#OSC_h2_2 摘要 http://www.cloud ...

  2. Impala SQL 语言元素(翻译)

    摘要: http://www.cloudera.com/content/cloudera-content/cloudera-docs/Impala/latest/Installing-and-Usin ...

  3. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

  4. c pointer and array

    Pointer:  A pointer is a variable that contains the address of a variable. if c is a char and p is a ...

  5. C pointer again …

    记录一个比较基础的东东…… C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

  6. C lang:Pointer operation

    Xx_Pointer opteration Do not dereference initialized Pointers Ax_Code #include<stdio.h> int ma ...

  7. SQL SERVER全面优化-------Expert for SQL Server 诊断系列

    现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...

  8. SQLite Expert Professional 3查看SQLite数据

    通常在android进行SQLite数据库的处理查看很不方便,于是自己下载了一个SQLite Expert Professional 3可视化工具用来进行查询数据,由于时间问题就不多说了,直接讲使用方 ...

  9. 苹果手机不支持click文字 需要添加 cursor:pointer 才能 识别可以点击

    给一个div 绑定一个 click事件,  苹果手机会识别不了,必须添加一个 cursor:pointer 才能 识别可以点击.安卓正常识别.

随机推荐

  1. Linux 计算器

    bc: 默认没有小数位,使用scale=2设置保留的位数.

  2. hdu 5612 Baby Ming and Matrix games(dfs暴力)

    Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...

  3. keil将程序装入外部FLASH具体解释

    在实际项目中,常常出现芯片的内部FLASH空间不够的情况,这就须要将程序分一部分装到外部FLASH中. 为了让大家能少走些弯路,在这里把我在这当中遇到的一些问题和经验教训给大家分享一下. 仅供參考,假 ...

  4. [R语言画图]气泡图symbols

    绘制气泡图主要使用函数symbols(x,y,circle=r).当中x.y是坐标轴,r是每一个点的半径. x<-rnorm(10) y<-rnorm(10) r<-abs(rnor ...

  5. 解决Xcode6.4安装插件后插件不能使用的问题

    下面是上网查的方法,综合了一下,亲测 原因: 苹果要求加入UUID证书从而保证插件的稳定性. 解决方法: 一.查看Xcode的UUID 在终端执行 defaults read /Application ...

  6. 移动前端开发之 viewport 的深入理解

    移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或响 ...

  7. 自制获取data-自定义属性

    jQuery.fn.dataset = function(attr, val) { // 获取数据集 if (arguments.length == 0) { var dataset = {}; jQ ...

  8. win7 AnkhSVN 安装报错

    重装系统后,需要安装AnkhSVN,结果一直报如下错误 An error occurred during the installation of assembly"Microsoft.VC8 ...

  9. 常用的方法,读取XML节点并赋值给List集合

    一.前言 很多时候也可以直接在XML文件中配置好节点,在程序需要用到的时候,修改XML文件并不需要重新编译,这里是在极光推送中拿出来的一部分代码.代码简单,大家直接看例子吧. 二.实现过程 1.新创建 ...

  10. Python学习笔记6(列表生成式)

    1.生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3 ...