码迷,mamicode.com
首页 > 编程语言 > 详细

数组名和指针

时间:2017-05-30 16:04:59      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:自增   test   其他   bsp   printf   变量   形参   pre   操作符   

#include <stdio.h>
int main()
{
char str[10]="123456789";
char *pStr = str;
printf("%d\n", sizeof(str));
printf("%d\n", sizeof(sizeof(pStr)));
printf("%s\n", str);
printf("%s\n", pStr);
return 0;
}

运行如上程序,结果显示

10

4

123456789

123456789

ps:printf("%s",)  逗号后面直接填想要输出的字符串的位置的指针,例如printf("%s",str+1);或者printf("%s",pStr+1);就会输出23456789

sizeof是一种操作符,不是函数,因为sizeof(int);这条语句合法,但是函数中不可能将数据类型作为形参.

说明1.数组名指代是一种数据结构--数组,因为10和4的区别

但是如上面程序2.数组名又可以常量指针赋给其他变量指针,因为pStr一样可以输出字符串;

#include <stdio.h>
void arrayTest(char str[])
{
printf("%d\n",sizeof(str) );
printf("%s\n",++str);
}
int main()
{
char str1[10] = "ILoveU";
arrayTest(str1);
return 0;
}

运行上面程序结果为

4

LoveU

说明3.(1) 数组名作为函数形参时,在函数体内,其失去了本身的数据内涵,仅仅只是一个指针; 

  (2) 在失去其内涵的同时,它还失去了其常量特性,可以作自增、自减等操作,可以被修改。

 


 

数组名和指针

标签:自增   test   其他   bsp   printf   变量   形参   pre   操作符   

原文地址:http://www.cnblogs.com/ma77045728/p/6919946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!