C++, expression must be a modifiable lvalue [duplicate] - c++

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));
}

Related

Reference class member variables give an error "uninitialized reference" in constructor [duplicate]

This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 7 months ago.
I have the following code:
#include<iostream>
using namespace std;
class Vec{
private:
int& var;
public:
Vec(int& tmp){
var = tmp;
}
};
int main(){
int x = 10;
Vec v1(x);
}
but it gives a compilation error:error: uninitialized reference member in ‘int&’ [-fpermissive]
How to resolve this?
You should use an initializer list.
#include<iostream>
using namespace std;
class Vec{
private:
int& var;
public:
Vec(int& tmp) : var(tmp) {}
};
int main(){
int x = 10;
Vec v1(x);
}

Function, Pointers and 2d arrays [duplicate]

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)

copying value of one struct to another [duplicate]

This question already has answers here:
segmentation fault when trying to cin into a string?
(3 answers)
How to use a C++ string in a structure when malloc()-ing the same structure?
(5 answers)
Closed 3 years ago.
I want to copy the values of one struct to another which has the same template.
Below is the sample code, where struct list is the template structure. Calling func1() must copy the contents of li to ref.
But when copy is performed segmentation fault occurs. Where I am going wrong with it ?
foo.cpp
#include<iostream>
#include <cstdlib>
class bar
{
public:
void func1(const list& li);
};
void bar::func1(const list& li)
{
listref ref = nullptr;
ref = (listref)malloc(sizeof(listref));
ref->a = li.a;//segfault occurs here
ref->b = li.b;
ref->c = li.c;
ref->d = li.d;
}
foo.h
#include<iostream>
struct list
{
std::string a;
int b;
int c;
const char* d;
};
typedef struct list* listref;
main.cpp
#include <iostream>
#include "foo.h"
#include "foo.cpp"
int main()
{
list l1;
std::string temp = "alpha";
l1.a = "alphabet";
l1.b = 60;
l1.c = 43;
l1.d = temp.c_str();
bar b;
b.func1(l1);
return 0;
}
You are mixing C and C++ concepts and this is what happens!
Your class list contains a member of the C++ type std::string, a complex class with semantics that you need to uphold.
Then you do a malloc over it!
Even if your size argument to malloc were correct (which it isn't; you merely gave it the size of a pointer), this does not properly construct anything. It should be new or std::make_unique!
Do not mix C and C++ idioms.

Why is passing a class instance into its own class constructor right? [duplicate]

This question already has answers here:
What is The Rule of Three?
(8 answers)
Is passing a C++ object into its own constructor legal?
(3 answers)
Closed 4 years ago.
I am curious about the following code which can execute correctly.
#include <iostream>
using namespace std;
class A
{
public:
A(int a=1, int b=2){
this->a = a;
this->b = b;
}
void print_a(){
cout<<a<<endl;
}
private:
int a;
int b;
};
int main() {
A aa = A(A()); //
aa.print_a();
return 0;
}
The output is 1 which is correct.
I am curious about the mechanism.
The difference between this question is that that question's constructor explicitly accepts an instance.

how to see which function is assigned to function pointer at run time..? [duplicate]

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);
}