如何将指针传递给未知大小的数组? [英] How to pass a pointer to an unknown-size array?
问题描述
你好!
我可以传递一个指向双重的指针接受
double *的函数,如下所示:
int func(double * var){
* var = 1.0;
...
}
double var;
n = func(& var);
---
现在我想传递一个指向双精度数组的指针,大小<但是,
的数组不能修复:
int func(double [] * array){
int index;
index = 3;
array [index] = 1.0;
...
}
双数组[100];
n = func(& array);
以上代码编译器给我一个错误。到目前为止,我发现的唯一
解决方案是非常不优雅的解决方案:
int func(void * array){
int index;
index = 3;
*((double *)(array)+ index)= 1.0;
... < br $>
}
双数组[100];
n = func(& array);
---
必须有一个更清洁的方式..但它是什么?
我是对C和仅C ++感兴趣解决方案。
谢谢!
迈克
Hello!
I can pass a "pointer to a double" to a function that accepts
double*, like this:
int func(double* var) {
*var=1.0;
...
}
double var;
n=func(&var);
---
Now I want to pass a pointer to an array of doubles, the size
of the array must not be fixed though:
int func(double[]* array) {
int index;
index=3;
array[index]=1.0;
...
}
double array[100];
n=func(&array);
with the above code the compiler gives me an error. The only
solution that I found so far is this very inelegant one:
int func(void* array) {
int index;
index=3;
*((double*)(array)+index)=1.0;
...
}
double array[100];
n=func(&array);
---
There must be a cleaner way.. but what is it?
I am interested in both C and "C++ only" solutions.
Thanks!
Mike
推荐答案
no****@nospam.com 写道:
no****@nospam.com wrote:
现在我想通过指向双精度数组的指针,数组的大小不得修复:
int func(double [] * array){
int index;
index = 3;
数组[index] = 1.0;
......
}
双数组[100];
> n = func(& array);
Now I want to pass a pointer to an array of doubles, the size
of the array must not be fixed though:
int func(double[]* array) {
int index;
index=3;
array[index]=1.0;
...
}
double array[100];
n=func(&array);
执行此操作的标准方法如下:
int func (double * array){
array [3] = 1.0;
...
}
double array [100];
n = func(array);
最后一行等同于:
n = func(& array [0]);
原因是数组的名称是
初始元素。
Espen
-
数字是人类思维的自由创造。
Julius Wilhelm Richard Dedekind
The standard way of doing it is like this:
int func(double* array){
array[3]=1.0;
...
}
double array[100];
n=func(array);
The last line is equivalent to this:
n=func(&array[0]);
The reason is that the name of an array is a synonym for the location of
the initial element.
Espen
--
Numbers are the free creation of the human mind.
Julius Wilhelm Richard Dedekind
>现在我想传递一个指向双数组的指针,但是不能修复数组的大小
> Now I want to pass a pointer to an array of doubles, the size
:
试试这个:
int func(double * array,int num_elements)
{
int i;
for(i = 0; i< num_elements; i ++)
{
array [i] = 1.0;
}
}
int main()
{
double array [100];
func(array,100);
}
单指针将指向数组的第一个元素。你这样做
不需要双重间接。您需要明确传递数组的大小
,因为C不保留该信息存储。
Jon
- ---
学习使用Linux汇编语言进行编程
http://www.cafeshops.com/bartlettpublish.8640017
没有**** @ nospam.com 写道:
你好!
我可以传递一个指针双重的一个接受
double *的函数,如下所示:
int func(double * var){
* var = 1.0;
...
}
double var;
n = func(& var);
---
现在我想传递一个指向双数组的指针,但是数组的大小不能修复:
int func(double [] * array){
int index;
index = 3;
array [index] = 1.0;
...
双数组[100];
n = func(& array);
上面的代码编译器给了我一个错误。到目前为止我发现的唯一解决方案是这个非常不优雅的解决方案:
int func(void * array){
int index;
index = 3;
*((double *)(array)+ index)= 1.0;
......
双数组[100];
n = func(& array);
---
必须有一个更清洁的方式..但它是什么?
>我对C和仅限C ++感兴趣解决方案。
谢谢!
迈克
Hello!
I can pass a "pointer to a double" to a function that accepts
double*, like this:
int func(double* var) {
*var=1.0;
...
}
double var;
n=func(&var);
---
Now I want to pass a pointer to an array of doubles, the size
of the array must not be fixed though:
int func(double[]* array) {
int index;
index=3;
array[index]=1.0;
...
}
double array[100];
n=func(&array);
with the above code the compiler gives me an error. The only
solution that I found so far is this very inelegant one:
int func(void* array) {
int index;
index=3;
*((double*)(array)+index)=1.0;
...
}
double array[100];
n=func(&array);
---
There must be a cleaner way.. but what is it?
I am interested in both C and "C++ only" solutions.
Thanks!
Mike
int func(double * array,size_t arrsize)
{
int index = 3;
if(index< arrsize)
{
array [index ] = 1.0;
}
...
}
....
double array [100];
int n = func(array,sizeof array);
....
请记住,在C中,下标操作a [i]被*定义为*
*(a + i)。因此,在* most *表达式上下文中(sizeof()是两个异常IINM中的一个
),a的类型从T的数组转换为
指向T的指针,并将其值设置为数组中第一个元素
的地址(& a [0]),因此表达式*(a + i)产生正确的
结果。由于这种转换,当你将一个数组作为
参数传递给一个函数时,最后传递的是一个指向
基类型的指针,它的值是第一个元素的地址(这是为什么我没有在上面的函数调用中使用地址运算符&)
如果你需要知道函数中数组的大小,需要将
指定数组大小作为单独的参数;一个指针不知道
它所指向的内存块的大小。
如果你想传递一个指向数组对象(不只是一个指针
到第一个元素),代码如下所示:
int func(double(* array)[100 ])//大小必须与原始匹配
{
int index = 3;
(* array)[index] = 1.0;
...
}
....
double array [100];
n = func(& array);
这一次,我们将指针传递给100个元素的double数组。
由于100个元素的double数组是一个独特的类型,这种方法
不适用于不同大小的数组。
int func(double *array, size_t arrsize)
{
int index = 3;
if (index < arrsize)
{
array[index]=1.0;
}
...
}
....
double array[100];
int n = func(array, sizeof array);
....
Remember that in C, the subscripting operation a[i] is *defined* as
*(a+i). Therefore, in *most* expression contexts (sizeof() being one
of two exceptions IINM), the type of a is converted from array of T to
pointer to T, and its value is set to the address of the first element
in the array (&a[0]), so the expression *(a+i) yields the correct
result. Because of this conversion, when you pass an array as a
parameter to a function, what you wind up passing is a pointer to the
base type, and its value is the address of the first element (this is
why I didn''t use the address operator & in the function call above).
If you need to know the size of the array in the function, you need to
specify the array size as a separate parameter; a pointer doesn''t know
the size of the chunk of memory it''s pointing to.
If you wished to pass a pointer to the array object (not just a pointer
to the first element), the code would look like this:
int func(double (*array)[100]) // size must match original
{
int index=3;
(*array)[index] = 1.0;
...
}
....
double array[100];
n = func(&array);
This time, we are passing a pointer to a 100-element array of double.
Since 100-element array of double is a distinct type, this approach
won''t work for arrays of different sizes.
这篇关于如何将指针传递给未知大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!