antonio jimenez
antonio jimenez

Reputation: 11

Unexpected result array of characters C

The objective of the exercise is to get the index of the first character of the array that is equal with the character I give as an argument. The problem is that always give me the number -256 instead of the index.

I have tried to change the while conditions and the % of printf and anything make the difference. Here is the code.

#include <stdio.h>

#define DIM 80

int main() {
    char argumento;
    char linea[DIM];
    int res = -1;
    printf("Ponga el caracter argumento\n");
    scanf("%s", &argumento);
    printf("ponga la cadena:\n");
    scanf("%s", linea);

    int i = 0;

    while (linea[i] != '\0' && res == -1) {
        if (linea[i] == argumento) {
            res = i;
        }
        ++i;
    }

    printf("El primer caracter que coincide es de indice: %d", res);
    return 0;
}

Upvotes: 1

Views: 66

Answers (1)

chqrlie
chqrlie

Reputation: 144540

Your code has undefined behavior.

scanf("%s", &argumento) will write beyond the end of the variable argumento, possibly corrupting other variables, such as res. The observed behavior is consistent with scanf() writing a null byte at the address of res, changing its initialized value from -1 to -256, causing the loop to exit immediately and the output you observe.

The behavior is undefined, anything else could happen, you should use %c to read a byte from stdin, but beware to ignore pending white space with a space in front of %c if you read other input using scanf(). Also pass the maximum number of characters to store to the destination arrays for %s to avoid another buffer overflow: use %79s in your case. And always test the return value of scanf.

Here is a modified version:

#include <stdio.h>

int main() {
    char argumento;
    char linea[80];

    printf("Ponga el caracter argumento\n");
    if (scanf(" %c", &argumento) != 1)
        return 1;

    printf("ponga la cadena:\n");
    if (scanf("%79s", linea) != 1)
        return 1;

    int res = -1;
    int i = 0;
    while (linea[i] != '\0') {
        if (linea[i] == argumento) {
            res = i;
            break;
        }
        ++i;
    }

    printf("El primer caracter que coincide es de indice: %d\n", res);
    return 0;
}

scanf() is very tricky and error prone, it is recommended to use fgets() instead to read a line of input from stdin and parse the contents appropriately:

#include <stdio.h>

int main() {
    char linea[80];

    printf("Ponga el caracter argumento\n");
    if (!fgets(linea, sizeof linea, stdin))
        return 1;

    char argumento = linea[0];

    printf("ponga la cadena:\n");
    if (!fgets(linea, sizeof linea, stdin))
        return 1;

    int res = -1;
    for (int i = 0; linea[i] != '\0'; i++) {
        if (linea[i] == argumento) {
            res = i;
            break;
        }
    }

    printf("El primer caracter que coincide es de indice: %d\n", res);
    return 0;
}

Upvotes: 1

Related Questions