函数中的C返回数组

函数中的C返回数组

本文介绍了函数中的C返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C较了解,我习惯于用Java编程,因此在数组方面我发现C有点困难.我仍然对这种情况感到困惑:

Im relatively knew to C, i am used to program in Java so i find C a little bit difficult in what concerns arrays. I still cofuse myself with this cases:

int a [];
int* a;
int *a;

在Java中,我将执行以下操作以在函数中返回数组:

In java, i would do something like this to return an array in a function:

int [] returnArr(int [] a){
... modify a ...
return a;
}

int [] a = {...};
int [] b = returnArr(a); ##

我该如何在C语言中执行相同的操作,特别是使用##的部分.

How can i do the same in C, specially the parts with ##.

我有这个功能:

float *normalizeValues(float *v, float maxY){

    int size = sizeof(v) / sizeof(float);
    float max = findMax(v);
    float ratio = maxY / max;
    int i;
    for(i = 0; i < size ; ++i){
        v[i] = v[i] * ratio;
    }
    return v;
}

我正在执行以下操作:

float vert [] = {306, 319, 360, 357, 375, 374, 387, 391, 391, 70, 82, 94, 91, 108, 114, 125, 127, 131};
int i = 0;
float *vert2;
vert2 = normalizeValues(vert, 0.7);

for(i = 0; i < sizeof(vert2) / sizeof(float); ++i){
    fprintf(stdout,": %f\n",vert2[i]);
}

输出只有1个元素.

推荐答案

要直接回答更新的问题:您必须传入数组的大小. C没有像Java这样的机制来存储数组的大小.如果由于数组是全局变量或局部变量而不是动态分配的,因此编译器知道数组的大小,则可以使用sizeof()运算符.否则,您必须单独知道大小,或者在数组中使用哨兵值(例如末尾为0.0或NULL).

To directly answer your updated question: you have to pass in the size of the array. C has no mechanism to store the size of arrays like Java does. If the compiler knows about the size of the array because the array is a global or local variable, not dynamically allocated, then you can use the sizeof() operator. Otherwise, you have to know the size separately, or use sentinel values in your array (such as a 0.0 at the end, or a NULL).

关于数组,指针和参数的一般信息,请参见下文:

As for arrays, pointers and arguments in general, see below:

您将返回一个指向数组的指针,该指针用'*'语法表示:

You will be returning a pointer to the array, which is indicated with the '*' syntax:

int *returnArr(int[] a) {
    // modify a...
    return a;
}

int a[] = { ... };
int *b;
b = returnArr(a);

一些注意事项:

  • 您不能在涉及非常数表达式(例如函数调用)的变量声明中进行赋值.不过,这在C99中可能已经改变.
  • 括号在变量名之后 ,与Java中的括号不同.尽管Java的语法更加一致,但在C语言中却没有多大意义,在C语言中,您经常在变量声明的括号中给出数组大小:

  • You can't do assignments in variable declarations that involve non-constant expressions (e.g., function calls). This might have changed in C99, though.
  • The brackets go after the variable name, unlike in Java where they are part of the type. Even though Java's syntax is more consistent, it doesn't quite make sense in C where you often give the array size in the brackets in the variable declaration:

int a[3] = { ... };

无法指定函数返回一个数组,而不是普通指针.在C语言中,数组引用会衰减到指针(尽管通常所说的指针和数组不是同一件事).这意味着,每当您传递一个数组时,C都仅提供一种将指针传递给该数组的方法.整个数组实际上并没有被复制.碰巧,数组的名称也是指向数组第一个元素的指针.

There's no way to specify that a function returns an array as opposed to a plain pointer. In C, array references decay to pointers (though pointers and arrays are NOT the same thing, as is commonly claimed). That means that whenever you pass an array around, C only provides a means to a pass a pointer to the array. The whole array isn't actually copied. As it happens, the name of the array is also a pointer to the first element of the array.

也请注意user268396在回答中说了什么.如果您打算创建一个新数组并返回它,则需要动态分配该数组,或者传递一个指向已分配数组的指针(这似乎是您正在做的事情)

Please also take note of what user268396 says in their answer. If you are planning to create a new array and return it, you'll need to either allocate the array dynamically, or have a pointer to an already allocated array be passed in (which is what it seems like you are kind of doing anyway).

这篇关于函数中的C返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:29