zoukankan      html  css  js  c++  java
  • initial pointer [expert c]

       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".

  • 相关阅读:
    DB2数据库基础
    mysqldump 备份数据库脚本
    Windows Server 2012 R2 卸载IE浏览器
    mysqlpump 和 mysql_config_editor测试
    Linux 修改时区
    PowerShe 消息提示框测试
    Python使用libsvm的“ImportError: No module named svmutil”问题
    常见的机器学习&数据挖掘知识点
    [猜你喜欢]冠军“yes,boy!”分享,含竞赛源代
    用户人品预测大赛获奖团队分享
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3283278.html
Copyright © 2011-2022 走看看