(C) 当我不使用';我不想这样
在我开始之前,这里是给我带来问题的代码块(C) 当我不使用';我不想这样,c,string,C,String,在我开始之前,这里是给我带来问题的代码块 typedef struct SymbPair{ char *symbol; int memoryAddress; }SymbPair; typedef struct SymbTable{ SymbPair *pairs; int numberOfItems; } SymbTable; SymbTable createTable(){ SymbTable table; table.pairs = (Symb
typedef struct SymbPair{
char *symbol;
int memoryAddress;
}SymbPair;
typedef struct SymbTable{
SymbPair *pairs;
int numberOfItems;
} SymbTable;
SymbTable createTable(){
SymbTable table;
table.pairs = (SymbPair*) calloc(1,sizeof(SymbPair));
table.numberOfItems = 0;
return table;
}
int checkForColon(char *word){
if (word[strlen(word) - 1] == ':'){
return 1;
} else {
return 0;
}
}
void removeLastChar(char *word, char *buffer){
int size = strlen(word);
char blank[size - 1];
for (int i = 0; i < size - 1; i++){
blank[i] = word[i];
}
blank[size - 1] = '\0';
strcpy(buffer,blank);
}
void symbPairSet(char *word, int address, SymbPair *pair){
pair -> memoryAddress = address;
pair -> symbol = word;
}
void addToSymbTable(SymbPair sp, SymbTable *array){
int currentTotal = array->numberOfItems;
array -> pairs = (SymbPair*) realloc(array->pairs,currentTotal+2);
array->pairs[currentTotal]=sp;
array -> numberOfItems = currentTotal + 1;
}
void firstPass(SymbTable *table, char **instructions){
int totalLabels = 0;
int i = 0;
while (instructions[i] != NULL){
if (checkForColon(instructions[i])){
char buffer[strlen(instructions[i]) - 1];
removeLastChar(instructions[i],buffer);
SymbPair new;
symbPairSet(buffer,(i*4) - totalLabels,&new);
addToSymbTable(new,table);
totalLabels++;
}
i++;
}
}
int main(){
SymbTable table = createTable();
firstPass(&table,instructionArray);
return EXIT_SUCCESS;
}
类型定义结构SymbPair{
字符*符号;
内部存储器地址;
}SymbPair;
类型定义结构符号表{
SymbPair*对;
国际项目数;
}符号表;
SymbTable createTable(){
符号表;
table.pairs=(SymbPair*)calloc(1,sizeof(SymbPair));
表1.numberOfItems=0;
返回表;
}
int checkForColon(字符*word){
如果(单词[strlen(word)-1]===':'){
返回1;
}否则{
返回0;
}
}
void removeLastChar(字符*字,字符*缓冲区){
int size=strlen(字);
字符空白[size-1];
对于(int i=0;i内存地址=地址;
配对->符号=单词;
}
void addToSymbTable(SymbPair sp,SymbTable*阵列){
int currentTotal=数组->numberOfItems;
数组->对=(SymbPair*)realloc(数组->对,currentTotal+2);
数组->对[currentTotal]=sp;
数组->numberOfItems=currentTotal+1;
}
无效首次通过(SymbTable*表格,字符**说明){
int-totalabels=0;
int i=0;
while(指令[i]!=NULL){
if(检查冒号(说明[i])){
字符缓冲区[strlen(指令[i])-1];
removeLastChar(指令[i],缓冲区);
SymbPair新;
SYMBPARSET(缓冲区(i*4)-总计标签和新标签);
addToSymbTable(新,表格);
Abels++;
}
i++;
}
}
int main(){
SymbTable table=createTable();
首次通过(表、指令阵列和);
返回退出成功;
}
第一步接收字符串数组并查找以冒号结尾的字符串,然后删除冒号并将编辑的字符串添加到符号对数组中,该数组保存在保存数组及其大小的结构调用symb表中。对于我当前的输入,数组中应该有一个项目具有符号循环和地址8。我的问题是,在firstPass函数中,字符串具有正确的值,但一旦它被传递到main中的值中,它就会完全改变。符号(或字符串)的值从firstPass函数内部更改为在main中赋值,但应相同。有什么帮助吗
如果您有:
char blank[size - 1];
那么这个
blank[size - 1] = '\0';
正在数组外部写入,因为它只有大小为-2的元素0
如果您在变量之外写入,您正在更改其他数据,这似乎与您的问题一致。如果您有以下情况:
char blank[size - 1];
那么这个
blank[size - 1] = '\0';
正在数组外部写入,因为它只有大小为-2的元素0
如果您在变量外部写入,您正在更改其他数据,这似乎与您的问题一致。
void removeLastChar
为什么要创建临时数组?首先将字符复制到临时数组,然后将临时数组复制到目标。。。只需将循环中的字符复制到目标。在下面的答案中添加:charbuffer[strlen(指令[i])-1]代码>也太小,将删除2个字符。而且缓冲区是一个可变长度的aray,它在中}
之后停止存在,而循环-all对->符号=word代码>无效,并指向相同的缓冲区
…是否需要在字符串中复制到SymbPair->word然后?void removeLastChar
为什么要创建临时数组?首先将字符复制到临时数组,然后将临时数组复制到目标。。。只需将循环中的字符复制到目标。在下面的答案中添加:charbuffer[strlen(指令[i])-1]代码>也太小,将删除2个字符。而且缓冲区是一个可变长度的aray,它在中}
之后停止存在,而循环-all对->符号=word代码>无效,并指向相同的缓冲区
…是否需要将字符串复制到SymbPair->word?谢谢您的建议。我试过了,但仍然存在同样的问题。另一个问题是,行array->pairs=(SymbPair*)realloc(array->pairs,currentTotal+2)
在大小计算中应该有一个*sizeof(SymbPair)
(大小以字节为单位)…谢谢您的建议。我试过了,但仍然存在同样的问题。另一个问题是,行array->pairs=(SymbPair*)realloc(array->pairs,currentTotal+2)
在大小计算中应该有一个*sizeof(SymbPair)
(大小以字节为单位)。。。