Possible Duplicate:
Why sizeof(param_array) is the size of pointer?可能的复制:为什么sizeof(param_array)是指针的大小?
I'm new to C, I got an warning from clang
when compiling my code:
我刚到C,在编译我的代码时收到了clang的警告:
#include<stdio.h>
char *strcpy (char destination[],const char source[]);
int main(void) {
char str1[] = "this is a very long string";
char str2[] = "this is a short string";
strcpy(str2, str1);
puts(str2);
return 0;
}
char *strcpy (char destination[], const char source[]) {
int size_of_array = sizeof source / sizeof source[0];
for (int i = 0; i < size_of_array; i++) {
destination[i] = source[i];
}
return destination;
}
I don't know what does the following warning mean:
我不知道下面的警告是什么意思:
string_copy_withou_pointer.c:12:29: warning: sizeof on array function parameter
will return size of 'const char *' instead of 'const char []'
[-Wsizeof-array-argument]
int size_of_array = sizeof source / sizeof source[0];
^
string_copy_withou_pointer.c:11:46: note: declared here
char *strcpy (char destination[], const char source[]) {
Any idea?
任何想法?
4 个解决方案
#1
4
That's because const char source[]
in argument position is just syntactic sugar for const char *source
. See, e.g., Steven Summit's C notes.
这是因为在参数位置上的const char源只是const char *源的语法糖。看,例如Steven Summit的C notes。
In this particular case, you'll want to call strlen
. When not dealing with strings, pass the size of the array as a separate argument.
在这个特殊的情况下,你需要调用strlen。当不处理字符串时,将数组的大小作为单独的参数传递。
#2
5
This warning is telling you that if you call sizeof(char[])
you won't get the size of the array but the size of a char*
pointer.
这个警告告诉您,如果您调用sizeof(char[]),您将无法得到数组的大小,但是您将得到一个char*指针的大小。
This means that your variable size_of_array
will be wrong because it won't represent the size of the real array.
这意味着您的变量size_of_array将是错误的,因为它不能代表实际数组的大小。
#4
0
The size of the array doesn't follow when you pass it to the function. Actually it's passed as a pointer which is why the warning message mentions
当您将数组传递给函数时,数组的大小不会跟随。实际上它是作为一个指针传递的,这就是为什么警告信息会被提到。
will return size of 'const char *'
将返回“const char *”的大小
#1
4
That's because const char source[]
in argument position is just syntactic sugar for const char *source
. See, e.g., Steven Summit's C notes.
这是因为在参数位置上的const char源只是const char *源的语法糖。看,例如Steven Summit的C notes。
In this particular case, you'll want to call strlen
. When not dealing with strings, pass the size of the array as a separate argument.
在这个特殊的情况下,你需要调用strlen。当不处理字符串时,将数组的大小作为单独的参数传递。
#2
5
This warning is telling you that if you call sizeof(char[])
you won't get the size of the array but the size of a char*
pointer.
这个警告告诉您,如果您调用sizeof(char[]),您将无法得到数组的大小,但是您将得到一个char*指针的大小。
This means that your variable size_of_array
will be wrong because it won't represent the size of the real array.
这意味着您的变量size_of_array将是错误的,因为它不能代表实际数组的大小。
#3
#4
0
The size of the array doesn't follow when you pass it to the function. Actually it's passed as a pointer which is why the warning message mentions
当您将数组传递给函数时,数组的大小不会跟随。实际上它是作为一个指针传递的,这就是为什么警告信息会被提到。
will return size of 'const char *'
将返回“const char *”的大小