我正在尝试在C代码中使用动态内存。我正在使用struct而我做错了什么,但我无法弄清楚是什么。第一部分是我的标题(.h)文件的一部分,我在其中定义了两个结构。
第二部分是我的C(.c)文件的一部分,我初始化结构并使用它。我得到一个seg错误,ddd告诉我那个seg。注释后出现故障:这是seg的所在。发生故障。 任何帮助都将非常感激
=======================================
第一部分
=======================================
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
void parseFile(FILE * fp, FILE * sketcher);
void processArgument(char argument[]);
void printOutput();
#define MAX_WORD 256
#define initial_size 17
extern const char argument[];
FILE* popen(const char*, const char*);
int pclose(FILE*);
struct pointxy {
double x;
double y;
};
struct figure{
char figureName[MAX_WORD];
struct pointxy vertices[17];
int countPoints;
};
struct figure figurehere[17];
struct figure newFigure[17];
=======================================
第二部分
=======================================
#include "draw2.h"
#include "draw2a.h"
#include "memwatch.h"
struct figure *figureHere;
void printOutput(){
printf("./draw2 started on:");
fflush(stdout);
system("date\n");
}
/*send what ever there is after the child to sketchpad(in that specific line)*/
void child (char line[], char word[], char nameFigure[], FILE * sketcher){
sscanf(line, "%s%s", word, nameFigure);
fprintf (sketcher, "%s\n", &line[6]);
}
/*I construct the struct by reading from the Figure line to the end figure line.*/
struct figure* figureFunction (FILE * fp, char line[], char word[], char figureName[], int countNumberoffigures){
double startx, starty;
int temp = 1;
sscanf(line, "%s%s%lf%lf%s", word, figureName, &startx, &starty, word);
figureHere->vertices[0].x = startx;
figureHere->vertices[0].y = starty;
strcpy(figureHere->figureName, figureName);
fgets(line, MAX_WORD - 1, fp);
int nuRead = sscanf(line, "%s", word);
int i = 1;
while (strncmp(word, "End", MAX_WORD)!=0){
if (strncmp(word, "#", MAX_WORD) == 0){
printf("%s",line);
}
if (strncmp(word, "draw", MAX_WORD) == 0){
sscanf (line, "%s%lf%lf", word, &startx, &starty);
figureHere->vertices[i].x = figureHere->vertices[i-1].x + startx;
figureHere->vertices[i].y = figureHere->vertices[i-1].y + starty;
i += 1;
}
fgets(line, MAX_WORD - 1, fp);
nuRead = sscanf(line, "%s", word);
}
figureHere->countPoints = i;
return figureHere;
}
=======================================
第三部分
=======================================
#include "draw2.h"
#include "draw2a.h"
#include "draw2b.h"
#include "memwatch.h"
struct figure myFigures[17];
struct figure **pointsAndname;
const char Exec_c[] = "java -jar Sketchpad.jar";
void parseFile(FILE * fp, FILE *sketcher){
char line [MAX_WORD], word [MAX_WORD], figureName [MAX_WORD];
int countNumberoffigures; //accounts to which figure in the array we are on
printOutput();
int temp = 1;
countNumberoffigures = 0;
while ( fgets(line, MAX_WORD - 1, fp) != NULL ){
int nuRead = sscanf(line, "%s", word);
if ( nuRead > 0 ){
if(strncmp(word, "Figure", MAX_WORD)==0){ //1)reads the figure, name and the two starting points
countNumberoffigures += 1; //accounts to which figure in the array we are on
pointsAndname = malloc(sizeof(struct figure*)*temp);
pointsAndname[countNumberoffigures -1] = figureFunction(fp,line, word, figureName, countNumberoffigures);
}
if(strncmp(word, "printFigure", MAX_WORD)==0){ //4)read the command printFigure, name of the figure
printFigure(fp, line, countNumberoffigures);
}
if(strncmp(word, "drawFigure", MAX_WORD)==0){ //5)read the command drawFigure and the name of the figure
drawFigure(sketcher, line, countNumberoffigures);
}
if(strncmp(word, "translate", MAX_WORD)==0){ //6)read the command translate
translate(line, sketcher, countNumberoffigures);
}
if(strncmp(word, "child", MAX_WORD)==0){ //7)reads command child and the name of the figure
child(line, word, figureName, sketcher);
}
if(strncmp(word, "#", MAX_WORD)==0){ //8)reads the whole line until the \n
printf(line);
//printf("ani po\n");
}
if(strncmp(word, "end", MAX_WORD)==0){
fprintf (sketcher, "end\n");
//printf("ani po\n");
}
if(strncmp(word, "rotate", MAX_WORD)==0){
rotate(line, sketcher, countNumberoffigures);
}
}
}
}
void processArgument(char argument[]){
FILE *sketcher;
FILE *fp;
fp = fopen (argument, "r");
sketcher = popen(Exec_c, "w");
if (sketcher == NULL){
printf ("Could not open pipe to %s\n", argument);
}else{
parseFile(fp, sketcher);
fclose(fp);
if (pclose(sketcher) == -1){
fprintf(stderr, "draw_line error: couldn't close pipe to %s.\n", Exec_c);
exit(EXIT_FAILURE);
}
}
}
int main (int argc, char *argv[]){
int i;
if ( argc < 2 ){
printf ("%s\n", "0 comment(s)");
}else{
for (i = 1; i < argc; i++){
processArgument(argv[i]);
}
}
//int *a = malloc(sizeof(int));
return 0;
}
答案 0 :(得分:3)
在你的头文件中,你要声明一个静态的数字数组:
struct figure figurehere[17];
但是在你的实现文件(* .c)中,你正在用
创建一个指向'figure'的单独指针struct figure *figureHere;
现在,从技术上讲,这些是完全独立的,figurehere
和figureHere
都可以在您的实现文件中访问,因为C区分大小写...但是,我想这可能不是故意的你的部分,最后可能被认为是不好的做法,因为它很可能在快速阅读代码时容易混淆或误导。我建议,如果你打算将它们作为完全独立的变量,那么你将它们命名为更明显不同。
然而,前面的评论中所说的是正确答案:您正在声明一个指针figureHere
,并且在没有初始化它的情况下使用它。
....我假设您打算figureHere
(大写字母H)指向figurehere
(较低的H),我已经写了一个解释,但后来我意识到这个问题具体提及malloc
。遗憾的是,malloc
甚至没有出现在您的代码中,所以我不确定该用法是如何“错误”的,除非没有用。代码是否丢失,或者您是否无法正确使用malloc
创建figure
figureHere
来指向?
编辑/附加:
重新阅读我注意到您具体说的问题
第一部分是我的一部分 标题(.h)文件,我在其中定义了两者 我的结构。
这个真的让我相信你打算让figurehere
和figureHere
成为同一个数组。要实现这一目标,您有以下几种选择:
1)删除figureHere
,然后直接使用figurehere
。因为你已经将figurehere
声明为全局(许多人会说你很糟糕,但这是一个完全不同的主题),你可以直接访问它而不需要任何指针。
2)初始化figureHere
以指向figurehere
。您只需将声明更改为
struct figure *figureHere = figurehere;