简体   繁体   English

是否可以在 scanf 或 sscanf 中使用 strcpy? (C语言)

[英]Is it possible to use a strcpy inside a scanf or a sscanf? (in C language)

I read somewhere that when we use structs we can't just write something like « example1.name = "Jim";我在某处读到,当我们使用结构体时,我们不能只写类似 « example1.name = "Jim";东西example1.name = "Jim"; », instead of it we should write « strcpy(example1.name, "Jim"); », 而不是我们应该写 « strcpy(example1.name, "Jim"); » »

The thing is that I need to use a struct and I need to scanf (and right after that sscanf) some information that corresponds to a string and I don't know how I should do it.问题是我需要使用一个结构,我需要 scanf (然后在 sscanf 之后)一些与字符串对应的信息,我不知道我应该怎么做。

Could somebody help me, please?有人可以帮我吗?

Edit:编辑:

My code isn't complete and I know it's wrong, but I'll post it here so that you know what I am talking about:我的代码不完整,我知道这是错误的,但我会在这里发布它,以便您知道我在说什么:

int main(){

    struct Cards{
        int value;
        char type[4];
    };

    for(i=1, 0 < i && i < 11, i++){  
         struct Cards cardi;
    }

    scanf("%d %s", &cardi.value, cardi.type); 
/*at this point I just know it's wrong but I am really bugged.
  I thought about something like «scanf("%d %s", &cardi.value, strcpy(cardi.type, "%s"));»
  but I just know it's very wrong */ 


    return 0;
}

This isn't true only about structs, but for all strings.这不仅适用于结构,而且适用于所有字符串。 You can use = for strings, only when you initialize them.您可以将=用于字符串,仅当您初始化它们时。 You can scanf a string and place it in a string.您可以扫描一个字符串并将其放入一个字符串中。 Example:例子:

scanf("%s", my_struct.str);

If you already have a string and you want to pass it in a struct, or in an other string variable you then need strcpy:如果您已经有一个字符串,并且想将它传递到结构体或其他字符串变量中,那么您需要 strcpy:

char str1[] = "abc", str2[4];
strcpy(str2, str1);

or或者

strcpy(my_struct.str, str1);

Edit:编辑:

for(i=1, 0 < i && i < 11, i++) {
    struct Cards cardi;
}

In your code cardi is not card0, card1 etc, it is a struct Cards variable with the name cardi.在您的代码中,cardi 不是 card0、card1 等,它是一个名为 cardi 的结构 Cards 变量。

If you want to store 10 cards, you sould make an array of struct Cards with capacity of 10 like:如果你想存储 10 张卡片,你应该制作一个容量为 10 的结构卡片数组,例如:

struct Cards array[10];
for (i = 0; i < 10; i++) {
    scanf("%d %s", &array[i].value, array[i].type);
}

Anyway i suggest that you focus on learning the basics on arrays, strings and pointers before you use structs.无论如何,我建议您在使用结构之前专注于学习数组、字符串和指针的基础知识。

Edit 2: You don't want to define structs inside your main, because in that way, if you write a function it will not "see" your struct.编辑 2:您不想在 main 中定义结构,因为这样,如果您编写一个函数,它就不会“看到”您的结构。 Structs usually are written in the top of the code.结构通常写在代码的顶部。

Your use of scanf() is correct.您对scanf()是正确的。 However scanning strings using %s is dangerous and discouraged.然而,使用%s扫描字符串是危险的,不鼓励使用。 This is because the type member of your structure has space for only 4 characters, including the terminating '\\0' (NUL) character.这是因为结构的type成员只有 4 个字符的空间,包括终止的'\\0' (NUL) 字符。 Entering a string longer than 3 characters will result in the extra characters being written into an unknown area of memory likely corrupting other variables and leading to incorrect program execution or a program crash.输入超过 3 个字符的字符串将导致额外的字符被写入内存的未知区域,这可能会破坏其他变量并导致程序执行不正确或程序崩溃。 You can correct this by adding a string length limit, simply replace %s with %3s , where 3 is the maximum numbers of characters that will be accepted ( scanf will automatically add a '\\0' (NUL) character following the last scanned character).您可以通过添加字符串长度限制来纠正此问题,只需将%s替换为%3s ,其中 3 是将被接受的最大字符数( scanf将在最后一个扫描字符之后自动添加一个'\\0' (NUL) 字符)。

Below are some additional comments:以下是一些补充评论:

If you want to declare an array for 10 Cards, you could do it this way without any loops:如果你想为 10 张卡片声明一个数组,你可以在没有任何循环的情况下这样做:

    struct Cards cardi[10];

To scan values into the first card (C arrays are 0-based):要将值扫描到第一张卡片中(C 数组是从 0 开始的):

    // this will scan into the first card; type field is restricted to at most 3 characters
    scanf("%d %3s", &cardi[0].value, cardi[0].type); 

At the top of the file you'll want to add:在文件顶部,您需要添加:

#include <stdio.h>

This header file provides a prototype (declaration) for the scanf function (among many others).这个头文件为scanf函数(以及许多其他函数)提供了一个原型(声明)。

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

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