1

I have the following code:

void dereference_pointer(int* pointer){
  pointer = (int[]){1, 2, 3};

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "1 | 2 | 3", like I wanted
}

int main(void) {
  int *pointer;

  pointer = malloc(sizeof(int)*3);
  dereference_pointer(pointer);

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "0 | 0 | 0", essentially undoing what happened inside the function

  return 0;
}

I'm not entirely sure pointer = (int[]){1, 2, 3}; is the correct way of pointing a pointer to a new array, but that's the only way I could do it that wouldn't give me a type error.

4
  • So what output do you expect? Commented Feb 11, 2018 at 14:51
  • @MichaelWalz I expected the array to have [1, 2, 3] outside the function Commented Feb 11, 2018 at 14:55
  • You should make that clear in the question. Commented Feb 11, 2018 at 15:00
  • @MichaelWalz Just did, thanks Commented Feb 11, 2018 at 15:05

2 Answers 2

2

Assigning pointer inside dereference_pointer doesn't change pointer outside the function, since it was sent by value. You could change it into:

#include <stdio.h>
#include <stdlib.h>

// Note the ** instead of *
void dereference_pointer(int** pointer){
  *pointer = (int[]){1, 2, 3}; // note dereferencing here
  //                        and here       and here      and here  :)
  printf("%d | %d | %d\n", (*pointer)[0], (*pointer)[1], (*pointer)[2]);
  // This prints "1 | 2 | 3"
}

int main(void) {
  int *pointer;

  pointer = malloc(sizeof(int)*3);
  dereference_pointer(&pointer); // by reference 

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "0 | 0 | 0"
  return 0;
}

This should fix the 0 | 0 | 0 you get in the end.


As for this:

I'm not entirely sure pointer = (int[]){1, 2, 3};

This is fine. Your pointer is pointing to an array which is ok. When the function returns, the you will be left with a pointer with the correct values.

You could instead have used this beauty :-P:

void dereference_pointer(int** pointer){
  **pointer=1, *(*pointer+1)=2, *(*pointer+2)=3;

  printf("%d | %d | %d\n", (*pointer)[0], (*pointer)[1], (*pointer)[2]);
  // This prints "1 | 2 | 3"
}

but your way is a one-liner and more readable IMO.

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

7 Comments

This works great! Thanks! But I was playing around with printing addresses and I noticed that after *pointer = (int[]){1, 2, 3}; inside the function, the pointer in main is pointing to a different place in memory. That made me think if maybe the assignment isn't already allocating space in memory, and I don't need that malloc before calling the function?
Not sure what you mean. The address of pointer (i.e. &pointer) itself does not change in my code since the malloc.
Weird. In this example the address of pointer1[0] changes after calling dereference_pointer
Does not compile...
I'm sorry. I inadvertedly made some changed to it after I posted it here. Could you try again? Thanks.
|
1

I'm not entirely sure pointer = (int[]){1, 2, 3}; is the correct way of pointing a pointer to a new array, but that's the only way I could do it that wouldn't give me a type error.

The pointer passed into the function is the array. This is because arrays, when passed as arguments, decay into a pointer to the first element. Therefore, you will need to pass the number of elements in the array. (Knowing the start address of the array doesn't tell you anything about the end).

Example:

void print(int *data, size_t n)
{
    for (size_t i = 0; i < n; ++i)
        printf("%d\n", data[i];
}

In fact, the function signature could also have been written as

int data[], size_t n

The caller code could look something like this:

int *data = malloc(n * sizeof(int));
/* populate the dynamically allocated array */
print(data, n);

Or it could look like this:

int data[MAX];
/* populate array */
print(data, MAX);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.