1

I have read that to return a pointer from a function to another function, the function that passes the pointer must be a pointer function. I mean for instance if the name of function is int Add it must be int *Add but when I test it, it seems like either way works just fine ? Is that a non mandatory rule ?

#include <stdio.h>

int *random_func(){ // if remove * and recompile it, still works fine.
    int arr[2] = {12,13};
    int *point = (int*) malloc(2*sizeof(int));
    point= arr;
    return point;
}

main(){
    int *ptr;
    ptr=random_func();
    printf("%d %d ",*(ptr), *(ptr+1));
    return 0;

}
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
hkn-a
  • 23
  • 5
  • Conversion from pointer to integer and the other way round can lead to undefined behavior if the integer is not big enough. Upgrade your compiler and stick to good programming practices. You want to return a pointer to int, then return a pointer to int... – DeiDei Jan 19 '17 at 19:09
  • Sorry for the typo in main function it should be ptr = random_func(); – hkn-a Jan 19 '17 at 19:09
  • 2
    "works fine" is very subjective. – Kerrek SB Jan 19 '17 at 19:11
  • 4
    `point = arr;` ... why have you overwritten the pointer that was returned by `malloc`? Instead, you return a pointer to a local array that will die as soon as you return from the function, and, cause a memory leak since you cannot now `free` the memory you allocated. – Weather Vane Jan 19 '17 at 19:11
  • @WeatherVane I did that because I wanted to be sure that I can access other values in the array otherwise I could have thought it returned just an integer ? (according to my logic if I can access other members of array from outside that means yes I returned a pointer. Since I am learning on my own developing good practises about c seems like a long way to go. – hkn-a Jan 19 '17 at 19:19
  • You "can access other members of the array from outside" only on a **lucky day** if you return a pointer to a local array that goes out of scope and life, in other words, its memory is up for grabs by anything else. You are **lucky** if this does not happen before you access the memory. You are **unlucky** if the memory gets reused by the system/language at the moment you show your code for approval. It is *undefined behaviour*. – Weather Vane Jan 19 '17 at 19:28
  • but since I allocated it on "heap" shouldn't I able to access it ? – hkn-a Jan 19 '17 at 19:32
  • 2
    You allocated the memory (on the heap) with `malloc`, but threw it away by assigning a local var's address to the pointer. *That* memory is on the stack, so will be overwriiten as soon as it is needed. – Weather Vane Jan 19 '17 at 19:33
  • thank you @WeatherVane for such great answers, is that means I have to create a for loop to write data(specifically to create an array) to the space that I allocated with malloc ? – hkn-a Jan 19 '17 at 19:48
  • You are going off the question now. If you return a pointer from a function, it must be to memory that will still exist. – Weather Vane Jan 19 '17 at 19:53
  • @hkn-a yes, that's what you should do. `memcpy` does an optimal loop for you, too. – Quentin Jan 19 '17 at 20:53
  • conversion between integer and pointer is implementation defined, so it's up to the implementation. – user3528438 Jan 19 '17 at 22:14
  • @Quentin thanks for replying, memcpy is really helpful. – hkn-a Jan 20 '17 at 08:39

0 Answers0