简体   繁体   English

标志验证不起作用。 使用数组,scanf函数和strcmp

[英]Flag validation doesn't work. Using arrays, scanf function and strcmp

I'm a beginner. 我是初学者。 I'm trying to write a program which will validate some words which I will enter with a given set of available words and then compare them. 我正在尝试编写一个程序,该程序将验证我将使用给定的一组可用单词输入的一些单词,然后进行比较。 What I'm using in this code is what I've learned so far. 到目前为止,我在这段代码中使用的是我所学到的。 Please help me to understand what is wrong with this code. 请帮助我了解此代码有什么问题。

So, when I enter a word, like "flag", it prints an error 因此,当我输入单词(例如“ flag”)时,它会显示一个错误

"...line 71: 1872 Sigmentation fault sh"${SHFILE}... “ ...第71行:1872 Sigmentation错误sh” $ {SHFILE} ...

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

int main(void) {
    system("COLOR B0");

    char *enter_flags[3];

    char*available_flags[3]={"print","scan","flag"};

    printf("\r\nEnther your flags here please:  ");

    for(int i=0;i<3;i++){

        scanf("%s",&enter_flags[i]);

        for(int j=0;j<3;j++){
            if(strcmp(enter_flags[i],available_flags[j])==0)
            {
                printf("---%s---|---%s--- MATCH", enter_flags[i], available_flags[j]);
            }

            else printf("---%s---|---%s--- INCORRECT", enter_flags[i], available_flags[j]);
        }


    }
    return 0;
}

I Understood my mistake. 我理解了我的错误。 Thank you all! 谢谢你们!

The main problem with your code is that you do not reserve any memory for storing the values (aka strings) entered by the user. 代码的主要问题是,您不保留任何用于存储用户输入的值(即字符串)的内存。

This line 这条线

char *enter_flags[3];

reserves memory for 3 char pointers but no memory for the strings (aka char arrays). 保留3个char指针的内存,但不存储字符串(aka char数组)的内存。

Instead you can do something like: 相反,您可以执行以下操作:

char enter_flags[3][32];

This will allow you to store 3 strings with max length 31 (plus the required zero termination). 这将允许您存储3个最大长度为31的字符串(加上所需的零终止符)。

To avoid overflow your scanf should then be: 为了避免溢出,您的scanf应该为:

scanf("%31s", enter_flags[i]);

btw.... 顺便说一句...

Now you may wonder why this code char *available_flags[3]= "print","scan","flag"}; 现在您可能想知道为什么此代码char *available_flags[3]= "print","scan","flag"}; is okay when char *enter_flags[3]; char *enter_flags[3]; is wrong. 是错的。

The reason is that available_flags is 3 char pointers to (constant) string literals. 原因是available_flags是指向(常量)字符串文字的3个char指针。 The compiler will place the 3 strings somewhere in memory and make the pointers in available_flags point to the strings. 编译器会将这三个字符串放在内存中的某个位置,并使available_flags的指针指向这些字符串。 Your code does not change these strings (and isn't allowed to). 您的代码不会更改这些字符串(并且不允许这样做)。

enter_flags is different because you want it to hold 3 strings that your program can change (ie via user input). enter_flags是不同的,因为您希望它包含3个字符串,程序可以更改它们(即,通过用户输入)。 Therefore enter_flags can't be 3 char pointers. 因此enter_flags不能是3个char指针。 There must be memory for storing the user input. 必须有用于存储用户输入的内存。 You achieve that by making enter_flags a 2 dimension char-array. 您可以通过将enter_flags为二维char-array来实现。 In C, a 2 dimensional char array can be used as an array of strings. 在C语言中,二维char数组可以用作字符串数组。

you are using the enter_flags array ,this var is array of pointer and it's hold unknown addresses because you just declare it without any value.you have to specify that this this varibles is hold at least 6 chars because the largest keyword or flag is "print" and have 5 chars . 您正在使用enter_flags数组,此var是指针数组,并且包含未知地址,因为您只声明了它而没有任何值。您必须指定此varible至少包含6个字符,因为最大的关键字或标志是“ print并且有5个字符。
the solution is declaring 解决方案是声明

char enter_flags[3][6];

or you should use malloc function. 或者您应该使用malloc函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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