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
I tried to write a simple function to concatenate two 2D double matrices.
double** concat(double **upmat,double **lowmat,int row,int col,int filecount)
{
double **temp=new double* [filecount*row];
for(int i=0;i<row*filecount;i++){
temp[i]=new double [col];
}
if (filecount>1)
std::copy(upmat,upmat+(filecount-1)*row*col,temp);
std::copy(lowmat,lowmat+row*col,temp+(filecount-1)*row*col);
return temp;
}
This function returns a 2D pointer. When I tried to access data from that pointer it shows invalid memory access error!!
temp, low, and high are all double** (pointer to pointer to double) , but you do std::copy as if they are double* (pointer to double) .
Related
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 4 years ago.
Improve this question
if(b[s][e]!=0)
{
return b[s][e];
}
else
{
int b[s][e]=palin(str,s+1,e-1)+2;
}
I am initializing this array with a value that is returned by function palin and it is giving the following error:
[Error] array must be initialized with a brace-enclosed initializer
With
int b[s][e]=...;
you define a new array of s arrays of e integer elements.
If you want to assign a value to b[s][e] just do it:
b[s][e] = ...;
You may not use:
int b[s][e]=palin(str,s+1,e-1)+2;
That defines a new array.
Perhaps you meant to use
b[s][e]=palin(str,s+1,e-1)+2;
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'.
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
I am trying to convert std::vector<T>::iterator to void *, but getting compiler error as wrong conversion. is there any way?
Just get the pointer to the vector element with dereference:
vector<Type>::iterator i = ...;
void* data = &*i;
Or to vector data:
void* data = vec.data();
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
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to convert some C++ code into the matlab
in the C++ there is following statement:
static double *L[2];
for (int y=0;y<2;y++)
L[y] = new double[size];
in matlab I'll need to initialize the L parameter with some initial value like 0.
Can you please explain what does the C++ code means?
I need the same for the following:
static double **a[2];
for (int x=0;x<2;x++)
{
a[x] = new double*[size];
for (int y=0;y<size;y++)
{
a[x][y] = new double[numstates];
}
}
BR
L is an array of two arrays, each of them is an array of size doubles. It's the same as
L[0] = new double[size];
L[1] = new double[size];
You may interpret L as a 2xsize matrix.
Likewise, a is 2xsizexnumstates matrix of doubles.
Can you please explain what does the C++ code means?
The first snippet is just initializing a (2 * size) matrix of double-precision floating point numbers.
The second snippet is doing the same, but it's initializing a (2 * size * numstates) matrix.