SIZEOF不会用C返回变量的真实大小 [英] Sizeof doesn't return the true size of variable in C

查看:96
本文介绍了SIZEOF不会用C返回变量的真实大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code

#include <stdio.h>

void print(char string[]){
 printf("%s:%d\n",string,sizeof(string));
}

int main(){
 char string[] = "Hello World";
 print(string);
}

和输出

Hello World:4

那么,有什么不对吗?

So what's wrong with that ?

推荐答案

它做的变量的真实大小返回(说真的,参数的功能)。问题是,这是不是那种你认为它是。

It does return the true size of the "variable" (really, the parameter to the function). The problem is that this is not of the type you think it is.

字符字符串[] ,作为参数传递给一个函数,相当于的char *字符串。你得到 4 的结果,因为这是大小,您的系统上,一个的char *的

char string[], as a parameter to a function, is equivalent to char* string. You get a result of 4 because that is the size, on your system, of a char*.

请在这里阅读更多: http://c-faq.com/aryptr/index.html

这篇关于SIZEOF不会用C返回变量的真实大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆