简体   繁体   English

使用指针保存从函数在c中返回的数据

[英]Using pointer to save the data that's being returned from the function in c

  1. When we wanna return one particular type of data, we need to declare a global variable first right? 当我们想返回一种特定类型的数据时,我们需要首先声明一个全局变量吗? and assign this variable to the value thats being returned by the funtion? 并将这个变量分配给函数返回的值?

  2. Also for int primitive data type, we cannot use malloc to reserve memory block? 同样对于int原始数据类型,我们不能使用malloc保留内存块吗?

Sincerely, headful of doubts. 真诚的,充满疑惑。

#include <math.h> 
#include <stdio.h> 

int *sum(); 
int main() 
{ 
    int *num; 
    num = sum(); 
    printf("\nSum of two given values = %d", *num); 
    return 0; 
} 

int *sum() 
{ 
    int a = 50, b = 80;
    int *sum = NULL; 
    printf("%d %d",a,b);
    *sum = a+b; 
    return sum; 
} 

I wanna using pointers to save the data thats being returned by the function. 我想使用指针保存该函数返回的数据。 is it possible? 可能吗? I know it's possible for linked list structures. 我知道链接列表结构是可能的。 But I'm not sure about integers and other primitive data types. 但是我不确定整数和其他原始数据类型。

Starting with your second question, you can use malloc to allocate memory of any size for any type of variable. 从第二个问题开始,可以使用malloc为任何类型的变量分配任何大小的内存。 Yes, you can use malloc to allocate ints and other primitive types on the heap. 是的,您可以使用malloc在堆上分配整数和其他原始类型。

int i_am_a_stack_variable = 1;
int * i_am_a_pointer_to_heap_memory = malloc(sizeof(int));

For your first question, I think you are mis-understanding how return variables work. 对于第一个问题,我认为您误会了返回变量的工作方式。 Typically, the use of global variables should be avoided. 通常,应避免使用全局变量。 They certainly aren't needed to return values from functions. 从函数返回值当然不需要它们。 The return value of a function is copied from the function's stack frame back to the calling stack frame wherever it is assigned. 一个函数的返回值从该函数的堆栈框架复制回分配给它的调用堆栈框架。 Note that it is COPIED back. 请注意,它已复制回去。 Whether it is a primitive type or a pointer (which is really just another type of primitive). 无论是原始类型还是指针(实际上只是原始的另一种类型)。 Your code could be written just not using pointers at all. 您可以完全不使用指针来编写您的代码。 Also, note that your code was not using a global variable at all even though you mentioned global variables. 另外,请注意,即使您提到了全局变量,您的代码也根本没有使用全局变量。

#include <math.h> 
#include <stdio.h> 

int sum(); 
int main() 
{ 
    int num; 
    num = sum(); 
    printf("\nSum of two given values = %d", num); 
    return 0; 
} 

int sum() 
{ 
    int a = 50, b = 80;
    int sum = 0; 
    printf("%d %d",a,b);
    sum = a+b; 
    return sum; 
} 

Does this make sense? 这有意义吗?

This should work 这应该工作

#include <math.h> 
#include <stdio.h> 

int *sum(); 
int main() 
{ 
    int *num; 
    num = sum(); 
    printf("\nSum of two given values = %d", *num); 
    free(num);
    return 0; 
} 

int *sum() 
{ 
    int a = 50, b = 80;
    int *sum = malloc(sizeof(int)); 
    printf("%d %d",a,b);
    *sum = a+b; 
    return sum; 
}

You need to allocate memory for the pointer. 您需要为指针分配内存。 In your case you need memory for one Integer. 在您的情况下,您需要一个整数的内存。 When you say int* sum =NULL your pointer has no Adress. 当您说int * sum = NULL时,您的指针没有地址。 You can't access a null pointer. 您不能访问空指针。

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

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