There is that way to set elements on array - int rgArrayNum [] = {16, 2, 77, 40, 12071};
How can I do same way on pointer with new ? I tried int *pArrayNum = new [] = {4 ,3 ,3} ; but it didn't worked .
In c++11, you can write:
int *pArrayNum = new int[3]{4, 3, 3};
However, in c++03 array new initialization is not allowed; you'd have to initialize the members individually or by copy from an array on the stack:
int rgArrayNum [] = {16, 2, 77, 40, 12071};
int *pArrayNum = new int[sizeof rgArrayNum / sizeof rgArrayNum[0]];
std::copy(&rgArrayNum[0], &rgArrayNum[sizeof rgArrayNum / sizeof rgArrayNum[0]],
pArrayNum);
In C++03 and earlier, you can't initialise the values of a dynamic array to anything except zero.
You can achieve something similar in C++11:
int *pArrayNum = new int [3] {4, 3, 3};
or if you don't mind using a container to manage the memory for you:
std::vector<int> array = {4, 3, 3};
You have to create the array not with integers but with integer pointers.
int* rgArrayNum2 [] = {new int(16), new int(16), new int(16), new int(16), new int(16)};
//test
int* test = rgArrayNum2[2];
*test = 15;
now rgArrayNum2[2] is 15.
Related
Recently while going over C++, I dynamically allocated space for an array and tried to initialize it with 8 default values on the next line.
int* intArray = new int[8];
intArray = {1, 2, 3, 4, 5, 6, 7, 8};
Visual Studio didn't like that, and underlined the 2 in red, as if there is a problem there, only to give me the error "too many initializer values"
I don't know if I used incorrect syntax or if you're just not allowed to set the value of an array that way after declaration. Any ideas?
Okay, it seems this also isn't working for regular non-pointer arrays too, I must be just doing something dumb.
intArray is not an array, it's a pointer. A pointer can't be initialized with an initializer list.
Dynamic allocated memory can be initialized at the moment of allocation:
int* intArray = new int[8] {1, 2, 3, 4, 5, 6, 7, 8};
C array can be initialized also at the declaration:
int intArray[8] = {1, 2, 3, 4, 5, 6, 7, 8};
C++ allows static allocation without dimension parameter
int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8};
where for dynamic allocation
int *intArray = new int[8] {1, 2, 3, 4, 5, 6, 7, 8};
the matching dimension must be passed.
Here is an example of what I am trying to do:
int size = 10;
int *data = new int[size];
int *array_1 = &data[0];
int *array_2 = &data[size/2];
//fill array_1 and array_2 with data
sort(array_1, array_1+size/2);
sort(array_2, array_2+size/2);
//now, is it possible to merge the 2 sorted arrays?
For example if array_1 = {1, 4, 7} and array_2 = {3, 5, 6}
I am trying to make data = {1, 3, 4, 5, 6, 7}
You want std::inplace_merge. See http://www.cplusplus.com/reference/algorithm/inplace_merge/
I'm working on a program using arrays and I'm trying to figure out
First, with the following array declaration, what is the value stored in the scores[2][2] element?
int scores[3][3] = { {1, 2, 3} };
And also with this array declaration, what is the value stored in the scores[2][3] element?
int scores[5][5] = {5};
Could someone please explain this for me.
int scores[3][3] = { {1, 2, 3} };
is equivalent to:
int scores[3][3] = { {1, 2, 3}, {0, 0, 0}, {0, 0, 0}};
The other one is similar. You know the answer.
Array indexing is zero-based.
Which means that for: int foo[3] = {10, 20, 30};
foo[0] is 10
foo[1] is 20
foo[2] is 30
For multidimensional arrays, you should think of them as arrays of arrays.
So this would create an array containing two int[3]s: int foo[2][3] = {{10, 20, 30}, {40, 50, 60}};
foo[0][0] is 10
foo[0][1] is 20
foo[0][2] is 30
foo[1][0] is 40
foo[1][1] is 50
foo[1][2] is 60
C supports partial initialization. In which it will default all non initialized values to 0.
So if you were to do this: int foo[3] = {5};
foo[0] is 5
foo[1] is 0
foo[2] is 0
Similarly for a multidimensional array: int foo[2][3] = {5};
foo[0][0] is 5
foo[0][1] is 0
foo[0][2] is 0
foo[1][0] is 0
foo[1][1] is 0
foo[1][2] is 0
For example, how can I move the 1st, 5th and 10th elements in array A to a new three-elements array B without assigning separately for three times?
In C, just declare and initialize a new array with the selected elements of your array. No assignment needed.
int main(void)
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[3] = {a[0], a[4], a[9]};
return 0;
}
Remember that initializers for arrays with automatic storage duration does not have to be constants.
Just do the three assignments! Why do you avoid it?
int ar1[10], ar2[10];
ar2[0] = ar1[0];
ar2[4] = ar1[4];
ar2[9] = ar1[9];
However, if you have lots of indices to move, perhaps you need another way.
I suggest this:
int ar1[1000], ar2[1000];
int indices[] = { 1, 3, 54, 6, 23, 35, 9, 42, 44, 995, 722, .... };
for (int i = 0; i < sizeof(indices) / sizeof(indices[0]); i++)
{
ar2[i] = ar1[i];
}
So I have a pointer to a 2D array like so:
int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
int* ptr[sizeof(board[0]) / sizeof(board[0][0])] = board;
I'm trying to follow this example. But for some reason I'm getting the error:
IntelliSense: initialization with '{...}' expected for aggregate
object
Any idea what the problem is?
Assign pointer to the first element of the array like below
int (*ptr)[5] = board;
Note: Column size [5] in the pointer declaration should be equal to the original 2 dimension array column size [5].
Declaring row size [3] is optional.
int main() {
int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
/*
// 3 Rows 5 Columns Matrix
int board[3][5] = { {3, 5, 2, 2, 1 },
{3, 4, 34, 2, 2 },
{3, 4, 3, 223, 923}
};
*/
// Assign pointer to the first element of the array
int (*ptr)[5] = board;
for(int i=0; i< (3*5); i++) {
std::cout<<(*ptr)[i]<<std::endl;
}
return 0;
}
A 2D array is not the same as an array of pointers. You cannot directly convert one to the other.
I just needed to put () around the *ptr. I have no idea how this fixes it but now I can do ptr[1][2].