Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166279
  • 博文数量: 41
  • 博客积分: 647
  • 博客等级: 上士
  • 技术积分: 366
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-29 14:13
文章分类

全部博文(41)

文章存档

2013年(11)

2012年(1)

2011年(29)

分类: C/C++

2011-11-22 16:02:04

 dyli现在来看看字符数组与字符指针的联系与区别
 
1、请先看下面的代码
 
  1. /* Name : char_array.c
  2.  * Author : dyli
  3.  * Write Date : 20110807
  4.  * Description : Used to distinguish between an array of
  5.                     characters and the string pointer.
  6.  * */

  7. #include <stdio.h>

  8. int main(int argc, char *argv[])
  9. {

  10.     char dyli_array[15] = "isDyliArray";/* Define an array of characters,
  11.                                           pay attention to the format.*/

  12.     char* dyli_str_pointer = "isDyliStrPointer";/* Define a string pointer*/

  13.     /*Below you can see,the base Address of the character array can be obtained
  14.      in three:
  15.      1,&+an array name;
  16.      2,&+ the first element of array.
  17.      3,the array name.    */

  18.         /* Use "&" to get the address of dyli_array */
  19.     printf("&dyli_array is %x\n",&dyli_array);
  20.         /* Get firset Item address of dyli_array */
  21.     printf("&dyli_array[0] is %x\n",&dyli_array[0]);
  22.         /* Here,dyli_array is a array pointer,
  23.         it representative the address of dyli_array.*/
  24.     printf("dyli_array is %x\n",dyli_array);

  25.     /* Yes!You know an array of characters can be output like this. */    
  26.     printf("the array of dyli_array is = %s\n",dyli_array);    
  27.     printf("output the first element of dyli_array = %c\n",dyli_array[0]);

  28.     printf("/*****----------------------------------------------------------****/");

  29.     /*Below you can see,the string pointer Variable itselt has an Address.
  30.     */
  31.         /* the pointer Variable Address*/
  32.     printf("\n&dyli_str_pointer is %x\n",&dyli_str_pointer);
  33.         /* the string constant first element address.*/
  34.     printf("&dyli_str_pointer[0] is %x\n",&dyli_str_pointer[0]);
  35.         /* the BaseAddress of string constant*/
  36.     printf("dyli_str_pointer is %x\n",dyli_str_pointer);

  37.     printf("the string of dyli_str_pointer is = %s\n",dyli_str_pointer);
  38.     printf("output the first element of dyli_str_pointer point to \n\
  39.          string constant = %c\n",dyli_str_pointer[0]);
  40.     getchar();
  41.     return 0;
  42. }
 
2、运行结果
 
  1. [root@localhost char_array]# ./char_array
  2. &dyli_array is bfe04d95
  3. &dyli_array[0] is bfe04d95
  4. dyli_array is bfe04d95
  5. the array of dyli_array is = isDyliArray
  6. output the first element of dyli_array = i
  7. /*****----------------------------------------------------------****/
  8. &dyli_str_pointer is bfe04d90
  9. &dyli_str_pointer[0] is 80485e4
  10. dyli_str_pointer is 80485e4
  11. the string of dyli_str_pointer is = isDyliStrPointer
  12. output the first element of dyli_str_pointer point to
  13.       string constant = i
 
3、再看看这两者在内存里面的怎样分布的
 
(1)、char dyli_array[15] = "isDaliArray";
 
 
 
 
 
(2)、char* dyli_str_pointer = "isDyliStrPointer";
 
 
 
 
  dyli_str_pointer这是一个字符指针变量,变量就会有空间,也会有地址。

  这个指针变量地址可以用&dyli_str_pointer来取;它的空间里面存放的是字符串常量的地址,因为该常量的地址已经赋值dyli_str_pointer变量,故可以直接用dyli_str_pointer来取得
 
  而这个dyli_str_pointer所指向的字符串常量的元素,也可以用dyli_str_pointer[0],dyli_str_pointer[1].....dyli_str_pointer[*]号取得!!
阅读(1398) | 评论(0) | 转发(0) |
0

上一篇:字符串操作大全

下一篇:回调函数

给主人留下些什么吧!~~