+ 2
C dynamic memory
//i want create dynamic array with 3 int int *x = (int *)calloc(3, sizeof(int)); //set the first array member *x = 10; //set the sixth array member *(x+5) = 20; std::cout << *(x+5); //output: 20 the questions: is there another way to set array member? i hope i can using [] and not using arithmetic operation how can i set 6th member of array while i just set 3 in calloc and i can access it too?
5 Answers
+ 2
thanks
+ 1
ofc mate, here:
int* arr = new int[size];
remember to delete it afterwards when you are done with it.
And you refer to it as arr[0], arr[1] etc
+ 1
@Paul
i know that using new. but using malloc or calloc with []?
0
I do not think that zi get the question. You want to create such a thing but without new, just using one of those?
0
have you tried using something like that tho?
int* arr = malloc(2*sizeof(int));
arr[0] = 3;
arr[1] = 9;
etc..
?