0
  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?

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.

4
  • 1
    you're dereferencing a NULL pointer. Commented May 14, 2019 at 21:19
  • To answer your question about malloc, yes, you can use it with primitives: int *sum = malloc(sizeof(*sum)); Commented May 14, 2019 at 21:23
  • Unless you are allocating memory in the function, you will have to pass a pointer to the required sum variable to the function – if you must use a pointer to the sum. Commented May 14, 2019 at 21:27
  • It should be int main(void) Commented May 14, 2019 at 22:50

2 Answers 2

1

Starting with your second question, you can use malloc to allocate memory of any size for any type of variable. Yes, you can use malloc to allocate ints and other primitive types on the heap.

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you sir, this makes so much sense. as you mention when the function is returning a data, it's just creating the copy of that data right? so we do need a variable somewhere to assign this copy to the variable for later use?
If you want to catch the return variable, yes it needs to be assigned to something. You are allowed to ignore a return value if you don't want to use it. In the code here, the value of the variable sum in the sum() stack frame is copied into the variable num in the main() stack frame. In the alternate version from Moschte's answer where an int * is being returned the address of the malloc'ed integer is copied from sum to num in the same way, but in that case, it is the address being copied rather than the int value. Though, in this code there really is no need to use pointers.
@Why'dofVir If this answers your question, please accept the answer so I get credit for it. Thanks.
0

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. You can't access a null pointer.

1 Comment

Beware of memory leak - calling malloc() without a corresponding free()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.