1、请先看下面的代码
- /* Name : char_array.c
- * Author : dyli
- * Write Date : 20110807
- * Description : Used to distinguish between an array of
- characters and the string pointer.
- * */
- #include <stdio.h>
- int main(int argc, char *argv[])
- {
- char dyli_array[15] = "isDyliArray";/* Define an array of characters,
- pay attention to the format.*/
- char* dyli_str_pointer = "isDyliStrPointer";/* Define a string pointer*/
- /*Below you can see,the base Address of the character array can be obtained
- in three:
- 1,&+an array name;
- 2,&+ the first element of array.
- 3,the array name. */
- /* Use "&" to get the address of dyli_array */
- printf("&dyli_array is %x\n",&dyli_array);
- /* Get firset Item address of dyli_array */
- printf("&dyli_array[0] is %x\n",&dyli_array[0]);
- /* Here,dyli_array is a array pointer,
- it representative the address of dyli_array.*/
- printf("dyli_array is %x\n",dyli_array);
- /* Yes!You know an array of characters can be output like this. */
- printf("the array of dyli_array is = %s\n",dyli_array);
- printf("output the first element of dyli_array = %c\n",dyli_array[0]);
- printf("/*****----------------------------------------------------------****/");
- /*Below you can see,the string pointer Variable itselt has an Address.
- */
- /* the pointer Variable Address*/
- printf("\n&dyli_str_pointer is %x\n",&dyli_str_pointer);
- /* the string constant first element address.*/
- printf("&dyli_str_pointer[0] is %x\n",&dyli_str_pointer[0]);
- /* the BaseAddress of string constant*/
- printf("dyli_str_pointer is %x\n",dyli_str_pointer);
- printf("the string of dyli_str_pointer is = %s\n",dyli_str_pointer);
- printf("output the first element of dyli_str_pointer point to \n\
- string constant = %c\n"
,dyli_str_pointer[0]);
- getchar();
- return 0;
- }
2、运行结果
- [root@localhost char_array]# ./char_array
- &dyli_array is bfe04d95
- &dyli_array[0] is bfe04d95
- dyli_array is bfe04d95
- the array of dyli_array is = isDyliArray
- output the first element of dyli_array = i
- /*****----------------------------------------------------------****/
- &dyli_str_pointer is bfe04d90
- &dyli_str_pointer[0] is 80485e4
- dyli_str_pointer is 80485e4
- the string of dyli_str_pointer is = isDyliStrPointer
- output the first element of dyli_str_pointer point to
- 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) |