r*******y 发帖数: 1081 | 1 #include
int f(double a[]){
return sizeof(a);
}
int main(void){
double a[100];
printf("%d %d\n", f(a), sizeof(a)));
}
为什么显示出来的是 4 800
而不是我想要的 800 800
有什么办法让函数的返回值是800吗?
谢谢 |
i*****f 发帖数: 578 | 2 simply put,
in f()'s argument, type is double *. sizeof(double*) is 4
in main(), type is double[100], sizeof(double[100]) is 800
【在 r*******y 的大作中提到】 : #include : int f(double a[]){ : return sizeof(a); : } : int main(void){ : : double a[100]; : printf("%d %d\n", f(a), sizeof(a))); : } : 为什么显示出来的是 4 800
|
r*******y 发帖数: 1081 | 3 有什么办法修改 f()里面的参数类型使得两种出来的结果都是 sizeof(double [100])
吗?
【在 i*****f 的大作中提到】 : simply put, : in f()'s argument, type is double *. sizeof(double*) is 4 : in main(), type is double[100], sizeof(double[100]) is 800
|
L***n 发帖数: 6727 | 4 no, array argument in C/C++ is simply treated as a pointer
【在 r*******y 的大作中提到】 : #include : int f(double a[]){ : return sizeof(a); : } : int main(void){ : : double a[100]; : printf("%d %d\n", f(a), sizeof(a))); : } : 为什么显示出来的是 4 800
|
r*******y 发帖数: 1081 | 5 还以为现在的 C 也能处理这个了。看来得多加一个参数了,本来想偷懒。
【在 L***n 的大作中提到】 : no, array argument in C/C++ is simply treated as a pointer
|
r****y 发帖数: 26819 | 6 办法就是把sizeof()的源代码改成一个函数
【在 r*******y 的大作中提到】 : 有什么办法修改 f()里面的参数类型使得两种出来的结果都是 sizeof(double [100]) : 吗?
|
r*******y 发帖数: 1081 | 7 work吗?
【在 r****y 的大作中提到】 : 办法就是把sizeof()的源代码改成一个函数
|
i*****f 发帖数: 578 | 8 it is not going to work. because the size of a var. is determined on compile
time. Assume you are passed a pointer to a double (that the only
information you have), you will never know if it is just a pointer to a
double or it is a part of an array.
There are two options for your problem.
1. pass the size as well
2. use something like:
struct array {double* data, size_t len};
【在 r*******y 的大作中提到】 : work吗?
|
r****y 发帖数: 26819 | 9 not exactly original but can simulate:
/*
* sizeof.c - Implementation of sizeof operator
* http://faq.zanvar.in
*/
#include
#include
/* Find the size of an variable */
#define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var)))
/* Find the size of a data type */
#define sizeof_type( type ) (size_t)((type*)1000 + 1 )-(size_t)((type*)1000)
int main ( void )
{
int a;
int b[10];
printf ( "%lu\n", sizeof ( b ) );
printf ( "%lu\n", sizeof ( b+0 ) );
printf ( "%lu\n", sizeof_va
【在 r*******y 的大作中提到】 : work吗?
|
N****w 发帖数: 21578 | 10 pass the number 100 into f() as a parameter.
【在 r*******y 的大作中提到】 : 有什么办法修改 f()里面的参数类型使得两种出来的结果都是 sizeof(double [100]) : 吗?
|
|
|
i*****f 发帖数: 578 | 11 what are you trying to say?
try this
int f(int b[])
{return sizeof(b);}
【在 r****y 的大作中提到】 : not exactly original but can simulate: : /* : * sizeof.c - Implementation of sizeof operator : * http://faq.zanvar.in : */ : #include : #include : /* Find the size of an variable */ : #define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var))) : /* Find the size of a data type */
|
r****y 发帖数: 26819 | 12 套用这个句型:
i am trying to say that the code i pasted can be used as a test.
至于这个f,得到的是指针大小。
【在 i*****f 的大作中提到】 : what are you trying to say? : try this : int f(int b[]) : {return sizeof(b);}
|
r*******y 发帖数: 1081 | 13 还是不行阿 我的 函数修改成
int f(int a[])
{
return (size_t)(&(a)+1)-(size_t)(&(a));
}
还是不是我想要的阿,只是一个指针的size.
【在 r****y 的大作中提到】 : not exactly original but can simulate: : /* : * sizeof.c - Implementation of sizeof operator : * http://faq.zanvar.in : */ : #include : #include : /* Find the size of an variable */ : #define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var))) : /* Find the size of a data type */
|
i*****f 发帖数: 578 | 14 yes your code implements the sizeof opterator. but sizeof operator does not
solve lz's problem, which is determining array's size at *runtime* only
through a pointer to that array.
【在 r****y 的大作中提到】 : 套用这个句型: : i am trying to say that the code i pasted can be used as a test. : 至于这个f,得到的是指针大小。
|
b****j 发帖数: 78 | 15 用C++就可以,改成
int f(double (&a)[100]) {
【在 r*******y 的大作中提到】 : #include : int f(double a[]){ : return sizeof(a); : } : int main(void){ : : double a[100]; : printf("%d %d\n", f(a), sizeof(a))); : } : 为什么显示出来的是 4 800
|
i*****f 发帖数: 578 | 16 If this is function declaration, you already put [100] in it -__-
【在 b****j 的大作中提到】 : 用C++就可以,改成 : int f(double (&a)[100]) {
|
b****j 发帖数: 78 | 17 this is a function definition, not declaration.
he didn't say "[100]" is not allowed.
【在 i*****f 的大作中提到】 : If this is function declaration, you already put [100] in it -__-
|
i*****f 发帖数: 578 | 18 you are selling a function specific for 100 elements. I really don't think
you are going to do this in real life. Plus, in c you can do:
f(int a[100])
{
return sizeof(a);
}
done.
【在 b****j 的大作中提到】 : this is a function definition, not declaration. : he didn't say "[100]" is not allowed.
|
b****j 发帖数: 78 | 19 why don't you try it out and let me know what it returns?
【在 i*****f 的大作中提到】 : you are selling a function specific for 100 elements. I really don't think : you are going to do this in real life. Plus, in c you can do: : f(int a[100]) : { : return sizeof(a); : } : done.
|
h*********y 发帖数: 49 | 20 If you use malloc, you may use msize/_msize to get the size.
【在 r*******y 的大作中提到】 : #include : int f(double a[]){ : return sizeof(a); : } : int main(void){ : : double a[100]; : printf("%d %d\n", f(a), sizeof(a))); : } : 为什么显示出来的是 4 800
|
|
|
r*******y 发帖数: 1081 | 21 seems a good solution. but in which header files?
I tried stdlib.h, malloc.h. But did not find msize or _msize. thanks.
【在 h*********y 的大作中提到】 : If you use malloc, you may use msize/_msize to get the size.
|
r*******y 发帖数: 1081 | 22 malloc_usable_size(void *ptr) works which is in malloc.h
【在 r*******y 的大作中提到】 : seems a good solution. but in which header files? : I tried stdlib.h, malloc.h. But did not find msize or _msize. thanks.
|
s*****u 发帖数: 164 | 23 In C++, you can try
template< size_t N >
int f( double ( &a )[ N ] );
This should give the desired result. |
r*******y 发帖数: 1081 | 24 I think I get a better one now as I desired
#include
#include
#include
int f(double *a){
return malloc_usable_size(a) - sizeof(void *);
}
int main(void){
double *a= malloc(sizeof(double) * 100);
printf("%d \n", f(a));
}
Thanks.
【在 s*****u 的大作中提到】 : In C++, you can try : template< size_t N > : int f( double ( &a )[ N ] ); : This should give the desired result.
|
n***s 发帖数: 1257 | 25 Is N constant or variable?
【在 s*****u 的大作中提到】 : In C++, you can try : template< size_t N > : int f( double ( &a )[ N ] ); : This should give the desired result.
|
v*s 发帖数: 946 | |
o**n 发帖数: 1249 | 27 就是就是
【在 v*s 的大作中提到】 : 还是老老实实传个长度进去吧。不折腾。
|