I am trying to assign the value of a string variable to another string variable of a structure. But gdb gives an runtime error. The error is as follows: Program received signal SIGSEGV, Segmentation fault. 0xb7f7c8f8 in std::string::assign(std::string const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
My C++ program is:
#include<iostream>
#include<stdlib.h>
#include<string>
typedef long unsigned int LUI;
using namespace std;
struct graph {
string string_node;
LUI node;
struct graph *link;
};
struct graph *abc[30];
struct graph *t;
string x;
int main() {
t = (struct graph *) malloc(sizeof(struct graph *));
x = "abc";
t->string_node = x;
t->link = NULL;
abc[0] = t;
cout << "Value is " << abc[0]->string_node << endl;
cout << "end";
return 0;
}
Please help me to store the value of x into t->string_node. Thanks in advance..
newto allocate memory properly.