This question already has answers here:
Why does int*[] decay into int** but not int[][]?
(4 answers)
Create a pointer to two-dimensional array
(10 answers)
Closed 2 years ago.
In C++,
We can do the following:
#include <iostream>
int main()
{
int arr[] = {1,2,3};
int *p = arr;
return 0;
}
However this dosen't seem to work:
#include <iostream>
int main()
{
int arr[][3] = {{1,2,3},{4,5,6}};
int **p = arr;
return 0;
}
Can somebody please explain me why it does not work for 2d arrays.
Also when dealing with pointers and 2d arrays, consider the following code:
#include <iostream>
int N = 5;
void func(int arr[][N]){
return;
}
int main()
{
int arr[N][N];
return 0;
}
The above code doesn't work either and i don't know why? Error: expression must have a constant value (Variable N)
Related
This question already has answers here:
Why are "double braces" needed in declaration of multi-dimensional array using stacked std::array?
(2 answers)
Closed 2 years ago.
int data[][4] = { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} };
I want to convert this into multidimensional std::array
array< array<int,4>, 3 > stddata = { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} };
like this.
But error occur in this code. Why does this error occur? and how can I change reset part { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} } to { , } numbers.
Update:
Here is code using std::array
#include <iostream>
#include <array>
int main() {
// your code goes here
std::array< std::array<int, 4>, 3> stddata = {{{1,2,3,4}, {9,8,7,6}, {2,4,6,8}}};
return 0;
}
If the usecase is to add / remove more values during the lifetime of the container, we can use std::vector. Here is sample code:
#include <iostream>
#include <vector>
int main() {
// your code goes here
std::vector< std::vector<int>> stddata = { {1,2,3,4}, {9,8,7,6}, {2,4,6,8} };
return 0;
}
This question already has answers here:
Range based for-loop on array passed to non-main function
(3 answers)
Closed 3 years ago.
I am trying to print values of array in called function using foreach loop but facing compilation error. Using c++11 compiler in linux platform and using VIM editor.
Tried using C-style for loop and it worked, when the size is passed from calling function
#include <iostream>
using namespace std;
void call(int [], int);
int main()
{
int arr[] = {1,2,3,4,5};
int size = sizeof(arr)/sizeof(arr[0]);
call(arr,size);
}
void call(int a[],int size)
{
for(int i =0 ; i<size; i++)
cout << a[i];
}
For-each loop used in the below code, which fails to compile.
#include <iostream>
using namespace std;
void call(int []);
int main()
{
int arr[] = {1,2,3,4,5};
call(arr);
}
void call(int a[])
{
for ( int x : a )
cout << x << endl;
}
For-each loop in C++11 expects to know the size of array to iterate ? If so, how it will be helpful with respect to traditional for loop. Or something wrong i coded here?
I would look forward for your help. Thanks in advance.
Because int a[] as function parameter is not an array, it is the same as writing int *a.
You can pass the array by reference to make it work:
template <size_t N> void call(int (&a)[N])
working example: https://ideone.com/ZlEMHC
template <size_t N> void call(int (&a)[N])
{
for ( int x : a )
cout << x << endl;
}
int main()
{
int arr[] = {1,2,3,4,5};
call(arr);
}
This question already has answers here:
How to copy a char array in C?
(14 answers)
Assign array to array
(6 answers)
Closed 6 years ago.
I have this following code:
#include "stdafx.h"
#include<iostream>
using namespace std;
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
bool adj[x][x];
char n;
};
int main(){
Graf graf1;
graf1.adj = graf_adj;
}
in main function when i try to assing graf_adj to graf1.adj
graf1.adj = graf_adj;
complier gives me this error:
Error Expression must be a modifiable lvalue
Can anybody give a solution to this ?
Thank you
Now that you have added the type for the const:
Here is the solution using memcpy
#include<iostream>
#include <cstring>
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
bool adj[x][x];
char n;
};
int main(){
Graf graf1;
std::memcpy(&graf1.adj, &graf_adj, sizeof(graf1.adj));
}
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 8 years ago.
My code is quite simple:
#include <iostream>
using namespace std;
int test(int b[]){
cout<<sizeof(b)<<endl;
return 1;
}
int main(){
int a[] ={1,2,3};
cout<<sizeof(a)<<endl;
test(a);
system("pause");
}
output of this code is:
12
4
which means that when a[] is transfered as a parameter to function test(),is has been deteriorated as a int *, so the output of size(b) is 4 ,instead of 12.So ,my question is , how can I get the actual length of b[] inside function test() ?
You could do this with a function template:
#include <cstddef> // for std::size_t
template<class T, std::size_t N>
constexpr std::size_t size(T (&)[N])
{
return N;
}
then
#include <iostream>
int main()
{
int a[] ={1,2,3};
std::cout << size(a) << std::endl;
}
Note that in C and C++, int test(int b[]) is another way of saying int test(int* b), so there is no array size information inside of the test function. Furthermore, you could use standard library container types which know their size, such as std::array.
This question already has answers here:
How to get function's name from function's pointer in Linux kernel?
(13 answers)
Closed 9 years ago.
#include<stdio.h>
int add(int i,int j)
{
printf("\n%s\n",__FUNCTION__);
return (i*j);
}
int (*fp)(int,int);
void main()
{
int j=2;
int i=5;
printf("\n%s\n",__FUNCTION__);
fp=add;
printf("\n%d\n",(*fp)(2,5));
printf("\n%s\n",*fp);
}
You can compare the function pointer with a pointer to function. Like this :
if (fp==add)
printf("\nadd\n");
There are no other (standard) ways1.
This
printf("\n%s\n",*fp);
is a compilation error.
There are platform specific ways. For linux, this works :
#include<stdio.h>
#include <execinfo.h>
int add(int i,int j)
{
printf("\n%s\n",__FUNCTION__);
return (i*j);
}
int (*fp)(int,int);
union
{
int (*fp)(int,int);
void* fp1;
} fpt;
int main()
{
fp=add;
fpt.fp=fp;
char ** funName = backtrace_symbols(&fpt.fp1, 1);
printf("%s\n",*funName);
}