4

I'm new in C, and I'm trying some exercises that I found.

In one of the exercises I'm trying to use a pointer to a string (a char array), but it doesn't work. It compiles, but when is executed, it throws "stack overflow" (well, I think is an "stack overflow" because I have it in spanish).

These are the problematic lines:

//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

Thank you in advance!!

EDIT

(I can't answer me :)) Then, I'll post a bit more of code.

When I execute, after inserting data, it throws "Violación de segmento", and google says that means 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;

}

This is the exact and full code, with comments in spanish for another forum. The size of "entrada" is big enough for any data send in the exercise. The "memset" is just added. The second comment shows where it crashes

Thank you for your quick answer!!

5
  • Are you perhaps entering in more than 100 characters of input in the scanf? I'd also like to see the exact error, even in spanish, though google translate is your freind there. Commented Sep 15, 2011 at 16:11
  • Shouldn't crash up to there unless the string you enter is longer than 99 characters... Commented Sep 15, 2011 at 16:13
  • Ok, if after 5 minutes, we've got no positive answers, I'm going to say "Post more code" because there's a strong chance that something elsewhere may be corrupting memory causing the issue. Commented Sep 15, 2011 at 16:18
  • Are you sure that it crashes at the printf? Try putting printf("Hola %s", ult); fflush(stdout); and/or just exiting after that line. It's possible the problem is later. Commented Sep 15, 2011 at 16:29
  • The error message is called Segmentation Fault in English. Commented Sep 15, 2011 at 16:32

2 Answers 2

5

The code before the while loop is fine as it compiles and runs correctly(as far as i can think)

But the while loop has an error i'm not sure how it compiled in your case. because you have written

while (*ult != "\0"){

which gives compiler error as

*ult is of type char
"\0" is of type const char*

you have to convert "\0" to '\0'

Sign up to request clarification or add additional context in comments.

2 Comments

THIS WORKS!! Thank you very much. I didn't know it was a difference between both!! And I love this forum, is the first time, but I'll use it lots of times more :D Thanks again!!
Try using warnings when you compile, eg. with gcc add -Wall
2

The following line:

cantidadstr[10] = '\0';

will write past the end of cantidadstr, which is definitely bad and most likely causing your stack overflow. If you want to null terminate cantidadstr, use cantidadstr[9]= '\0';. Arrays in C are zero based, not one based, so the first element of an array of size N starts at [0] and the last referenceable element is [N-1].

2 Comments

Might also mention that he actually wants cantidadstr[k] = '\0'; and it should be before the atoi.
This was a known error, and I wanted to clean the variable, but I was sure the problem wasn't there. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.