なぜこのエラーが発生するのか完全にはわからないため、単一の製品または製品の配列を使用しても同じ結果が得られます。構造体プロパティを参照する際のセグメンテーションフォールト(コアダンプ)
test.cの
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "salesbase.h"
//Function Declarations//
struct Product new_product(int id, char name[120], float cost);
int main(int argc, char *argv[]){
struct Product testitem = new_product(0, "yes", 2.0);
printf("%f\n", testitem.product_cost);
getchar();
return 0;
}
struct Product new_product(int id, char name[120], float cost){
struct Product temp;
temp.product_id = id;
strcpy(name,temp.product_name);
temp.product_cost = cost;
temp.product_discount = 0.00;
return temp;
}
salesbase.h
#ifndef SALESBASE_H_
#define SALESBASE_H_
struct Product {
int product_id;
char product_name[120];
float product_cost;
float product_discount;
};
struct Sale {
int sale_id;
struct Product sale_items[100];
};
#endif
'strcpyの(名前、temp.product_name)を変更;' 'strcpyの(temp.product_name、名)である必要があります。' – kaylum
それは何かの値によって構造体を返すためにも少し珍しいですこのような。初期化される構造体へのポインタを渡す方がはるかに意味があります。そうすれば、不要な構造コピーを避けることができます。これはバグではなく、悪いデザインの選択肢です。 –