How to turn into a tiny value of a struct?

Asked

Viewed 113 times

0

The next function should turn variables into lowercase and then compare them. I’ve tried converting before, but also bugged. Arithmetic use of pointers together with variables of a struct. Pointer points to the struct.

How to fix the error present in the following code snippet:

struct registro{ 
    char conta[50], senha[50], usuario[50];
}atual[1000], *ptr;

int verifica_usuario(int *ptr){
    int i;
    for(i = 0; i < *ptr; i++) {
        if(strcmp(tolower(atual[*ptr].usuario), tolower(atual[i].usuario)) == 0) {
                return 0;
        }
    }
    return 1;
}
  • "Arithmetic pointer use" no, you do not use pointer arithmetic. You use a simple positional vector access atual. And also pass int *ptr as argument seems quite unnecessary for the use being made of it

  • Yes, yes. My goal follows this conviction, I want the program to classify "NAME" == "name", hence the tolower. the program receives an entry in which it asks for a user name, but this name cannot already be registered, even if there is divergence between upper and lower case.

  • 2

    tolower receives a char and returns a char, so it makes no sense to call for a char char[] or char*. atual.usuario is of what type ?

  • It’s kind of char, too.

  • Put the definition of struct in the question, so that it is evident to anyone who wants to answer

  • What Isac meant was, how’s the code of struct between the word recervada struct, your key opener { and their respective key closes } with all field definitions and their respective names

  • What are you calling your role in main ?

  • verific = verifica_account(&v);

Show 3 more comments

1 answer

1


Although there are insensitive functions, it is not part of the standard. To run anywhere create your own function:

#include <stdio.h>
#include <ctype.h>
 
int stricmp(char const *s1, char const *s2) {
   while (1) {
        int res = tolower(*s1) - tolower(*s2);
        if (res != 0 || !*s1) return res;
        s1++;
        s2++;
    }
}
 
int main(void) {
    printf("%d\n", stricmp("aaa", "AAA"));
    printf("%d\n", stricmp("aaa", "aaa"));
    printf("%d\n", stricmp("aaa", "AAB"));
    printf("%d\n", stricmp("abb", "AAB"));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Then check the member of the structure I think you already know. Follow the recommendations of the comments, the algorithm is more complicated than it should be. And there are other mistakes in it, but that’s another problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.