What am I missing in defining an array? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I have been trying to learn arrays and I can't seem to define an array.
const int size = 10;
??[size];
Can anybody spot my mistakes? Thank You.

To declare an array using a constant, you should use this syntax:
// constexpr is explicitly compile time constant
constexpr int size = 10;
int my_array[size]; // int array, can be changed to float
For dynamic array, use vector:
int size = ...;
std::vector<int> my_array;
array.resize(size);
You can also use modern statically sized array:
constexpr int size = 10;
std::array<int, size> my_array;

const int size = 10;
type array[size];
Is what you want. Substitute type for any type (i.e. int, float, bool etc.).
In this case, specifying array size using a const variable is valid, but there are many cases where it isn't. You'd probably want to learn the difference between const and constexpr.
Also, instead of learning by asking questions here, try finding a good c++ source: a book, online article series or a video tutorial. It'll definitely accelerate your education.

Related

What type is the next variable? [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 1 year ago.
Improve this question
So I had this question at an exam. And I don't know which answer is correct.
#include<bits/stdc++.h>
using namespace std;
struct Test{
int note;
char exam[20];
}qwerty;
int main()
{
cout<<qwerty.exam;
}
What is the type of qwerty.exam in the previous code? Is it char or char[20]?
They said the right answer is char[20]. But if the type is char[20], why can't I declare something like: char[20] exam; ?
Moreover, I found this on cplusplus website:
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:
type name [elements];
where type is a valid type (such as int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.
So I understand that the type does not depend on the number of elements. Please try to explain.
Thanks in advance!
But if the type is char[20], why can't I declare something like: char[20] exam; ?
That's not the correct syntax. The correct syntax is char exam[20]. You could also do something like this:
using char_array = char[20];
char_array exam;
So I understand that the type does not depend on the number of elements.
That's exactly the opposite of what's the case. Arrays of different sizes have completely different types. Therefore, char[20] and char[21] are completely different types.

Should I use the 'const' keyword in c++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am new to C++ and have come across the const keyword. I looked it up online and read that The main use is to prevent the changing the value throughout the program. I have these two snippets below
const int size = 56;
Compared to using
int size = 56;
Why should I use one over the other? What is the idea behind using it.
Yes, you should make all variables const if you are never going to change its value after initialization. This prevents bugs where you accidentally change a value you're not supposed to. You seem to be aware of this already.
In addition, there is something that you might not be aware of, which is that making an int variable const also makes it a constant-expression, so long as the initializer itself is also a constant-expression, e.g. the int literal 56. This allows you to use it in contexts where you need a constant-expression, e.g. as the dimension of a static array:
const int size = 56;
int a[size]; // ok
int size = 56;
int a[size]; // error
In c++ CONST is used when you want to declare something which will not change its value throughout the program. But if you accidentally try to rewrite its value, the compiler will through an error. Most of the time it is recommended to declare a variable of int type so you can redefine its value when it's required. CONST is used when you want to declare a static array i.e.
const int a = 100;
int sampleAray[a];
And int is used when you, the value of the variable will be modified at some point i.e.
int a = 12;
int arr[4] = {11,22,33,554};
for (int i=0; i<4; i++){
if(arr[i]%2 == 0){
a+=arr[i];
}
}
When you have a const variable holding something that should not change and accidentally write code that modifies the variable anyway, you get a nice compiler error and can correct your mistake easily. If the variable is non-const, the compiler cannot help you and you now have a bug that may be hard to find.

Returning pointer to an array of arrays created using new [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 3 years ago.
Improve this question
I have created an array of arrays using char (*H)[N] = new char[M][N];
I want to return this pointer to my main function. My question is what should the function return type be in this case? Am I allowed to have a return type as a pointer to an array of arrays.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes, this would be the syntax for your particular case:
char (*get_array())[N] {
return H;
}
But you really should consider using either std::unique_ptr<char[N]> or std::array<std::array<char, N>, M>.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes. One way to do that would be to define a type alias and use it as the return type.
using MyArrayPointer = char (*)[N];
MyArrayPointer foo()
{
auto ptr = new char[M][N];
return ptr;
}

What is "int * a[][10];"? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The question is in the title:
What is int * a[][10];
Is it an array of pointers to arrays of int? I tried to youse the clockwise/spiral rule but I am not sure...
int * a[][10];
is an illegal declaration in C++, as the storage size isn't known. You need to initialize it with arrays of 10 pointers to int, like so:
int* a[][10] = {{nullptr}}; // initialize with one array, the latter consisting of null pointers
or, even simpler,
int* a[][10] = {{}};
Once initialized, it becomes an array of arrays-10 of pointers to int.
int *a[][10], is to be read as pointer to 2D array of undefined rows (by default I guess it is 0) and 10 columns,with data read out row-wise in memory layout.
But this declaration is only possible in C.
If we declare a 2D array namely int abc[10][10] then we can make 'a' to point this array 'abc'.

How do explicitly create temporary by pointer? [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 8 years ago.
Improve this question
int i=int(); //OK
int* pi=int*(); //error
How to do it correctly?
You can default initialize both of them with uniform initialization.
int i{};
int *ip{};
The constructor like calls are unnecessary and convoluted, as of course, behave differently for some types as you've found out yourself.
If what you're trying to do is have the pointer point to space allocated for an int, you'll need to allocate with new
int *ip = new int;
or give it the address of an existing int
int i{};
int *ip = &i;
You can't construct a pointer. You get it by:
Getting a reference:
int x;
int* xpointer = &x; //pointer to x
Creating using new:
int* x = new int;
Try
int * i = new int();
This is basic C++, maybe you need to familiarize yourself with dynamic memory allocation. This is a good enough resource on the subject: http://www.cplusplus.com/doc/tutorial/dynamic/