简体   繁体   English

C - 在 function 中使用 memcpy 分配值而不是返回

[英]C - using memcpy in function to assign value instead of return

Playing around and trying to learn how arrays in C work, and just how C works in general.尝试了解 C 中的 arrays 是如何工作的,以及 C 是如何工作的。 I wanted to make a function that returned a 2 element array and assign that array to a variable.我想做一个 function 返回一个 2 元素数组并将该数组分配给一个变量。 first I did;首先我做到了;

int make_array(int i) {
   int out[2] = {i, i + 1};
   return (int) *out;}

int main(void) {
    int arr[2] = { make_array(68) }; // should be {68, 69}
    for (int i = 0; i < 2; i++) {
        printf("%d, ", arr[i]);}
    return 0;

>> 68, 0,

This was the only way I could find to write it without throwing an error, and it output 68, 0, instead of 68, 69,.这是我能找到的唯一一种在不引发错误的情况下编写它的方法,它是 output 68, 0,而不是 68, 69,。 So I tried this;所以我尝试了这个;

void make_another_array(int j, void *vp) {
   int out[2] = {j, j + 1};
   memcpy(vp, &out, sizeof(int) * 2);}

int main(void) {
   int *hotdog_water = malloc(sizeof(int) * 2);
   make_another_array(68, hotdog_water);
   for (int i = 0; i < 2; i++){
       printf("%d, ", hotdog_water[i]);}
   free(hotdog_water); hotdog_water = NULL;
   return 0;}

The function saves the result somewhere instead of returning it. function 将结果保存在某处而不是返回它。 The end result seems to work the way I'd like it to, but not assigning a value from something returned from a function seems hacky.最终结果似乎按照我想要的方式工作,但不从 function 返回的东西中分配值似乎很麻烦。 I'm sure there's a better way to do this specific thing, but is there anything fundamentally wrong with doing it this way?我确信有更好的方法来做这个特定的事情,但是这样做有什么根本错误吗? What problems would I run into if I wrote to where a pointer was pointing for everything instead of using return?如果我写一个指针指向所有东西的地方而不是使用返回,我会遇到什么问题? Are there any use cases for copying the results instead of returning it?是否有任何用于复制结果而不是返回结果的用例?

Returning data in space provided by the calling routine is normal and common.在调用例程提供的空间中返回数据是正常且常见的。 The standard C library includes several examples, such as:标准 C 库包括几个示例,例如:

  • scanf("%d%d", &a, &b) stores values in a and b . scanf("%d%d", &a, &b)将值存储在ab中。
  • frexp(float x, &e) returns one value and stores another in e . frexp(float x, &e)返回一个值并将另一个值存储在e中。
  • fread(array, size, number, stream) provides number values in array . fread(array, size, number, stream)array中提供number值。

If you do wish to provide two values through the function-return-value mechanism, you can do this by using a structure:如果您确实希望通过函数返回值机制提供两个值,则可以使用以下结构来实现:

struct foo { int v[2]; } make_array(int i)
{
    return (struct foo) {{ i, i+1 }};
}

#include <stdio.h>

int main(void)
{
    struct foo x = make_array(68);
    for (int i = 0; i < 2; ++i)
        printf("%d, ", x.v[i]);
    printf("\n");
}

For large structures, a C implementation typically implements the return mechanism just as you suggest with pointers: Where the C code looks like the function returns a structure, in the assembly code, the calling routine provides space for the structure and actually passes a pointer to it as an argument the function.对于大型结构,C 实现通常实现返回机制,正如您使用指针所建议的那样: C 代码看起来像 function 代码中的结构体,调用例程的指针提供结构体,它作为 function 的参数。 When returning, the function uses that pointer to fill in the return value.返回时,function 使用该指针填充返回值。

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

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