Two dimensional array in c++ initialization to one value [duplicate] - c++

This question already has answers here:
how to set or initialize default value for all elements of a table or 2d array or multdimentional array
(4 answers)
Closed 3 years ago.
I have two dimensional char array in c++ and want to give all element one value without using loops
is there any function to achieve it ?

char demo[5][7];
memset(demo, '\0', sizeof(demo));
Based on the provided info, does this do what you want?
Since you're using C++ I'd question to use a native char array, as long as you're not required to and/or needing to use the stack.
Otherwise its idiomatic to use the collection classes provided by the STL.
Adding to the answer as requested, this obviously does what the question asks for: initializing the memory of a char array.
Please see the docs for more information on memset: https://en.cppreference.com/w/cpp/string/byte/memset

Related

how to select/replace all elements of a row/columns in a matrix? [duplicate]

This question already has answers here:
Accessing an entire row of a multidimensional array in C++
(3 answers)
Closed 5 years ago.
I know in matlab we can do Matrix(1,:), and it gives us all the elements of the first row for all the columns.
Is there anyway to do that in c++?
Like if I have int Matrix[3][3]={{1,1,1},{2,2,2},{3,3,3}}; how I can get/or replace {1,1,1} all together?
In the case of your matrix
int Matrix[3][3]={{1,1,1},{2,2,2},{3,3,3}};
it is merely a two dimensional array. The index of an array starts with 0. Matrix[0] refers to the first element, which is {1,1,1}. I guess this is what you were looking for.
If you want to replace the first element of the matrix then you need to replace element by element. It could be for example
for (int i=0; i<3; i++)
Matrix[0][i] = 4;
The matrix would then be
int Matrix[3][3]={{4,4,4},{2,2,2},{3,3,3}};
In the book The C++ Programming Language from Bjarne Stroustrup you can find some discussion about implementing a matrix class.
Also you might find matrix implementations out there in the internet.
Since I believe you are relatively new to C++ I also suggest to learn about the STL, in particular the vector class, which is basically a dynamic array.
I'd also suggest to use the array class if you use C++11 or later.
A more advanced approach would be to use an array of pointers. This would allow you to replace an entire row. However, this would be more C and less C++. The C++ way would be to implement a matrix class.

C++ what if I don't know array size and need to receive an array [duplicate]

This question already has answers here:
How do I find the length of an array?
(30 answers)
Closed 6 years ago.
I have a function like:
function(double* numbers)
I know that if I know the size of the numbers array, I can then do
double* numbers = new double[size];
function(numbers)
This will return the value in numbers correctly, however, now I don't know the size of numbers, I still want to use function function, which is a third party library function which I don't want to change, is there a way to get the array out without knowing the size of array?
No, there's no standard way to retrieve the size of the array at run time from a mere pointer. I suggest you either use std::array for fixed-size arrays, or std::vector for variable-length arrays. An less safe alternative would be to pass the size of the array together with the pointer.

passing array of object to member function in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am beginner in oop in c++ fairly say in programming. i am trying to make the array of objects and pass it to member function sort(class_name[size]) to sort by their id number.
i have tried so hard for passing arrays of objects but cant find the solution.
please help on it.
Thanks in advance.
Classically and erroneously, arrays have been passed by:
Name of the array
Address of first element
The biggest issue is the length (capacity) of the array. In C and C++ the array (not std::array), does not store it's capacity. So the receiving function has no clue to the end of the array.
Another issue is the number of elements in the array. In C and C++, the array structure does not maintain the number of items in the array. An array may have a capacity of 32 elements, but only 3 loaded.
If you write your own functions and use an array, please provide arguments for the array, the capacity and the number of elements in the array.
Edit 1:
One problem with passing a pointer or the address of the first element is that the pointer only points to a single character, and technically, not an array object. The assumption has been that an array is a container where the elements are consecutive with no other data types between. Thus you only needed to know the first position of the array.
A problem is that I could pass a pointer to a single character and the receiving function would treat the data as an array of characters instead of a single character. Thus creating undefined behavior often known as buffer overruns.
So to be more type safe, you should use the array syntax when passing arrays rather than a pointer to the first element. For better safety, using std::vector or std::array or put the array in a structure and pass the structure.

can we have variable size array at arduino? [duplicate]

This question already has answers here:
Vectors in Arduino
(7 answers)
Closed 9 years ago.
I have the following variables:
char* words[20];
char* tracks[20];
where they represent the number of tracks and words saved in a database on a SD card. My question is that, I would like the array of words and tracks to be of variable size and not static, that it is I don't want to specify the size of the array.
I suggest you take a look at Standard C++ for Arduino It lets you use STL (standard template library) vector

When declaring a new array using 'new int[n]' why doesn't it appear in locals list? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
View array in Visual Studio debugger?
I declare an array of 50 elements using:
int *ans;
ans = new int[50];
yet when I'm looking at the 'Locals' list and try to expand the array, it have only one
element, why's that? am I doing something wrong?
The locals list just knows you have a pointer to an integer. It doesn't have a good way to know how big that array is, unfortunately. It could probably be made more sophisticated, or maybe you can create your own custom viewer for that variable - it depends on what IDE you're using probably.