我在处理函数内部结构成员的访问时遇到困难,而结构实例是双指针参数。我正在使用strcpy
和strcmp
,对我来说(我使用debuger)似乎做了坏事。
首先,我使用typedef
创建了结构,将其实例化了2次并为其中一个分配了内存。
其次,我创建了函数loadVariablesToMemory
,我将实例化的结构实例作为双指针传递给它(第二行 - 在//函数注释之后)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//FUNCTIONS
int loadVariablesToMemory(const char* fileName, const char* fileAccessMode, varData **dynamicData, varData **tmp);
//DECLARATION
typedef unsigned short int UINT16;
typedef struct data {
char varName[10];
UINT16 value;
} varData;
int main(){
//ALLOCATING MEMORY
varData *dynamicData;
dynamicData = (varData*)malloc(sizeof(varData));
varData *tmp;
tmp = NULL;
int numOfVars = loadVariablesToMemory("u02v1_input_03.txt", "r", &dynamicData, &tmp);
return 0;
}
问题:
在函数loadVariablesToMemory
内部,我试图访问已分配的&#34;变量&#34;上面实例化和声明的结构,使用:
.....
if(strcmp((*(dynamicalData+j-1))->varName, buffer) == 0){
.....etc.
strcpy((*(dynamicalData+numOfVars-1))->varName, buffer);
.....etc.
我运行调试器并且所有似乎都工作[也如果我将整个函数重写到Main之外它可以工作(现在单指针的双指针几乎没有变化)]但是我被卡在我使用{{1}的行中和strcpy();
函数,我不知道为什么,我有点绝望,因为这两个semms只有两件事阻止我创建一个工作函数,我正在变得绝望,因为我几乎处理这个问题10个小时没有任何实际进展。
完成功能strcmp()
声明:
loadVariablesToMemory
INSIDE INPUT FILE:u02v1_input_03.txt
int loadVariablesToMemory(const char* fileName, const char* fileAccessMode, varData **dynamicalData, varData **tmpal) {
FILE *fSource = fopen(fileName, fileAccessMode);
char oneChar = '\0';
char buffer[10];
memset(buffer,'\0',10); //inicialization of buffer chars to \0
int i = 0;
int varOrNum = 0;
int numOfVars = 0;
bool match = false;
while (!feof(fSource)) {
oneChar = fgetc(fSource); //Load letter in file
if((oneChar == ' ') | (oneChar == '\n')) {
i = 0;
if((strcmp(buffer,"NOT") == 0) || (strcmp(buffer,"AND") == 0) || (strcmp(buffer,"OR") == 0) || (strcmp(buffer,"LSHIFT") == 0) || (strcmp(buffer,"RSHIFT") == 0) || (strcmp(buffer,"->") == 0)) {
memset(buffer,'\0',10);
}
else{
varOrNum = atoi(buffer); //returns (0) if varOrNum is variable (0)
if((varOrNum == 0) & (buffer[0] != '0'))
{ //if varOrNum is a variable (0)
for(int j = 0; j<=numOfVars; j++) { //control in memory for match of variable name
//HERE SEEMS NOT TO WORK - this is just random try if strcpy will work
//strcpy((*(dynamicalData+j-1))->varName, buffer);
//HERE SEEMS NOT TO WORK
if(strcmp((*(dynamicalData+j-1))->varName, buffer) == 0){
memset(buffer,'\0',10);
match = true; //match found
break;
}
else
match = false; //no match found
} //endForCycle
if(match == false){ //no match found
numOfVars++; //number of variables for allocation
tmpal = (varData*)realloc((*dynamicalData), numOfVars * sizeof(varData));
if(!tmpal) { //or equal to NULL
//printf("Could not resize/reallocate memory %i times \n", numOfVars);
}
else{
dynamicalData = tmpal;
strcpy((*(dynamicalData+numOfVars-1))->varName, buffer);
printf("%s \n", (*(dynamicalData+numOfVars-1))->varName);
}
}
}
varOrNum = 0; //if varOrNum is a number (1)
memset(buffer,'\0',10);
}
}
else{
buffer[i] = oneChar;
i++;
}
} //endWhileCycle (!feof(fSource))
fclose (fSource);
return numOfVars;
}
任何人都可以这么好并帮助我吗?非常感谢你。
答案 0 :(得分:1)
尝试这样的事情:
if((varOrNum == 0) && (buffer[0] != '0')) {
int j;
for(j = 0; j<numOfVars; j++) {
if(strcmp(dynamicalData[j]->varName, buffer) == 0)
break;
}
if(j>=numOfVars) {
numOfVars++; //number of variables for allocation
tmpal = (varData*)realloc((*dynamicalData), numOfVars * sizeof(varData));
if(!tmpal) { //or equal to NULL
//printf("Could not resize/reallocate memory %i times \n", numOfVars);
}
else{
dynamicalData = tmpal;
strcpy(dynamicalData[numOfVars-1]->varName, buffer);
printf("%s \n", dynamicalData[numOfVars-1]->varName);
}
}
}
varOrNum = 0; //if varOrNum is a number (1)
memset(buffer,'\0',10);
在第1行你错过了&amp ;.
在第一个循环(j == 0)中,你执行strcmp dynamicalData+j-1
,这意味着dynamicalData[-1]