you have declared input matrices of size [size_b_row1]. This I assume should be a constant value. Then you have declared this same identifier as an int variable.
{
int size_b_row1;
int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}
}
this is actually wat the code was i tried a few things like vector got confused but i still get the same error expression must have a constant value could you please help me i have been debugging this error from 4 days
When you have int foo[N] in your code, the compiler must know at compile time what the value of N is.
The value of size_b_row1 must be set before/outside your function definition.
When you have
1 2 3
void foo( int bar ) {
int bar;
}
You show two different variables with same name. The first is the parameter to the function, and the second is a local variable. If you then use bar, which one do you mean?
You do not set any value to your local variable size_b_row1. Then you use that variable anyway.
You declare that you have a double matrix_t[1][size_b_row1], but then you reference matrix_t[1][0]. That item is outside of your array.
Study the std::vector harder. It may look confusing at first, but it will make your life simpler.
PS. Despite your thread title, there is nearly no dynamic allocation here.
Thank you so much for the response , the error is expression must have a constant value for that we need to allocate memory dynamically so i wrote the topic as dynamic memory allocation
here is my code again if you want to
i will get an error on the first line itself
void transpose_vec(double matrix[size_b_row1],
double matrix_t[1][size_b_row1])
{
int size_b_row1;
int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}
}
You want to input matrices of length size_b_row1. This value of size_b_row1 varies in your calling function. What I suggest is as follows:
1. Don't use "dynamic memory allocation". If you must use it, then you have to use vectors. There must be some maximum limit of this size_b_row1. Lets say that limit is MAX_LIMIT;
oh my god!! it runs you are awesome!! thank you so much, but seriously my boss says he just wants dynamic memory allocation i did like this now i still get error
yeah size of row can maximum be 40 should i introduce a constant double some thing like this const double size_b_row1 =40; now i just get an error saying expression must have pointer to object type between the for loop(the error)
void transpose_vec()
{
std::vector<int> temp(size_b_row1);
using namespace std;
std::vector<double> matrix(size_b_row1);
std::vector<double> matrix_t(size_b_row1);
int i;
for(i=0;i<size_b_row1;i++)
{
matrix_t[1][i] = matrix[i];
}
We can suggest a vector implementation code for you but it will not solve the purpose. What i instead propose is:
1. As pointed out earlier -
- put your code in code tags
- you are again trying to modify the value of matrix_t[1][i]. Please note that array indices start from 0.
2. From your code it seems there is a lot of confusion in function declaration, variables scope, vector implementation etc. You need to get these concepts clarified.
3. A slight hint, to implement matrices using vectors, you can maybe use nested vectors like the following: std::vector<std::vector<double> > matrix_t (size_b_row1);