- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我是 C 编码新手,在使用以下代码创建链接列表时遇到问题。我一直遇到的问题是 fscanf 每次读入内容时都会更改变量。所以我的头指针值不断变化。有没有办法来解决这个问题?谢谢!
经过一番讨论,我意识到这是因为结构体中的助记符是一个指针,所以 head 的助记符会发生变化,因为它只是指向指针。有没有办法在不改变结构本身的情况下解决这个问题? (意思是将助记符留在struct Instr中作为指针)
void linkTypeInstr(struct InstrType *it, char *c, FILE *mfp){
int count = 0;
char type[2], mnemonic[11];
uint32_t uid = 0, pretty = 0;
struct Instr *head = NULL;
struct Instr *ptr = NULL;
/*read through the file in format*/
while(fscanf(mfp, "%s %u %s %u ", type, &uid, mnemonic, &pretty) != NULL){
if(head != NULL)
printf("head: %s\n", head -> mnemonic);
/*check if is the match type*/
if(*type == *c){
/*make a new struct instr for the item want to add*/
struct Instr *temp = malloc(sizeof(struct Instr));
memset(temp, 0, sizeof(struct Instr));
temp -> uid = uid;
temp -> pretty = pretty;
temp -> mnemonic = mnemonic;
temp -> next = NULL;
temp -> prev = NULL;
printf("temp: %s\n", temp -> mnemonic);
printf("count: %d\n", count);
/*check if list is empty, set ptr to head*/
if(count == 0){
head = temp;
count++;
}
/*if list is not empty, find the place to put the item in alphabetical order*/
else{
ptr = head;
printf("head: %s\n", head -> mnemonic);
while(ptr != NULL){
/*if temp should come after ptr in the order*/
if(strcmp(ptr -> mnemonic, temp -> mnemonic) < 0){
if(ptr -> next == NULL){
ptr -> next = temp;
temp -> prev = ptr;
count++;
ptr = NULL;
}
else if(strcmp(temp -> mnemonic, ptr -> next -> mnemonic) < 0){
temp -> next = ptr -> next;
ptr -> next -> prev = temp;
ptr -> next = temp;
temp -> prev = ptr;
count++;
ptr = NULL;
}
else
/*if should be after ptr -> next*/
ptr = ptr -> next;
}
/*if temp should come before ptr in the order*/
else if(strcmp(temp -> mnemonic, ptr -> mnemonic) < 0){
if(ptr -> prev == NULL){
ptr -> prev = temp;
temp -> next = ptr;
head = temp;
count++;
ptr = NULL;
}
else if(strcmp(ptr -> prev -> mnemonic, temp -> mnemonic) < 0){
ptr -> prev -> next = temp;
temp -> prev = ptr -> prev;
ptr -> prev = temp;
temp -> next = ptr;
count++;
ptr = NULL;
}
else
/*if should be before ptr -> prev*/
ptr = ptr -> prev;
}
}
}
}
}
it -> count = count;
it -> head = head;
}
struct Instr {
uint32_t uid; /* Unique identification bits */
uint32_t pretty; /* The type of pretty print format this Instr is */
char *mnemonic; /* The human readable mnemonic */
struct Instr *next; /* Pointer for doubly linked list */
struct Instr *prev; /* Pointer for doubly linked list */
};
struct InstrType {
char type; /* Should be R, I, or J */
size_t count; /* Length of Instr list */
struct InstrType *next; /* Points to the next InstrType node */
struct InstrType *prev; /* Points to the next InstrType node */
struct Instr *head; /* Points to the first Instr node of this type */
};
我试图读取的文件看起来像这样:
r 00000020 add 3
r 00000021 addu 3
r 00000022 sub 3
r 00000023 subu 3
r 00000018 mult 2
r 00000019 multu 2
r 0000001a div 1
r 0000001b divu 1
r 00000010 mfhi 0
r 00000011 mthi 0
r 00000012 mflo 0
r 00000013 mtlo 0
r 00000000 sll 8
r 00000002 srl 8
r 00000003 sra 8
r 00000004 sllv 3
r 00000006 srlv 3
r 00000007 srav 3
r 00000024 and 3
r 00000025 or 3
r 00000026 xor 3
r 00000027 nor 3
r 0000002a slt 3
r 0000002b sltu 3
r 00000008 jr 0
r 00000009 jalr 2
r 0000000c syscall 5
i 20000000 addi 2
i 24000000 addiu 2
i 3c000000 lui 2
i 28000000 slti 2
i 2c000000 sltiu 2
i 30000000 andi 2
i 34000000 ori 2
i 38000000 xori 2
i 80000000 lb 4
i 84000000 lh 4
i 88000000 lwl 4
i 8c000000 lw 4
i 90000000 lbu 4
i 98000000 lwr 4
i a0000000 sb 4
i a4000000 sh 4
i a8000000 swl 4
当我运行此命令时,结果总是将我的 head ptr 更改为创建的新临时值:
temp: add
count: 0
head: addu
temp: addu
count: 1
head: addu
...
调用该函数的代码如下所示:
int main(int argc, char *argv[]){
int c;
int iFlag = 0, oFlag = 0;
char mFileName[256] = "instruction_mapping.txt";
char oFileName[256];
char iFileName[256];
extern char *optarg;
FILE *mfp, *ifp, *ofp;
/*create three struct instrtype nodes*/
struct InstrType *itR = malloc(sizeof(struct InstrType));
struct InstrType *itI = malloc(sizeof(struct InstrType));
struct InstrType *itJ = malloc(sizeof(struct InstrType));
memset(itR, 0, sizeof(struct InstrType));
memset(itI, 0, sizeof(struct InstrType));
memset(itJ, 0, sizeof(struct InstrType));
/*link the struct instrtype together*/
if(itR != NULL && itI != NULL && itJ != NULL){
itR -> prev = NULL;
itR -> next = itI;
itI -> prev = itR;
itI -> next = itJ;
itJ -> prev = itI;
itJ -> next = NULL;
}
/*set up instrtypes*/
itR -> type = 'R';
itI -> type = 'I';
itJ -> type = 'J';
/* parse arguments */
while ((c = getopt(argc, argv, "m:i:o:h")) != -1){
switch(c){
case 'h':
USAGE(argv[0]);
return EXIT_SUCCESS;
case 'm':
strcpy(mFileName, optarg);
break;
case 'i':
iFlag = 1;
strcpy(iFileName, optarg);
break;
case 'o':
oFlag = 1;
strcpy(oFileName, optarg);
break;
default:
USAGE(argv[0]);
return EXIT_FAILURE;
}
}
/*check -i and -o must be in argv*/
if(iFlag == 1 && oFlag == 1){
mfp = fopen(mFileName, "r");
if(mfp == NULL){
printf("Cannot open instruction mapping file\n");
EXIT_FAILURE;
}
printf("mapping file: %s\n", mFileName);
ifp = fopen(iFileName, "r");
if(ifp == NULL){
printf("Cannot open input file\n");
EXIT_FAILURE;
}
printf("input file: %s\n", iFileName);
ofp = fopen(oFileName, "w");
if(ofp == NULL){
printf("Error creating or writing file\n");
EXIT_FAILURE;
}
printf("output file: %s\n", oFileName);
/* make linked lists of instr in R, I, J instrtype */
linkTypeInstr(itR, "r", mfp);
linkTypeInstr(itI, "i", mfp);
linkTypeInstr(itJ, "j", mfp);
if(mfp != NULL)
fclose(mfp);
if(ifp != NULL)
fclose(ifp);
if(ofp != NULL)
fclose(ofp);
}
else{
printf("Must have both input and output files\n");
return EXIT_FAILURE;
}
free(itR);
free(itI);
free(itJ);
return EXIT_SUCCESS;
}
最佳答案
你的问题是这一行:
temp -> 助记符 = 助记符;
事实上 temp->mnemonic 是一个指针,这意味着它将始终指向助记符数组中相同的 11 个字符。由于 head 是从 temp 分配的,因此 head->mnemonic 将指向相同的 11 个字符,这些字符将在下次调用 fscanf 时被覆盖。
解决方案是更改您的
结构指令{
uint32_t uid;/* 唯一标识位 */
uint32_t 漂亮;/* 该 Instr 的 pretty-print 格式类型 */
char *助记符;/* 人类可读的助记符 */
结构指令 *下一个;/* 双向链表指针 */
结构指令 *prev;/* 双向链表指针 */
};
到
结构指令{
uint32_t uid;/* 唯一标识位 */
uint32_t 漂亮;/* 该 Instr 的 pretty-print 格式类型 */
字符助记符[11];/* 人类可读的助记符 */
结构指令 *下一个;/* 双向链表指针 */
结构指令 *prev;/* 双向链表指针 */
};
并用 strncpy 对其进行赋值:
strncpy(temp -> 助记符, 助记符, 11);
关于c - 读入文件并在c中创建双链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070817/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!