+ 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?

25th May 2018, 2:30 PM
Kevin AS
Kevin AS - avatar
5 Réponses
+ 2
thanks
5th Jun 2018, 3:03 AM
Kevin AS
Kevin AS - avatar
+ 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
25th May 2018, 2:37 PM
Paul
+ 1
@Paul i know that using new. but using malloc or calloc with []?
25th May 2018, 2:52 PM
Kevin AS
Kevin AS - avatar
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?
25th May 2018, 7:43 PM
Paul
0
have you tried using something like that tho? int* arr = malloc(2*sizeof(int)); arr[0] = 3; arr[1] = 9; etc.. ?
25th May 2018, 7:46 PM
Paul