mar*_*kmb 4 c stack-overflow pointers
我是C的新手,我正在尝试一些我发现的练习.
在其中一个练习中,我正在尝试使用指向字符串的指针(char数组),但它不起作用.它编译,但是当执行时,它会抛出"堆栈溢出"(好吧,我认为是"堆栈溢出",因为我用西班牙语).
这些是有问题的线:
//This is the variable declaration, before this, there is the "main function" declaration
char entrada[100];
char *ult=entrada;
char cantidadstr[10];
int i,j,k = 0;
int res;
scanf ("%s",entrada);
printf ("\n%s",entrada);
//Here crashes
printf ("Hola %s",ult);
while (*ult != "\0"){
//And here there's more code
Run Code Online (Sandbox Code Playgroud)
先感谢您!!
(我不能回答:))然后,我会发布一些代码.
当我执行时,插入数据后,它会抛出"Violacióndesegmento",谷歌说这意味着Stack Overflow
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
char entrada[1001*11*101];
/*Asi tenemos el tamano maximo:
1001 por las 1000 posibles lineas, mas la primera
11 por el tamano maximo del numero (1 + 9 ceros), mas el espacio o salto de linea siguiente
101 por el numero de numeros por linea, mas el primero
*/
char *ult=entrada;
char cantidadstr[10];
int i,j,k = 0;
int res;
memset (entrada,'\0',1001*11*101);
scanf ("%s",entrada);
printf ("\n%s",entrada);
//poniendo ese print ahi arriba, ese me lo muestra, por tanto, el fallo esta en el puntero de debajo de esta linea
printf ("Hola %s",ult);
while (*ult != "\0"){
if(*ult == "\n"){
if(i != 0){
printf("\n");
}
i++;
j = 0;
}
else if(i != 0){
if(*ult == " "){
j++;
k=0;
res = atoi(cantidadstr);
printf("%d ",res*2);
//Este es el otro cambio que hablaba
cantidadstr[10] = '\0';
}
else if(j != 0){
cantidadstr[k] = *ult;
}
}
k++;
*ult++;
}
return 0;
Run Code Online (Sandbox Code Playgroud)
}
这是完整的代码,其他论坛的西班牙语评论."entrada"的大小足以支持练习中发送的任何数据.刚刚添加了"memset".第二条评论显示它崩溃的地方
谢谢你们的快速响应!!
while循环之前的代码很好,因为它编译并正确运行(据我所知)
但是while循环有一个错误,我不确定它是如何在你的情况下编译的.因为你写过
while (*ult != "\0"){
这给出了编译错误
*ult is of type char
"\0" is of type const char*
Run Code Online (Sandbox Code Playgroud)
你必须将"\ 0"转换为'\ 0'