简体   繁体   中英

Comparing strings passed to function via char-pointers

I need help to compare two string on a functions using pointers, I know how to do this without using pointers.

Here is the code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

char *randString(int dif);
void compare(char *ptr, char *palabra[10]);
int main (){
    srand(time(NULL));

    int contador = 0;
    int dif;
    char *palabra[10];
    /*while (  contador < 100 ){
        char* ptr = randString();
        printf ( "%s\n", ptr );

        contador++;
    }*/
    printf("Dificultad: ");
    scanf("%i",&dif);
    char *ptr=randString(dif);
    printf("Palabra seleccionada: %s\n",ptr);

    printf("Introduce palabra para comparar: ");
    scanf("%s",palabra);

    compare( ptr, palabra );

    getchar();
    return 0;
}

static char difone[5][100] ={
    {"Duck"},
    {"Taxi"},
    {"Hola Mundo!"},
    {"Paris"},
    {"Lexugon"}
};
static char diftwo[5][100] ={
    {"colan"},
    {"cocuy"},
    {"cojee"},
    {"clona"},
    {"cobra"}
};
static char difthree[5][100] ={
    {"cogen"},
    {"codee"},
    {"cocio"},
    {"cogio"},
    {"coces"}
};

char *randString(int dif) {
    if(dif==1){
        return difone[rand() % ( 4 - 0 )] ;
    }else{
        if(dif==2){
            return diftwo[rand() % ( 4 - 0 )] ;
        }else{
            if(dif==3){
                return difthree[rand() % ( 4 - 0 )] ;
            }
        }
    }
}

and this is the compare function:

void compare(char* ptr, char *palabra[10]){
    printf("Palabra seleccionada: %s\n",ptr);
    printf("Palabra introducida: %s",palabra);

    if(strcmp((*ptr)[1],(*palabra)[1]==0){
        printf("Igual");
    }else{
        printf("No igual");
    }

}

So, several issues:

First, you declare palabra as an array of pointer to char , which is not what you want based on the code:

  scanf("%s",palabra);

If you want palabra to store a single string of 9 characters or less, simply declare it as

char palabra[10];

This means you'll also need to change the declaration and definition of your compare function, like so:

void compare( char *ptr, char *palabra )

Next, change your strcmp call in the compare function to

if( strcmp( ptr, palabra ) ==0 ){

The expression (*ptr)[1] attempts to dereference ptr and then apply the subscript to the result, which is not going to work in this case. The result of *ptr is the value of the first character in the string ptr points to (one of the strings in your difone , diftwo , and difthree arrays). A single character is not an array or pointer expression, so you can't apply the [] operator to it.

Except when it is the operand of the sizeof or unary & operators, an array expression like palabra will be converted ("decay") to a pointer expression, and the value of the pointer will be the address of the first element of the array. We declare palabra as a 10-element array of char in main , but when we pass it as an argument to scanf or compare , that array expression is converted to a pointer expression, which is why we declare it as char *palabra in the definition to compare .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM