M*m 发帖数: 141 | 1 void f(int a[]){
// when I try to get the size of a, it is always 4,
// how can I get the actual size of the array?
}
int main(){
int a[] = {1, 2, 4, 5};
sizeof(a) // 16;
f(a)..
} | k****f 发帖数: 3794 | 2 void f(int*a, int nLengthA){
}
【在 M*m 的大作中提到】 : void f(int a[]){ : // when I try to get the size of a, it is always 4, : // how can I get the actual size of the array? : } : int main(){ : int a[] = {1, 2, 4, 5}; : sizeof(a) // 16; : f(a).. : }
| M*m 发帖数: 141 | 3 yeah, that works. but is there a way that I can get the size of a within f,
using sizeof... so that I do not have to pass the size of an array?
【在 k****f 的大作中提到】 : void f(int*a, int nLengthA){ : }
| g*********s 发帖数: 1782 | 4 No way unless it's a properly initialized char*. Then you can call strlen or
check the '\n'.
,
【在 M*m 的大作中提到】 : yeah, that works. but is there a way that I can get the size of a within f, : using sizeof... so that I do not have to pass the size of an array?
| M*m 发帖数: 141 | 5 it seems that if I do
int main(){
int a[] = {12, 23, 121};
//sizeof(a) returns 12 = 3*sizeof(int)
}
as stated in the sizeof definition, we can do sizeof of an object or a type.
so the question is
1. how does sizeof work for an array object,
2. how do we pass the array to a function as an object. so that we can get
the size in the same way as we do here.
or
【在 g*********s 的大作中提到】 : No way unless it's a properly initialized char*. Then you can call strlen or : check the '\n'. : : ,
| t****t 发帖数: 6806 | 6 The array size information is bound to the array type. But if the function
you called does not know the type of the array, i.e. doesn't know the lenght
of the array, then you can not know the length of array. :)
That is to say, you have to know the length of array before you know the
length of array. So the answer is no for C language. For C++, this can be
solved with template.
Please read C FAQ, there's a section for array and pointer.
type.
【在 M*m 的大作中提到】 : it seems that if I do : int main(){ : int a[] = {12, 23, 121}; : //sizeof(a) returns 12 = 3*sizeof(int) : } : as stated in the sizeof definition, we can do sizeof of an object or a type. : so the question is : 1. how does sizeof work for an array object, : 2. how do we pass the array to a function as an object. so that we can get : the size in the same way as we do here.
| M*m 发帖数: 141 | 7 Hmm, thanks, how do I understand parameter "a" here?
void f(int a[3]){
printf("size %d", sizeof(a)); // print 4;
}
Does that mean that C compiler will ignore [3], and simply convert the it to
int *?
lenght
【在 t****t 的大作中提到】 : The array size information is bound to the array type. But if the function : you called does not know the type of the array, i.e. doesn't know the lenght : of the array, then you can not know the length of array. :) : That is to say, you have to know the length of array before you know the : length of array. So the answer is no for C language. For C++, this can be : solved with template. : Please read C FAQ, there's a section for array and pointer. : : type.
| S****t 发帖数: 1186 | 8 you got it
to
【在 M*m 的大作中提到】 : Hmm, thanks, how do I understand parameter "a" here? : void f(int a[3]){ : printf("size %d", sizeof(a)); // print 4; : } : Does that mean that C compiler will ignore [3], and simply convert the it to : int *? : : lenght
| t****t 发帖数: 6806 | 9 I guess you didn't read C FAQ. Go read it.
to
【在 M*m 的大作中提到】 : Hmm, thanks, how do I understand parameter "a" here? : void f(int a[3]){ : printf("size %d", sizeof(a)); // print 4; : } : Does that mean that C compiler will ignore [3], and simply convert the it to : int *? : : lenght
|
|