简体   繁体   English

在 C 和 malloc 用法中返回一个 char 数组

[英]Return an array of char in C and malloc usage

First attempt:第一次尝试:

char* loadValues (char* str) {

  char* toReturn[5];

  .. some operations here ..

  return toReturn

}

This will obviously return warnings and will not work properly since the memory location is going to be free-d after the function is finished.这显然会返回警告并且不会正常工作,因为内存位置将在函数完成后被释放。

So I thought of using malloc, however, I don't understand how this may work with arrays.所以我想到了使用 malloc,但是,我不明白这如何与数组一起使用。

Second attempt:第二次尝试:

char* loadValues (char* str) {

  char (*toReturn)[5] = malloc(sizeof *toReturn);

  .. some operations here ..

  return toReturn

}

my toReturn contains strings, for example toReturn[0] may be "Hello"我的toReturn包含字符串,例如toReturn[0]可能是"Hello"

Any suggestion?有什么建议吗?

As far as I understand, you want to return an array of pointers and allocate memory for the pointees of that array.据我了解,您想返回一个指针数组并为该数组的指针分配内存。 With your current code, you can't return the array of pointers as it's local.使用您当前的代码,您不能返回指针数组,因为它是本地的。 You can do it in the following way:您可以通过以下方式进行:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char** loadValues () {
  char** toReturn;
  int i;
  toReturn = malloc(5*sizeof(char*));
  for(i=0;i<5;i++)
  {
    toReturn[i] = malloc(25); //Change the size as per your need
    strncpy(toReturn[i], "string",i+1); //Something to copy
  }
  return toReturn;
}

int main()
{
  int i; 
  char **arr = loadValues();
  for(i=0;i<5;i++)
  {
    printf("%s\n", arr[i]);
  }

  for(i=0;i<5;i++)
  {
    free(arr[i]);
  }

  free(arr);
  return 0;  
}

Notice the return type of loadValues and memory allocated for the array is freed in main .注意loadValues的返回类型和为数组分配的内存在main中被释放。


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void loadValues (char **toReturn) {
  int i;
  for(i=0;i<5;i++)
  {
    toReturn[i] = malloc(25); //Change the size as per your need
    strncpy(toReturn[i], "string",i+1); //Something to copy
  }
}

int main()
{
  int i; 
  char *arr[5];
  loadValues(arr);
  for(i=0;i<5;i++)
  {
    printf("%s\n", arr[i]);
  }

  for(i=0;i<5;i++)
  {
    free(arr[i]);
  }

  return 0;  
}

You should also check if calls to malloc succeeded and handle errors.您还应该检查对malloc的调用是否成功并处理错误。

malloc simply works by allocating a chunk of memory and returning a pointer to the beginning. malloc 只是通过分配一块内存并返回指向开头的指针来工作。 What might be tricky here is that the array you're making doesn't contain characters, it contains pointers to characters, which means you can't simply say这里可能有点棘手的是你正在制作的数组不包含字符,它包含指向字符的指针,这意味着你不能简单地说

char* toReturn[5] = malloc(memory);

As far as I can see you'd need to malloc enough memory for five char pointers for toReturn, then go through the elements and malloc however much memory you think you'll need to them.据我所知,您需要为 toReturn 的五个字符指针分配足够的内存,然后遍历元素和 malloc,无论您认为需要多少内存。 Take extra care you remember to go through the array again and free all the memory when you're done with it, before freeing the array itself.在释放数组本身之前,要格外小心,记得再次遍历数组并在完成后释放所有内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM