由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a weak c question, how to pass an array into a function?
相关主题
一道 memset in C++的题这个怎么allocate memory?
Array in C问一个defining array 的问题
size不固定的struct怎么定义呀?difference between: char** p and char*p[] ??
又一道面试题,我是不是想多了?C++ Interview Question
C++指针问题 int (*) [10]关于C++中一个Class的大小 (转载)
用数组做参数,在函数内部如何知道数组的size?问题:vptr/vtable for virtual function & vptr/vtable for
腆着脸在问一道菜鸟问个C++问题
C++里get array size的问题 (转载)请教大家一道C的面试题
相关话题的讨论汇总
话题: array话题: int话题: sizeof话题: size话题: function
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
请教大家一道C的面试题C++指针问题 int (*) [10]
Is there anything wrong with this C++ array initialization?用数组做参数,在函数内部如何知道数组的size?
free(char *)的问题 (转载)腆着脸在问一道
为什么这个小程序错了?C++里get array size的问题 (转载)
一道 memset in C++的题这个怎么allocate memory?
Array in C问一个defining array 的问题
size不固定的struct怎么定义呀?difference between: char** p and char*p[] ??
又一道面试题,我是不是想多了?C++ Interview Question
相关话题的讨论汇总
话题: array话题: int话题: sizeof话题: size话题: function