1

I want to allocate an array inside of a function and to be able to use the pointer to that array outside of it as well. I don't know what's wrong with my code

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

void alloc_array (int **v, int n)
{
    *v=(int *)calloc(n,sizeof(int));
    printf ("the address of the array is %p",v);
}

void display_pointer (int *v)
{
    printf ("\n%p",v);
}

int main()
{
    int *v;
    alloc_array(&v,3);
    display_pointer(v);

    return 0;
}

I would expect to get the same address in both printfs, but that is not the case. What is my mistake?

2
  • 1
    printf ("the address of the array is %p",v); ---> printf ("the address of the array is %p",*v); Recall what the double pointer is pointing to, and what its value is. Commented Nov 28, 2017 at 18:38
  • @StoryTeller thank you. I understand now. I actually feel pretty stupid for not realising this. Commented Nov 28, 2017 at 18:41

3 Answers 3

3
void alloc_array (int **v, int n)
{
    *v = calloc(n,sizeof(int));
    printf("the address of the array is %p", *v);
    //                                   ----^
}

Note the additional star in my printf call.

Also, don't cast the return value of malloc / calloc.

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

6 Comments

You are right. My bad for asking such an obvious thing, I didn't pay enough attention.
It's an easy mistake to make when you are dealing with multiple levels of indirection.
Could you please explain why the return value shouldn't be casted? I was taught that we should do this because those functions return a type void pointer
In C, void* can be freely assigned to pointers of other types without cast and vice-versa.
|
1

v in alloc_array contains address of a pointer variable. That variable is vin main().

You don't care about it. Because it won't change. But it the content of v in main would.

You should print *v but why?

Because the content of v in alloc_array contains the address of the memory that you allocated.

But display_pointer doesn't need this. because here you pass the pointer variable itself and the local copy of it in the called function will contain the address of the memory that you allocated.

Comments

1

In this code snippet

int *v;
alloc_array(&v,3);
display_pointer(v);

the function alloc_array accepts the address of the variable (pointer) v and this address is outputted in the function

printf ("the address of the array is %p",v);

On the other hand the function display_pointer accepts the value stored in the variable v and this value is outputted within the function

printf ("\n%p",v);

Add one more statement in the function alloc_array and you will see the difference

printf ("the address of the original pointer is %p", ( void * )v);
printf ("the address of the first element of the allocated array is %p", ( void * )*v);

Comments

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.