1

I am mixed up on my pointers and references. I want to create a pointer in a main function and set it to null. I would like to pass that pointer to another function that creates an array on the heap, fills it, and returns the size of the created array.

I tried to find another article about this topic but failed to find one that allocated memory from within the function. The example code below illustrates the concept but I am not writing a program to accomplish any specific task.

int fillarray(/* pointer to an array */){
 // malloc the array to size n
 // fill array with n elements
return n;
}
int main(){
 int* array = NULL;
 int size = fillarray(/* pass the pointer by reference */);
 for(int i = 0; i < size; i++) printf("%d\n", array[i]);
 free(array);
return 0;
}

UPDATE:

Thank you all for your comments. I learned a ton about pointers working through this problem. The code below accomplishes what I need it to. Thank you @Lundin. Your answer led me to the actual solution. Thank you @ssd as well. Your visual helped me gain some intuition on what I was looking at in the code.

int fillarray(int** array){
 *array = (int*)malloc(2 * sizeof(int));
 (*array)[0] = 0;
 (*array)[1] = 1;
return 2;
}
int main(){
 int* array = NULL;
 int size = fillarray(&array);
 for(int i = 0; i < size; i++) printf("%d\t", array[i]);
return 0;
}
trincot
357k38 gold badges282 silver badges338 bronze badges
asked Jan 7, 2020 at 15:12
6
  • 2
    Did you try searching on this site ? [c] pass pointer by reference. For what it's worth, it is arguably easier to pass the size as a by-address argument and reserve the function retval for the pointer result. Commented Jan 7, 2020 at 15:16
  • What would be the point of returning a number of elements (that would be a fixed number)? I ask because the way I would allocate the array might change according to the purpouse of fillarray() function Commented Jan 7, 2020 at 15:17
  • @Cubo78: not necessarily a fixed number; the code in fillarray() or called from fillarray() could prompt the user to specify the size, for example, or read it from a file, or ... Commented Jan 7, 2020 at 15:18
  • @JonathanLeffler you are right.. Commented Jan 7, 2020 at 15:29
  • As I said in a comment on Lundin's answer which was quoting your source code: I would not use array as a variable name for a pointer. The differences are usually not very obvious (you can index, reference and pass as arguments both of them in the same way, as far as the source code is concerned) -- but they are really two very different beasts. Among others, you can assign another address to a pointer, and sizeof() usually is very different, and for an array &array == array (why?), while for a pointer usually &p != p. Commented Jan 7, 2020 at 17:02

2 Answers 2

4

Strictly speaking there are no "references" in C, everything is passed by value. Although the term "pass by reference" is language agnostic and really just means pass an address to the data. (C++ has a language concept called references, but it's really just glorified read-only pointers.)

So to achieve what you want, you have to pass the address of the pointer itself. That way, the function can access the pointer and set it to point at a new address. Thus declare the function as int fillarray (int**)call the function as fillarray(&array). The result of malloc needs to be assigned to where the pointer-to-pointer points at - it points at the address of the original pointer declared variable in main().

As for allocating a variable size, you will have to add an additional plain integer parameter to the function for that.

answered Jan 7, 2020 at 15:31
Sign up to request clarification or add additional context in comments.

2 Comments

@Peter-ReinstateMonica Agree, but this comment is I think more appropriate for the OP´s question.
@RobertS-ReinstateMonica Yes, true.
1

I do agree & consent Lundin's answer but to be more conceptual, I'll add the drawing below, for I had the same pain in the past understanding pointers.

  • The first integer pointer below (int *a) is at memory address 4c4b40 and its value is set to zero (which means this pointer a is set to null).

  • The second integer pointer (int *b) is at memory address 4c4b48 and its value is set to 4c4b58 (which means, this pointer points to memory address 4c4b58 with a length of integer). If that address corresponds to variable int x = 16 (hexadecimal 10 is equal to 16), then dereferencing pointer b will give you an integer value of 16 (*b = 16 because x = 16).

  • The third memory is another character string pointer, which points to a memory address somewhere down below.

Returning to the answer:

If you are to change the address of the first pointer (a) in order to point to the variable x, then the function you're calling needs to receive the address of the pointer (not the pointer, itself); i.e. &a instead of a, which corresponds to 4c4b40. The function called will receive the address 4c4b40 as a memory address and change its value from 000000 to 4c4b58. The function decoration, of course, will contain an int **; for, if an integer pointer is an int *, a pointer to a pointer will be an int **.

enter image description here

answered Jan 7, 2020 at 17:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.