C++ Question
-
Given the declaration. int *a; The statement. a = new int [50]; Dynamically allowcates an array of 50 components of the type? int, int*, pointer, address?
-
Given the declaration. int *a; The statement. a = new int [50]; Dynamically allowcates an array of 50 components of the type? int, int*, pointer, address?
You get a pointer to an array of 50 unique integers.
-
Given the declaration. int *a; The statement. a = new int [50]; Dynamically allowcates an array of 50 components of the type? int, int*, pointer, address?
int* a;
declaresa
as a pointer to anint
.a = new int[50]
allocates an array of 50int
, and then returns the address of the array in memory into a. then, you can codea[1]
to access the 2nd cell on the array. if you wanted an array of pointers to int, you should write this :int* a;
a = new int*[50];cheers,
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
int* a;
declaresa
as a pointer to anint
.a = new int[50]
allocates an array of 50int
, and then returns the address of the array in memory into a. then, you can codea[1]
to access the 2nd cell on the array. if you wanted an array of pointers to int, you should write this :int* a;
a = new int*[50];cheers,
TOXCCT >>> GEII power
[toxcct][VisualCalc]Not quite. That would be:
// 50 new pointers to integers. int **a = new (int *)[50]; // Parentheses might not be necessary. int *b = a[0]; // Assign one element of the array to an int pointer. int c = 0; // New integer on the stack b = &c; // Assign a value to the pointer. a[0] = &c; // Same effect as the above line. *b = 5; // Assign a value to the integer pointed to by b. *(a[0]) = 5; // Same effect as the above line.
(Note: I wrote this code in the input box and didn't compile it to double-check if it works, so if I got something wrong please correct me.) Like the other posters said, the code "int *a = new int[50]" allocates an array of 50 integers on the heap and assigns a to the address of the beginning of that array. -
Given the declaration. int *a; The statement. a = new int [50]; Dynamically allowcates an array of 50 components of the type? int, int*, pointer, address?