I have an array of struct having const variables;
I am initialize by using constructor initializer list,
but if I make an array of that struct how to initialise all the elements of array of that structure.
if the struct contains no const var, then it is easily done
if the struct contains no const var, then it is easily done
#include<iostream>
struct s_Nconst{
int num;
int x1;
// s_Nconst(int);
};
int main(){
s_Nconst sN2;
sN2.num=5;
std::cout<<"initial values of num\t"<<sN2.num<<std::endl;
//making array of s_Nconst struct
s_Nconst sN3[sN2.num];
for(int i=sN2.num-1; i>-1; i--,++sN2.num){
sN3[i].x1= sN2.num;
std::cout<<"sn3["<<i+1<<"]\t"<<sN3[i].x1<<std::endl;
}
return 0;
}
but if suppose the struct comprises of const var, then I am able to make single object of the struct using the constructor initializer list, but if I am going to make array of the struct it is giving error
#include<iostream>
struct s_const{
const int x1,y1,z1;
static int num;
s_const(int,int,int);
};
s_const::s_const(int x,int y, int z)
:x1(x),y1(y),z1(z) {
std::cout<<"initial values of x1,y1,z1\t"<<this->x1<<" "<<this->y1<<" "<<this->z1<<std::endl;
}
/*struct s_Nconst{
int num;
int x1;
// s_Nconst(int);
};
s_Nconst::s_Nconst(int x)
:num(x){
std::cout<<"initial values of num\t"<<this->num<<std::endl;
} */
int main(){
int a,b,c;
a=1;b=2;c=3;
s_const s1(a,b,c);
//array of s1 is giving error
s_const sc[3];
//s_Nconst s2(++c);
//s_Nconst sN2;
//sN2.num=5;
//std::cout<<"initial values of num\t"<<sN2.num<<std::endl;
//making array of s_Nconst struct
//s_Nconst sN3[sN2.num];
//for(int i=sN2.num-1; i>-1; i--,++sN2.num){
// sN3[i].x1= sN2.num;
std::cout<<"sn3["<<i+1<<"]\t"<<sN3[i].x1<<std::endl;
//}
return 0;
}
is there any way to make array of structs having const variables and how to initialize them?
You can create an array of const objects, but they have to be initialized. For example, for your structs it should be:
s_const sc[3] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}};
On a side note, you might prefer std::array to built-in arrays.
Related
I have some problem with my code.
I wanna using struct array to calculate something.
But my array size is dynamic not static.
Here's my code
#include <iostream>
#define MAX 5
using namespace std;
struct Point{
int x,y;
}arrayy[MAX];
int main(){
int num_howmanytime,num_max;
cin >> num_howmanytime;
while(num_howmanytime--){
cin >> num_max;
}
}
As you can see the num_max is dynamic, it will change value according user input what value on it.
So my question is:
How to let MAX get the same value with num_max
I know that that is not possible so must use others ways, such as
How to let MAX get the same value with num_max?
That's impossible. MAX is a compile-time constant (that you would better declare as e.g. constexpr std::size_t max = 5; instead of using the preprocessor), while num_max is a value determined at runtime.
The difference with respect to array sizes is that you must dynamically allocate the memory for arrays with a runtime-dependent size. As suggested in the comments, you typically don't do that manually, but instead rely on an existing type, often a template.
Example for your case:
#include <vector>
std::vector<Point> points;
cin >> num_max;
// Set the runtime array size, let the vector allocate its memory.
// Also, provide a default initial value for all Point instances.
points.resize(num_max, {0, 0});
Note that passing the default Point instance {0, 0} to std::vector::resize is optional here, as the function will value-initialize the newly created elements, which is is zero-initialization in this case.
There are some ways here.
In C++
You can use std::vector
struct Point {
int x, y;
};
int main() {
int num_howmanytime, num_max;
cin >> num_howmanytime;
while (num_howmanytime--) {
cin >> num_max;
std::vector<Point> arrayy(num_max);
}
return 0;
}
In C(since C99)
You can use VLA(Variable-length array)
struct Point {
int x, y;
};
int main() {
int num_howmanytime, num_max;
scanf("%d", &num_howmanytime);
while (num_howmanytime--) {
scanf("%d", &num_max);
struct Point arrayy[num_max];
}
return 0;
}
In C(before C99)
You can allocate the memory dynamically
struct Point {
int x, y;
};
int main() {
int num_howmanytime, num_max;
scanf("%d", &num_howmanytime);
while (num_howmanytime--) {
scanf("%d", &num_max);
struct Point *arrayy;
arrayy = malloc(sizeof(struct Point) * num_max);
}
return 0;
}
Error: braces around scalar initializer for type int*
I'm trying to pass an array to the function but I keep getting this error in the initialization step.
How can I fix this?
#include <iostream>
using namespace std;
void func (int *p[4]);
int main()
{
int *p[4]={ {1,4,5},{3,5,6},{6,6,2},{6,5,3}}; //The error appears here
func(p);
return 0;
}
void func (int *p[4])
{
for(int i=0;i<4;i++)
{
for(int j=0;j<1;j++)
{ cout<<p[i][j]; }
}
cout<<" \t";
}
Problem is what is p.
Whit this definition:
int *p[4];
this is 4 element array of pointers to int.
I guessing you wanted this:
int (*p)[3];
pointer to 3 element array of ints.
Also to use initialization you need an array.
Here is working example.
The variable p is an array of pointers. { 1, 4, 5 } is not a pointer.
You need to make p an array of arrays:
int p[4][3] = { ... };
Because of that change, you have to change the func function argument as well, as p will now decay to a pointer to an array, of type int (*)[3] (that is, the argument for func should be int (*p)[3]).
This would work, however you are going to lose array information because they will decay into pointers:
int arr1[] = { 1,4,5 };
int arr2[] = { 3,5,6 };
int arr3[] = { 6,6,2 };
int arr4[] = { 6,5,3 };
int *p[4] = { arr1, arr2, arr3, arr4 };
I have
struct board{
int x[3];
int y[3];
};
// in the class PatternReader
board PatternReader::testPattern1DX() {
struct board aBoard;
int x[3] = { 1,1,1 };
aBoard = x;
return aBoard;
}
Error is "incompatible types in assignment of int *".
How do you set arrays that are inside a struct?
You cannot assign arrays. However, you can can initialize the struct:
board PatternReader::testPattern1DX()
{
board aBoard = {
{1, 1, 1},
{2, 2, 2}
};
return aBoard;
}
This initializes y as well as x.
Add an initializer function to the board struct:
struct board
{
int x[3];
int y[3];
void initX(int* values)
{
for(int i = 0; i < 3; i++)
x[i] = values[i]
}
};
Then use it:
board PatternReader::testPattern1DX()
{
struct board aBoard;
int x[3] = { 1,1,1 };
aBoard.initX(x);
return aBoard;
}
Your code
int x[3] = { 1,1,1 };
aBoard = x;
is creating a variable of type int* with the initial values 1,1,1. You are then trying to assign that to a variable of type board. You don't want to do that. I think you intended:
int x[3] = { 1,1,1 };
aBoard.x = x;
Note the .x at the end of aBoard. However, this is still wrong. You can't assign arrays like that. Look up "copying arrays" instead. Is there a function to copy an array in C/C++?
Honestly, I would suggest making board a class with constructors and and then you can make the constructors behave as you want, and also look into overloading assignment operators. But for now, trying copying from x to aBoard.x is probably what you want.
I have problem i need to convert from my "Array" structure to std::vector<int>... the point is i have a dynamic matrix who purpose is being Database. But at some point i need to move some values from the 'Array' to a vector. and i get the fallowing error
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits /stl_iterator_base_types.h:166:53: error: 'int' is not a class, struct, or union type
Anyone has some clue how to achive this thing?
structure:
const int days=31;
const int exp=6;
struct Arr{
int days;
int exp;
int **M;
};
typedef Arr* Array;
vector:
vector <int> vec(31);
EDIT:
int dayExp(int day, Array &M){
int s=0;
for(int i=0;i<6;i++){
s += M->M[day][i];
}
return s;
}
void srtDesc(Array &M){
vector <int> vec(31);
for(int i=0;i<31;i++){
vec[i]=dayExp(i, M);
}
sort(vec[0],vec[1]);
for(int i=0;i<31;i++){
cout<< vec[i];
}
}
Your Arr struct does not implicitly convert to int. First of all, your struct contains 3 int types, whereas a vector<int> is one int at a time.
If you want to put your Arr into a vector, you should create a std::vector<Arr> and then call push_back on it to put your Arr structs in it.
std::vector<Arr> myvec;
myvec.push_back(someArr);
You will need to create for loop that goes through the length - 1 of your array and calls push_back to put each item in the vector
this question is regarding the syntax of an array of array of structs.
I have a struct that takes in two ints:
struct point
{
int x, y;
};
I have created another struct that takes in 8 of these structs:
//Creating an Array of Array of structs
struct Arraypoint
{
point variable[8];
};
//Not sure if this is the correct way to do it.
Now, in main, I want to declare an array variable of type Arraypoint with 8 indices, so effectively I will have 8 * 8 = 64 elements of struct point and 128 ints (64 x and 64 y).
Also, how would I access an individual element struct point from the array Arraypoint?
Okay after having declared in main lets say Arraypoint is 2.
Arraypoint arr[2];
How do I initialize the elements without having to type in arr[0].variable[0].x = ... or without using for loops.
Why can't I do the following, it doesn't seem to work.
Arraypoint arr[2] = { {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
{(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)} }//xy are rand
I have used curly braces in my code, the error returned is missing braces around initializer for type point and too many initializers for type Arraypoint.
In C++, you'd just write:
Arraypoint arr[8];
An individual point could then be accessed via:
arr[i].variable[j];
More practically, though, you'd probably be better off using e.g.
std::vector<std::vector<point> >
or writing your own class with an overloaded operator(int i, int j). For example:
class PointMatrix
{
private:
std::vector<point> m_points;
public:
PointMatrix() : m_points(64) {}
point& operator()(int i, int j) { return m_points[8 * i + j]; }
const point& operator()(int i, int j) const { return m_points[8 * i + j]; }
};
PointMatrix mat;
m(3, 4).x = 23;
got it: ideone.com/ix3hC. Arraypoint::variable has to have it's own { } pair.
struct point
{
int x, y;
};
#define P {0, 0}
struct Arraypoint
{
point variable[8];
};
#define V { P, P, P, P, P, P, P, P}
#define AP { V } //this is the pair you missed
int main() {
Arraypoint arr[2] = { AP, AP };
}
struct Arraypoint arraypoints[8];
is what you're after, I think. To use them:
int firstx = arraypoints[0].variable[0].x;
This isn't so pretty though
struct point { int x, y; };
struct point[8][8] arraypoints;
Is probably better? Don't know what exactly you're after though.
To create an array of Arraypoints, you can do:
Arraypoint arr[8];
To access an element:
arr[i]
will return the i'th Arraypoint element
arr[i].variable[j]
will return the j'th point in the element
arr[i].variable[j].x
will return the x coordinate of that point.
So I realized why I couldn't declare my array as such,
Arraypoint arr[2] = { {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
{(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)} }
//xy are randomn integer values
its because in my struct declaration of Arraypoint, it takes in 8 elements of type point. So
I have to create variables of type point to store(x,y) and then i could store this variable in Array point.
point point1 = {x,y}, ...;
Arraypoint arr[2] = { {point1,point2,point3,point4,point5,....} };
Just for anyone in the future who stumbles across the same problem.