文字列変数の値を構造体の別の文字列変数に代入しようとしています。しかしgdbは実行時エラーを出します。エラーは次のとおりです。 プログラム受信信号SIGSEGV、セグメンテーションフォルト。 std ::文字列::アサイン(STD ::文字列のconst &)() C++の構造体の文字列変数に文字列変数の値を代入する方法は?
私のC++プログラム/usr/lib/i386-linux-gnu/libstdc++.so.6からで 0xb7f7c8f8は次のとおりです。
#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;
}
xの値をt-> string_nodeに保存するのを手伝ってください。ありがとうございます。
試しになる前にcppのレッスンを読んだことがありますか?あなたは本当にすべきです。 http://www.learncpp.com/ – Stargateur
メモリを正しく割り当てるには 'new'を使います。 –