如何解决C语言中的分段错误?
你能帮我解这个密码吗?我写这篇文章是为了学术目的,我也有一些错误和分段错误。我会很感激的 我正在学习C语言中的堆栈。我需要根据ASCII表中的值组织字母(如char Contents所示),并使用一些规则,正如您在我的上一个函数中看到的那样 为了解释我的代码,我从两个结构开始。然后创建一个函数来分配内存,然后对堆栈中的元素进行分组。但是,最后一个函数是使用创建的每个堆栈地址来组织数组,以解决我的问题如何解决C语言中的分段错误?,c,gcc,compiler-errors,segmentation-fault,C,Gcc,Compiler Errors,Segmentation Fault,你能帮我解这个密码吗?我写这篇文章是为了学术目的,我也有一些错误和分段错误。我会很感激的 我正在学习C语言中的堆栈。我需要根据ASCII表中的值组织字母(如char Contents所示),并使用一些规则,正如您在我的上一个函数中看到的那样 为了解释我的代码,我从两个结构开始。然后创建一个函数来分配内存,然后对堆栈中的元素进行分组。但是,最后一个函数是使用创建的每个堆栈地址来组织数组,以解决我的问题 typedef struct no { int dado; struct no
typedef struct no {
int dado;
struct no *prx;
} no_t;
typedef struct pilha {
int altura;
no_t *topo;
} pilha_t;
pilha_t* pilha_cria () {
pilha_t *p = (pilha_t*)malloc(sizeof(pilha_t));
p->altura = 0;
p->topo = NULL;
return p;
}
void pilha_empilha (pilha_t *p, char conteiner) {
if (p == NULL) return;
no_t *no = (no_t*)malloc(sizeof(no_t));
no->dado = conteiner;
no->prx = p->topo;
p->topo = no;
p->altura++;
}
int pilha_organiza (int altura, char *conteiners, int quantidade) {
pilha_t *p = pilha_cria();
pilha_empilha(p, conteiners[0]);
pilha_t *v = pilha_cria();
pilha_empilha(v, p); //register P address in V array
v->altura++;
int k = 1, j=0;
while (k < quantidade) {
if (p->topo->dado == conteiners[k] && p->altura < altura) {
pilha_empilha(p, conteiners[k]);
}
else if (p->topo->dado < conteiners[k]) {
p++; //mudar de posicao
p = pilha_cria();
pilha_empilha(p, conteiners[k]);
pilha_empilha(v, p); //register P address in V array
v->altura++;
}
else {
while (j < quantidade) {
if (v->topo->dado > conteiners[k]) { //SEG FAULT HERE!
pilha_empilha(v, conteiners[k]);
}
j++;
v++;
}
}
k++;
}
printf("v altura: %d\n", v->altura);
return 1;
}
int main() {
int altura = 6;
char conteiners[] = "AKKEFAGCFE";
int quantidade = strlen(conteiners);
if (quantidade == 0) return 0;
pilha_organiza (altura, conteiners, quantidade);
return 1;
}
typedef结构号{
国际护墙板;
结构号*prx;
}没有,;
typedef结构pilha{
阿尔图拉国际酒店;
无地形图;
}皮哈乌特;
pilha_t*pilha_cria(){
pilha_t*p=(pilha_t*)malloc(sizeof(pilha_t));
p->altura=0;
p->topo=NULL;
返回p;
}
无效pilha_empilha(pilha_t*p,字符内容){
如果(p==NULL)返回;
no_t*no=(no_t*)malloc(sizeof(no_t));
否->护墙板=上下文;
否->prx=p->地形;
p->topo=否;
p->altura++;
}
国际组织(国际altura、char*Contents、国际quantidade){
pilha_t*p=pilha_cria();
pilha_empilha(p,conteiners[0]);
pilha_t*v=pilha_cria();
pilha_empilha(v,p);//在v数组中注册p地址
v->altura++;
int k=1,j=0;
while(ktopo->dado==contencers[k]&&p->alturatopo->dado<上下文[k]){
p++;//穆达尔·德波西科
p=pilha_cria();
pilha_empilha(p,conteiners[k]);
pilha_empilha(v,p);//在v数组中注册p地址
v->altura++;
}
否则{
而(jtopo->dado>conteiners[k]){//SEG故障在这里!
pilha_empilha(v,conteiners[k]);
}
j++;
v++;
}
}
k++;
}
printf(“v altura:%d\n”,v->altura);
返回1;
}
int main(){
int altura=6;
char contents[]=“AKKEFAGCFE”;
int quantidade=strlen(上下文);
如果(quantidade==0)返回0;
pilha_组织(altura、conteiners、quantidade);
返回1;
}
回答:v->altura应该是4。
感谢阅读。欢迎阅读堆栈溢出。当您编写代码时,从一些小而简单、工作完美的东西开始,然后以小的增量增加复杂性,并在每一步进行测试,这一点至关重要。永远不要添加不起作用的代码。如果你在没有测试的情况下编写了这么多的代码,那么它将包含许多bug,并且它们将很难被隔离代码>你为什么这么做
v
似乎是指向单个pilha\u t
的指针。它不是数组。看起来这将导致v
成为无效指针,可能是seg故障的原因。@kaylum还有一个p++在那之前,我知道他为什么这么做。Caio,你能解释一下你为什么这么做吗?你用不同的参数调用pilha_empila:指针和字符(p,contencers[k]),指针和指针(v,p)。