他の二重ポインタを指す一連の二重ポインタを使用しています。 現在、私はダブルポインターを持つ他の構造体を指す構造体を持っています。二重ポインタは正しくポインタ値を格納せず、右に解放しません。
私は値を保存するために "sprintf"コマンドを使用していました。これはsegフォルトが発生するまでうまくいっていました。
sprintf(a->cs[a->structCount].fs[sFunCount].parameterType[insideParam],
"%s", a->array);
私はsprintfを取り除いて、それをa-> arrayと同じにしていました。しかし、それは正しく格納されません。
a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = a->type;
これは、他のものではなく "int"を印刷しています。私がするとき
a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = "double";
正しく保存します。
私のすべての配列を正しくmallocすることができますか、正しくそれらを解放することもできますか?相続人
すべての私のコード..
#include "header.h"
int main(int argc, char * argv[]){
all * a;
if(argc != 2){
printf("Error: Send input file as command line argument.\n");
return 1;
}
display(argv[1]);
a = openFile(argv[1]);
printProgram(a);
freeMe(a);
return 0;
}
all * openFile(char * fileName){
FILE *fp;
int token = 0, z = 0, x = 0, j = 0, i = 0, dotSwitch = 0,
hashSwitch = 0, leftPacSwitch = 0, varNCount = 0, funNCount = 0,
parameter = 0, parameterName = 0, rightBrace = 0, leftBrace = 0,
structNameSwitch = 0, insideStruct = 0, globalCount = 0,
funRightBrace = 0, funLeftBrace = 0, sFunCount = 0, insideParam = 0;
all * a = malloc(sizeof(all));
switches * s = malloc(sizeof(switches));
//a = m(a);
s->structSwitch = 0;
s->functionSwitch = 0;
s->variableSwitch = 0;
s->commentSwitch = 0;
s->quoteSwitch = 0;
s->slashSwitch = 0;
s->nameSwitch = 0;
s->bracketSwitch = 0;
a->hashCount = 0;
a->cs = malloc(sizeof(cStruct *) * 100);
a->fs = malloc(sizeof(fStruct *) * 100);
a->globalVariableType = malloc(sizeof(char *) * 100);
a->globalVariableName = malloc(sizeof(char *) * 100);
a->includes = malloc(sizeof(char *) * 100);
a->array = malloc(sizeof(char));
a->type = malloc(sizeof(char));
a->vName = malloc(sizeof(char));
a->preDot = malloc(sizeof(char));
a->prePac = malloc(sizeof(char));
a->structName = malloc(sizeof(char));
a->structCount = 0;
a->cs[a->structCount].variableCount = 0;
for(i = 0; i < 100; i++){
a->cs[i].name = malloc(sizeof(char)*50);
a->cs[i].variableType = malloc(sizeof(char)*50);
a->cs[i].variableName = malloc(sizeof(char)*50);
a->cs[i].fs = malloc(sizeof(fStruct)*50);
a->fs[i].functionName = malloc(sizeof(char)*50);
a->fs[i].functionType = malloc(sizeof(char)*50);
a->fs[i].parameterName = malloc(sizeof(char)*50);
a->fs[i].parameterType = malloc(sizeof(char)*50);
a->globalVariableName[i] = malloc(sizeof(char)*50);
a->globalVariableType[i] = malloc(sizeof(char)*50);
a->includes[i] = malloc(sizeof(char)*50);
for(x = 0; x < 100; x++){
a->cs[i].variableType[x] = malloc(sizeof(char)*50);
a->cs[i].variableName[x] = malloc(sizeof(char)*50);
a->cs[i].fs[x].functionName = malloc(sizeof(char)*50);
a->cs[i].fs[x].functionType = malloc(sizeof(char)*50);
a->cs[i].fs[x].parameterType = malloc(sizeof(char)*50);
a->cs[i].fs[x].parameterName = malloc(sizeof(char)*50);
a->fs[i].parameterName[x] = malloc(sizeof(char)*50);
a->fs[i].parameterType[x] = malloc(sizeof(char)*50);
for(z = 0; z < 100; z++){
a->cs[i].fs[x].parameterType[z] = malloc(sizeof(char)*50);
a->cs[i].fs[x].parameterName[z] = malloc(sizeof(char)*50);
}
}
}
//Opening the file
fp = fopen(fileName,"r");
//If file doesnt exist then display output
if(fp == NULL){
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
else{
printf("\nUpdated File(C):\n");
}
while((token = fgetc(fp)) != EOF){
if(token != ' ' && token != '{' && token != '}' && token != ';' && token != '\n' && token != '(' && token != ')' && token != '/' && token != '"' && token != '*' && token != '\\' && token != ',' && token != '.' && token != '>' && token != '#'){
a->array[j] = token;
j++;
}
else{
if(token == '/'){
s->commentSwitch++;
printf("/");
}
else if(token == '>'){
sprintf(a->prePac,"%s%c", a->array, token);
leftPacSwitch = 1;
}
else if(token == '#'){
hashSwitch = 1;
}
else if(token == '.'){
sprintf(a->preDot,"%s%c", a->array, token);
dotSwitch = 1;
}
else if(token == '{' && s->structSwitch == 1 && structNameSwitch == 0){
sprintf(a->cs[a->structCount].name,"%s", a->structName);
//structCount++;
rightBrace++;
printf("%c", token);
structNameSwitch = 1;
insideStruct = 1;
}
else if (token == '{' && s->structSwitch == 1){
rightBrace++;
a->cs[a->structCount].rightBracket = rightBrace;
if (s->structFunctionSwitch == 1){
funRightBrace++;
a->cs[a->structCount].fs[funRightBrace].rightBracket = funRightBrace;
}
printf("%c", token);
}
else if(token == '}' && s->structSwitch == 1){
leftBrace++;
if(rightBrace == leftBrace){
s->structSwitch = 0;
rightBrace = 0;
leftBrace = 0;
insideStruct = 0;
sFunCount = 0;
a->structCount++;
}
else if (s->structFunctionSwitch == 1){
funLeftBrace++;
a->cs[a->structCount].fs[funLeftBrace].leftBracket = funLeftBrace;
if(funRightBrace == funLeftBrace){
s->structFunctionSwitch = 0;
funRightBrace = 0;
funLeftBrace = 0;
}
}
printf("%c", token);
}
else if(token == '\\'){
s->slashSwitch = 1;
s->quoteSwitch = 0;
printf("%s", a->array);
printf("%c", token);
}
else if(token == '(' && s->variableSwitch == 1 && s->structSwitch == 0){
s->bracketSwitch = 1;
sprintf(a->fs[funNCount].functionType, "%s", a->type);
sprintf(a->fs[funNCount].functionName, "%s", a->array);
funNCount++;
printf("%s", a->array);
printf("%c", token);
}
else if(token == '(' && insideStruct == 1 && s->variableSwitch == 1){
s->bracketSwitch = 1;
insideParam = 0;
printf("%s",a->array);
printf("%c", token);
sprintf(a->cs[a->structCount].fs[sFunCount].functionType, "%s", a->type);
sprintf(a->cs[a->structCount].fs[sFunCount].functionName, "%s", a->array);
s->structFunctionSwitch = 1;
s->variableSwitch = 0;
s->nameSwitch = 0;
a->cs[a->structCount].functionCount = sFunCount;
}
else if(token == ')' && insideStruct == 1 && s->bracketSwitch == 1){
s->bracketSwitch = 0;
s->structFunctionSwitch = 0;
a->cs[a->structCount].fs[sFunCount].parameterName[insideParam] = a->array;
a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = a->type;
printf("%d",insideParam);
a->cs[a->structCount].fs[sFunCount].paraCount = insideParam;
// printf("s: %d , f: %d, i: %d\n", a->structCount,sFunCount,insideParam);
// printf("%s\n", a->cs[a->structCount].fs[sFunCount].parameterName[insideParam]);
printf("%s", a->array);
printf("%c", token);
sFunCount++;
}
else if(token == ')' && s->variableSwitch == 1){
sprintf(a->fs[funNCount].parameterName[a->fs[funNCount].paraCount], "%s", a->array);
parameterName++;
printf("%s",a->array);
printf("%c", token);
s->bracketSwitch = 0;
s->variableSwitch = 0;
parameter = 0;
}
else if(token == '*'){
printf("%s", a->array);
printf("%c", token);
}
else if(s->commentSwitch == 2){
printf("%s",a->array);
printf("%c", token);
if(token == '\n'){
s->commentSwitch = 0;
}
}
else if(s->slashSwitch == 1){
printf("%s",a->array);
printf("%c", token);
s->slashSwitch = 0;
}
else if(token == '"' && s->slashSwitch == 0 && s->commentSwitch == 0){
s->quoteSwitch++;
printf("%s",a->array);
printf("%c", token);
if(s->quoteSwitch == 2){
s->quoteSwitch = 0;
}
}
else if(s->quoteSwitch == 1 && token != '"'){
printf("%s",a->array);
printf("%c", token);
}
else{
if(hashSwitch == 1 && dotSwitch == 1 && leftPacSwitch == 1){
printf("#%s%s", a->preDot,a->prePac);
sprintf(a->includes[a->hashCount], "%s%s", a->preDot,a->prePac);
a->hashCount++;
hashSwitch = 0;
dotSwitch = 0;
leftPacSwitch = 0;
}
else if(strcmp(a->array,"class") == 0 && s->commentSwitch != 2 && s->quoteSwitch != 1){
strcpy(a->array,"struct");
s->structSwitch = 1;
structNameSwitch = 0;
varNCount = 0;
}
else if(s->structSwitch == 1 && insideStruct == 1){
if(strcmp(a->array,"int") == 0 || strcmp(a->array,"double") == 0 || strcmp(a->array,"float") == 0 || strcmp(a->array,"char") == 0 || strcmp(a->array,"void") == 0){
if(s->bracketSwitch == 0){
strcpy(a->type,a->array);
s->variableSwitch = 1;
s->nameSwitch = 0;
}
else if(s->bracketSwitch == 1){
// printf("vjjkjj");
a->cs[0].fs[0].parameterType[4] = a->type;
strcpy(a->type,a->array);
s->variableSwitch = 0;
s->nameSwitch = 0;
s->structFunctionSwitch = 1;
insideParam++;
a->cs[a->structCount].fs[sFunCount].paraCount++;
}
// printf("var1: %d nam: %d Bra: %d fun: %d inside: %d st: %d\n", s->variableSwitch, s->nameSwitch,s->bracketSwitch, s->structFunctionSwitch, insideStruct, s->structSwitch);
/*else{
s->variableSwitch = 0;
}*/
}
//Gets all the variables in the struct
else if(s->nameSwitch == 0 && s->variableSwitch == 1 && s->bracketSwitch == 0){
varNCount++;
strcpy(a->vName,a->array);
a->cs[a->structCount].variableCount = varNCount;
sprintf(a->cs[a->structCount].variableType[varNCount], "%s", a->type);
sprintf(a->cs[a->structCount].variableName[varNCount], "%s", a->vName);
s->variableSwitch = 0;
}
else if(s->nameSwitch == 0 && s->variableSwitch == 0 && s->bracketSwitch == 1 && s->structFunctionSwitch == 1){
a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = a->type;
a->cs[a->structCount].fs[sFunCount].parameterName[insideParam] = a->array;
if(strcmp(a->type, "double") == 0){
a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = "double";;
}
a->cs[1].fs[0].parameterType[3] = "double";
a->cs[1].fs[0].parameterName[3] = "gg";
printf("struct: %s\n", a->type);
//printf("structh: %d, Function: %d, Param: %d", a->structCount, sFunCount, insideParam);
// a->cs[a->structCount].fs[sFunCount].paraCount = insideParam;
// printf("HAHA%d", a->cs[a->structCount].fs[sFunCount].paraCount);
s->nameSwitch = 1;
}
}
else if(s->structSwitch == 1 && structNameSwitch == 0){
strcpy(a->structName,a->array);
}
else if(strcmp(a->array,"int") == 0 || strcmp(a->array,"double") == 0 || strcmp(a->array,"float") == 0 || strcmp(a->array,"char") == 0 || strcmp(a->array,"void") == 0){
if(s->bracketSwitch == 1){
sprintf(a->fs[funNCount].parameterType[parameter], "%s", a->array);
a->fs[funNCount].paraCount = parameter;
parameter++;
}
else{
strcpy(a->type,a->array);
}
s->variableSwitch = 1;
s->nameSwitch = 0;
}
else if(s->variableSwitch == 1 && s->nameSwitch == 0){
if(s->bracketSwitch == 1){
sprintf(a->fs[funNCount].parameterName[a->fs[funNCount].paraCount], "%s", a->array);
parameterName++;
}
else{
strcpy(a->vName,a->array);
}
s->nameSwitch = 1;
}
else if(s->variableSwitch == 1 && s->nameSwitch == 1 && s->bracketSwitch == 0){
sprintf(a->globalVariableType[globalCount], "%s", a->type);
sprintf(a->globalVariableName[globalCount], "%s", a->vName);
a->globalCount = globalCount;
globalCount++;
s->variableSwitch = 0;
s->nameSwitch = 0;
}
printf("%s",a->array);
printf("%c", token);
}
memset(a->array,0,strlen(a->array));
j = 0;
}
}
a->funCount = funNCount;
printf("%d\n\n",a->fs[2].paraCount);
printf("{ : %d\n", rightBrace);
printf("} : %d\n", leftBrace);
fclose(fp);
return a;
}
int fOrV(char * c){
int len = strlen(c), i;
//char * array = malloc(sizeof(char*));
for(i = 0;i < len; i++){
if(c[i] == '('){
return 1;
}
else if(c[i] == ')' || c[i] == ','){
//printf("LOOL %s\n",c);
return 2;
}
}
return 0;
}
int display(char * fileName){
FILE *fp;
char * array;
int token = 0, totalStruct = 0, totalVariable = 0, j = 0;
array = malloc(sizeof(char));
//Opening the file
fp = fopen(fileName,"r");
//If file doesnt exist then display output
if(fp == NULL){
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
else{
printf("File %s opened successfully\nOriginal Code(C++):\n", fileName);
}
while((token = fgetc(fp)) != EOF){
if(token != ' ' && token != '{' && token != '}' && token != ';' && token != '\n' && token != '(' && token != ')' && token != '/' && token != '"' && token != '*' && token != '\\' && token != ',' && token != '.' && token != '[' && token != ']'){
array[j] = token;
j++;
}
else{
if(strcmp(array,"class") == 0){
totalStruct++;
}
else if(strcmp(array,"int") == 0 || strcmp(array,"double") == 0 || strcmp(array,"float") == 0 || strcmp(array,"char") == 0 || strcmp(array,"void") == 0){
totalVariable++;
}
memset(array,0,strlen(array));
j = 0;
printf("%s",array);
}
printf("%c", token);
}
fclose(fp);
return 0;
}
void printProgram(all * a){
int b, i;
for(b = 0; b < a->structCount; b++){
printf("Struct Name: %s\n", a->cs[b].name);
for(i = 1; i <= a->cs[b].variableCount; i++){
printf("\tVariabe Type: %s %s\n", a->cs[b].variableType[i],a->cs[b].variableName[i]);
}
for(i = 0; i <= a->cs[b].functionCount; i++){
printf("\tFunction Type: %s %s\n", a->cs[b].fs[i].functionType,a->cs[b].fs[i].functionName);
}
}
for(i = 0; i <= a->globalCount; i++){
printf("Global Variable: %s %s\n", a->globalVariableType[i], a->globalVariableName[i]);
}
printf("----------------------------------------------\n");
for(i = 0; i < a->hashCount; i++){
printf("%s\n", a->includes[i]);
}
printf("\n");
for(i = 0; i <= a->globalCount; i++){
printf("%s %s;\n", a->globalVariableType[i], a->globalVariableName[i]);
}
printf("\n");
for(b = 0; b < a->structCount; b++){
printf("struct %s {\n", a->cs[b].name);
for(i = 1; i <= a->cs[b].variableCount; i++){
printf("\t%s %s;\n", a->cs[b].variableType[i],a->cs[b].variableName[i]);
}
for(i = 0; i <= a->cs[b].functionCount; i++){
printf("\t%s (*%s%s)();\n", a->cs[b].fs[i].functionType,a->cs[b].name,a->cs[b].fs[i].functionName);
}
printf("}\n");
}
printf("WHAT: %s\n", a->cs[0].fs[0].parameterType[3]);
int x;
for(b = 0; b < a->structCount; b++){
// printf("S: %d", b);
for(i = 0; i <= a->cs[b].functionCount; i++){
//printf("F: %d", i);
printf("%s %s%s(", a->cs[b].fs[i].functionType,a->cs[b].name,a->cs[b].fs[i].functionName);
for(x = 1; x <= a->cs[b].fs[i].paraCount; x++){
//printf("F: %d\n", x);
if(x == (a->cs[b].fs[i].paraCount)){
printf("%s %s", a->cs[b].fs[i].parameterType[x],a->cs[b].fs[i].parameterName[x]);
}
else if(x == 1){
printf("%s %s,", a->cs[b].fs[i].parameterType[x],a->cs[b].fs[i].parameterName[x]);
}
else{
printf(" %s %s,", a->cs[b].fs[i].parameterType[x],a->cs[b].fs[i].parameterName[x]);
}
}
printf("){\n");
}
printf("\n");
}
for(i = 0; i < a->funCount; i++){
printf("%s %s(", a->fs[i].functionType, a->fs[i].functionName);
for(b = 0; b <= a->fs[i+1].paraCount; b++){
printf("%s %s ", a->fs[i+1].parameterType[b], a->fs[i+1].parameterName[b]);
}
printf("){\n");
}
}
void freeMe(all * a){
int i, x;
/*for(i = 0; i < 100; i++){
free(a->cs[i].name);
free(a->cs[i].variableType);
free(a->cs[i].variableName);
free(a->cs[i].fs);
free(a->fs[i].functionName);
free(a->fs[i].functionType);
free(a->fs[i].parameterName);
free(a->fs[i].parameterType);
free(a->globalVariableName[i]);
free(a->globalVariableType[i]);
free(a->includes[i]);
}
free(a->cs);
free(a->fs);
free(a->globalVariableType);
free(a->globalVariableName);
free(a->includes);
free(a->array);
free(a->type);
free(a->vName);
free(a->preDot);
free(a->prePac);
free(a->structName);*/
free(a);
//free(s);
}
私たちにとってはすばらしいチャレンジです。複数のネストされた構造体と配列、深くネストされた 'if ... else'節を持つコードの壁です。たぶん鋭い鷹が問題を見つけられるかもしれませんが、あなたは自分でデバッガを解くのに熱心かもしれません。 –
@WeatherVaneあなたが助けることができると思いますか? – MunajSyed
おそらく、このリンク[どのように私は良い質問をしますか?](0120)を参照してください。 –