我是新的C,和我有点困惑的类型系统。我试图编写一些代码来打印字符串中小写字符的数量,但只有一个int大小的数组被传递给cntlower函数。为什么是这样?
#include "lecture2.h"
int main(void){
int lowerAns;
char toCount[] = "sdipjsdfzmzp";
printf("toCount size %i\n", sizeof(toCount));
lowerAns = cntlower(toCount);
printf("answer %i\n", lowerAns);
}
int cntlower(char str[]) {
int lowers = 0;
int i = 0;
printf("str size %i\n", sizeof(str));
printf("char size %i\n", sizeof(char));
for(i = 0; i < (sizeof(str)/sizeof(char)); i++) {
if(str[i] >= 'a' && str[i] <= 'z') {
lowers++;
}
}
return lowers;
}
正因为如此,输出是目前: toCount尺寸13 STR大小4 字符尺寸1 答案4
我敢肯定,这样做的原因是显而易见的一些你,但不幸的是它不适合我!
+0
阅读http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array类似的例子,你在做什么错误的sizeof和指针。 –