- #1
subwaybusker
- 51
- 0
I need to initialize the arrays of an object to zero, but when I compile it, it gives me gibberish.
Here's my code:
Here's my code:
Last edited:
void CBin::getImage(unsigned char** destImage)
{
for(int i=0;i<height*width;i++)
{
destImage[i] = image[i];
}
}
No, it means that one of the side-effects of your error is that you obliterated the pointers to all of the rows in destImage, and replaced them with copies of the pointers to the rows in CBin::image.subwaybusker said:Does that mean one of them is redundant?
In C++, a 2D array can be declared and initialized using the following syntax: data_type array_name[row_size][column_size] = { {value1, value2, ...}, {value1, value2, ...}, ... };
The data_type
refers to the type of data that will be stored in the array, while row_size
and column_size
indicate the dimensions of the array. The values enclosed in curly braces represent the elements of the array, with each inner curly brace representing a row of elements.
To dynamically allocate memory for a 2D array in C++, you can use the new
operator. The syntax is as follows: data_type **array_name = new data_type*[row_size];
for (int i = 0; i < row_size; i++) {
array_name[i] = new data_type[column_size];
}
This will create a 2D array with row_size
rows and column_size
columns, and each element can be accessed using the index notation (array_name[row_index][column_index]
). Don't forget to deallocate the memory using delete[]
when you're done using the array.
Yes, you can initialize a 2D array with user input in C++. You can use nested for
loops to traverse through the array and use cin
to get user input for each element. For example: for (int i = 0; i < row_size; i++) {
for (int j = 0; j < column_size; j++) {
cin >> array_name[i][j];
}
}
Note that the array must be declared and initialized before using user input to populate its elements.
To access and modify elements in a 2D array in C++, you can use the index notation (array_name[row_index][column_index]
). For example, to access the element in the first row and second column, you would use array_name[0][1]
. To modify the value of an element, simply assign a new value to it using the assignment operator (=
). For example, array_name[0][1] = 10;
will change the value of the element to 10.
No, a 2D array in C++ must have the same data type for all of its elements. However, you can create a 2D array of structures, where each structure can contain different data types. For example: struct Person {
string name;
int age;
};
Person people[2][3]; // creates a 2D array of Person structures with 2 rows and 3 columns
In this case, each element in the array is a structure that contains a string and an integer.