C语言中数组作为形参传递时退化为指针

C语言中内存长度的问题

char str[] = “http://www.renren.com”  (长度为21)
char *p = str ; 

请计算
sizeof (str ) = ?(1)
sizeof ( p ) = ?(2)
void Foo ( char str[100]){
sizeof( str ) = ?(3)
}
void *p = malloc( 100 );
sizeof ( p ) = ?(4)

结果应该是**== 22,4,4,4 ==**

只要是指针都是4
数组作为形参退化为指针处理,所以(3)处是4

在VC++6.0中输入一下代码

#include<stdio.h>


	int Foo( char str[100])
	{
		return sizeof(str) ;  
	
	}

                    
int main()
{
	char str[] = "http://www.renren.com";  
    char *p = str ; 
	int *q;
	q=malloc(100*sizeof(int));



	printf("%d\n",sizeof(str));
	printf("%d\n",sizeof(p));
	printf("%d\n",sizeof(q));
	printf("函数调用时str长度等于: %d\n",Foo(str));
	printf("%d\n",sizeof(char));



}

输出结果如下图所示:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值