前提・実現したいこと
c言語でリストを利用して、入力された文字列の ポインタをセルに格納するプログラムを考えています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
strcpy_s(g->s, sizeof(g->s), buf); 0xC0000005: 場所 0xCDCDCDCD への書き込み中にアクセス違反が発生しました
該当のソースコード
include<stdio.h>
include<stdlib.h>
include<string.h>
struct LIST {
struct LIST * next;
char *s;
};
struct LIST * addList(struct LIST ** pstart) {
struct LIST *p, *ne;
p = *pstart;
if ((ne = (struct LIST *) malloc(sizeof(struct LIST))) == NULL) {
return NULL;
}
ne->next = NULL;
if (p != NULL) {
while (p->next != NULL) {
p = p->next;
}
p->next = ne;
}
else {
*pstart = ne;
}
return ne;
}
int main() {
char buf[512];
struct LIST *start = NULL;
struct LIST *g;
while(1) {
gets_s(buf, sizeof(buf));
g = addList(&start);
strcpy_s(g->s, sizeof(g->s), buf);
}
return 0;
}
試したこと
補足情報(FW/ツールのバージョンなど)
VS2017を使用
includeは文字が大きくなるため#を省いています。
回答2件
あなたの回答
tips
プレビュー