0

So I've created a pointer to make a new array with the calloc() function. I want to access the array from this pointer as an array, but I'm not sure how to do that. Basically I want the array my_array to be the same as the contents in the array starting at the *stack pointer.

#include <stdio.h>
#include <stdlib.h>
    
    int main (int argc, char*argv[])
    {
        int capacity=5, *stack;
        int my_array[3];
        stack=(int*)calloc(capacity, sizeof(int));
        capacity++;
        
        stack=(void*)(realloc)(stack, capacity*sizeof(int));
        
        my_array[0]=*stack;
        my_array[1]=5;
        
            
    }



6
  • 2
    As you wrote it, my_array is complete independent from stack. You can copy a value from stack as you do but there is no fixed link between the two. By the way, it is not very clear what you want to do and why you want to do it. Commented May 24, 2021 at 8:47
  • 1
    @Aquila For example you can use the subscript operator like my_array[0]=stack[0]; Commented May 24, 2021 at 8:47
  • Do you want to copy contents of my_array to stack, or do you want my_array and stack to be the same array? Commented May 24, 2021 at 8:48
  • I wanted them to be the same array Commented May 24, 2021 at 8:49
  • Yeah he is saying it: I want the array my_array to be the same as the contents in the array Commented May 24, 2021 at 8:49

1 Answer 1

1

I wanted them to be the same array

Then you must have my_array an int* and make it equal to stack, and keep it synchronized each time you realloc stack.

At first glance, what you want to do will lead to unreliable software easily.

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

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.