- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在查看一些旧代码以编写一个程序(在 C 中),该程序创建类似于单链表堆栈的推送和弹出方法。我目前遇到段错误,无法找出问题所在。
任何推送的输入都是单个字符,这是一个输入示例:
推;
推 g
推.
流行
推 -
代码(注释掉了一些导致错误的东西):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char data;
struct node* next;
}*top = NULL;
void push(char c);
char pop();
int main(int argc, char *argv[])
{
char* p1;
char p2;
FILE *fp = NULL;
fp = fopen(argv[2], "r");
loop:
while (!feof(fp))
{
fscanf(fp,"%s", &p1);
while (strcmp(&p1,"push") == 0)
{
fscanf(fp,"%s", &p2);
printf("%s\n", &p2);
// push(&p2);
fscanf(fp,"%s", &p1);
if (strcmp(&p1,"pop") == 0)
{
//pop();
fscanf(fp,"%s", &p1);
}
}
while (strcmp(&p1,"pop") == 0)
{
//pop();??
fscanf(fp,"%s",&p1);
if (strcmp(&p1,"push") == 0)
{
fscanf(fp,"%s",&p2);
printf("%s\n",&p2);
// push(&p2);
}
goto loop;
}
}
fclose(fp);
return 0;
}
void push(char c)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = c;
temp->next = top;
top = temp;
}
char pop()
{
struct node *temp = top;
char data = temp->data;
top = top->next;
free(temp);
return data;
}
当前警告:
stack.c: In function âmainâ:
stack.c:24:3: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]
stack.c:26:3: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]
/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â
stack.c:31:4: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]
stack.c:32:4: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]
/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â
stack.c:35:5: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]
stack.c:39:3: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]
/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â
stack.c:42:4: warning: format â%sâ expects argument of type âchar *â, but argument 3 has type âchar **â [-Wformat]
stack.c:43:4: warning: passing argument 1 of âstrcmpâ from incompatible pointer type [enabled by default]
/usr/include/string.h:143:12: note: expected âconst char *â but argument is of type âchar **â
最佳答案
警告已经告诉您该怎么做。函数期望的指针 char*
和您传递的指针 &p1
aka char**
是不同类型的。要解决此问题,您必须使用 p1
而不是 &p1
。
但是你还有另一个问题,你在初始化之前使用了p1
。这也会导致段错误,因为指针指向任何地方,函数 printf
或 strcmp
试图访问那里的内容。
这意味着在 fscanf
的情况下,您必须分配可以存储内容的内存,参见 malloc
.当然,p2
也是如此。
关于c - 实现push和pop的单链表-seg fault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551276/
所以下面的内容让我很困惑。 #!/usr/bin/python test = [0, 0, 0, 1, 2, 3, 4, 5, 6] test1 = [0, 0, 0, 1, 2, 3, 4, 5,
这个问题是这个问题的后续问题: deque.popleft() and list.pop(0). Is there performance difference? 在 Python 中,我可以使用 .
我正在使用 bootstrap v2.2.2。我尝试了其他一些方法(即: close popover outside popover but inside stay open 和 How to dis
我正在用 Python 创建提交后脚本并使用子进程调用 git 命令。 在我的脚本中,我想在运行某些命令之前存储所有更改,然后将它们 pop 。问题是,如果没有任何东西可以存储,stash pop 会
我有一个嵌入在 UINavigationController 中的 UITableViewController,我正在尝试将 Peek & Pop 实现到 TableView 中。我的“窥视”部分工作
我的 Windows 机器上安装了 Cygwin、msysgit 和 TortoiseGit。我正在为 Cygwin 编写一个脚本,该脚本通过 ssh 将 git 推送到远程机器: git push
我在 Jenkins 中使用groovy,并且我需要这个字符串来获取其中的最后一个单词。假设字符串是 STATUS = "EXECUTE SIT" 。所以我所做的就是分割字符串,这样我就会得到一个数组
本文是不太具体的问题的后续/重新表述 Is it possible to have a hyperlink inside {content:"..."}? . 用户 Naeem Shaikh ,非常感
Navigator.of(context).pop 和 Navigator.pop(context) 有什么区别? 对我来说两者似乎都在做同样的工作,实际的区别是什么。一个被弃用了吗? 最佳答案 Na
这可能吗?我想要一个更简单的命令来 git stash pop stash@{13} 其中 stash@{13} 只是 last 意思是“最后的存储在列表上”或“最古老的藏品”。 我知道我可以为 gi
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
Visual Studio 2019 中用于 GIT 存储的以下命令有什么区别? 分阶段 pop 和恢复 (--index) 全部 pop 为未暂存状态 使用https://visualstudio.
我想弹出模型的最后一层。所以我使用了 tf.keras.layers.pop(),但它不起作用。 base_model.summary() base_model.layers.pop() base_m
我想使用 navigator.pop 将值从第 2 页传递到第 1 页,并使用 initstate 中的新值刷新或重新加载我的第 1 页或任何其他解决方法? 我能够在第一页中获取这些值,但无法使用 i
pop 函数的文档说: user> (doc pop) ------------------------- clojure.core/pop ([coll]) For a list or queu
我有以下点击处理程序,当点击它时,我从 handsontable 中提取一个数组然后从数组中删除最后一个元素,并将新数组传递给 ajax post。问题是,如果我再次单击该按钮,它将从数组中删除另一个
我在mailmuch中制作了表单并从中获取了代码,我添加到网页并使用href,当用户单击显示弹出窗口时显示表单。没关系 show popup 但是现在我有ajax请求,我希望在ajax返回成功时显示此
我目前正在学习 Python 中的 pop() 函数并有一个问题。 >>> a = [1,2,3,4] >>> a.pop(3) #or a.pop() 4 >>> print(a) [1,2,3]
我目前正在学习 Python 中的 pop() 函数并有一个问题。 >>> a = [1,2,3,4] >>> a.pop(3) #or a.pop() 4 >>> print(a) [1,2,3]
我可以将对象$push编码到Mongo数组上,如下所示: db.foo.update({},{$push:{bar:3}}) 但是我找不到一种语法,可以让我对列表中的最后一项进行$pop编码。 我已经
我是一名优秀的程序员,十分优秀!