返回使用C数组 [英] Returning an array using C

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

问题描述

我是比较新的C和我需要一些帮助的方法处理数组。从Java编程的到来,我习惯了,能够说 INT []的方法(),以返回数组。然而,我发现,用C你有当您返回他们使用指针数组。作为一个新的程序员,我真不明白这一切时,即使有很多论坛我经历看。

I am relatively new to C and I need some help with methods dealing with arrays. Coming from Java programming, I am used to being able to say int [] method()in order to return an array. However, I have found out that with C you have to use pointers for arrays when you return them. Being a new programmer, I really do not understand this at all, even with the many forums I have looked through.

基本上,我试图写在C.返回一个字符数组,我会提供方法的方法(可以称之为returnArray)与数组。它会创建从previous数组的新数组,并返回一个指向它。我只需要关于如何得到一些帮助这个开始以及如何一旦送出阵列的读指针。任何帮助解释这是AP preciated。

Basically, I am trying to write a method that returns a char array in C. I will provide the method (lets call it returnArray) with an array. It will create a new array from the previous array and return a pointer to it. I just need some help on how to get this started and how to read the pointer once it is sent out of the array. Any help explaining this is appreciated.

数组返回函数建议code格式

char *returnArray(char array []){
 char returned [10];
 //methods to pull values from array, interpret them, and then create new array
 return &(returned[0]); //is this correct?
} 

功能的来电

int main(){
 int i=0;
 char array []={1,0,0,0,0,1,1};
 char arrayCount=0;
 char* returnedArray = returnArray(&arrayCount); ///is this correct?
 for (i=0; i<10;i++)
  printf(%d, ",", returnedArray[i]);  //is this correctly formatted?
}

我没有测试此然而,正如我的C编译器不工作的时刻,但我想这出

推荐答案

您不能从函数C.您返回数组也不能(也不应该)做:

You can't return arrays from functions in C. You also can't (shouldn't) do this:

char *returnArray(char array []){
 char returned [10];
 //methods to pull values from array, interpret them, and then create new array
 return &(returned[0]); //is this correct?
} 

返回与自动存储时间,并把它引用创建一旦离开它的声明范围内,将变得无效,即,当函数返回。

returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

您将需要动态分配内存的函数内或填充调用者提供一个preallocated缓冲区。

You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

选项1(主叫方负责释放 RET

Option 1 (caller responsible for deallocating ret)

char *foo(int count) {
    char *ret = malloc(count);
    if(!ret)
        return NULL;

    for(int i = 0; i < count; ++i) 
        ret[i] = i;

    return ret;
}

说它像这样:

int main() {
    char *p = foo(10);
    if(p) {
        // do stuff with p
        free(p);
    }

    return 0;
}

选项2(来电者中分配 BUF 并传递给函数)

void foo(char *buf, int count) {
    for(int i = 0; i < count; ++i)
        buf[i] = i;
}

和调用它像这样:

int main() {
    char arr[10] = {0};
    foo(arr, 10);
    // no need to deallocate, we allocated 
    // arr with automatic storage duration.
    // if we had dynamically allocated it
    // (i.e., malloc or some variant) then we 
    // would need to call free(p)
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆