简体   繁体   English

我在字符串中有问题,在使用 get 函数时,在 c 语言中

[英]I have problem in string, in using the gets function, in c language

I have a problem with string in the c language we know, the string is known as a null-terminated character** array, in the string, we declare how many characters this string is store.我在我们知道的c语言中字符串有问题,该字符串被称为空终止字符**数组,在字符串中,我们声明这个字符串存储了多少个字符。 and the memory of the character is 1byte.字符的内存为1byte。 when we declare char name[5];当我们声明字符名称[5]时; it means the string store at least 4 characters value and null value.这意味着字符串存储至少 4 个字符的值和空值。

but there is a problem with strong text the gets function it.但是强文本存在问题,获取功能。 when we have entered a name that contains more than 5 characters it is accepted and prints all characters.当我们输入包含超过 5 个字符的名称时,它会被接受并打印所有字符。 In this case, the compiler does not throw the warning.在这种情况下,编译器不会抛出警告。

Please tell me the solution to it....... the code is below.请告诉我解决方法......代码如下。

#include<stdio.h>

int main()
{
    char name[4]="smti prajapati";

    printf("Print the name througth the initilizing : ");
    puts(name);


    printf("Enter the name : ");
    gets(name);
    printf("Print the name througth the user : ");
    puts(name);

    return 0;
}

In the terminal program trow the look like this error在终端程序中,看起来像这个错误

rough.c: In function 'main':
rough.c:5:18: ***warning: initializer-string for array of chars is too long***
     char name[1]="smti prajapati";
                  ^~~~~~~~~~~~~~~~
Print the name througth the initilizing : s3
Enter the name : smit prajapati
Print the name througth the user : smit prajapati

You've declared the name array to have a single element;您已将name数组声明为具有单个元素; it's not large enough to hold the contents of the string "smti prajapati" .它不足以容纳字符串"smti prajapati" You'd need to declare name to be at least 15 elements wide - 14 characters in the string plus the zero terminator.您需要将name声明为至少15 个元素宽 - 字符串中的 14 个字符加上零终止符。

You can omit the size in the declaration and write您可以省略声明中的大小并写入

char name[] = "smti prajapati";

and the array size will be taken from the size of the initializer.数组大小将取自初始化程序的大小。

Note that the size of name is fixed after it is defined, and will not able to store strings longer than the initializer.请注意, name的大小在定义后是固定的,并且无法存储比初始化程序更长的字符串。

Do not use gets ;不要使用gets if you enter a string that's too long for the array, it will write those extra characters to whatever follows the array.如果您输入的字符串对于数组来说太长,它会将这些额外的字符写入数组后面的任何内容。 This is known as a buffer overflow, and can lead to anything from corrupted data, a runtime error, or branching to a random location in your program.这称为缓冲区溢出,可能导致数据损坏、运行时错误或分支到程序中的随机位置等任何事情。 Buffer overflows are a common malware exploit.缓冲区溢出是一种常见的恶意软件利用。 Use fgets instead, as it allows you to limit how many characters get written to the target.请改用fgets ,因为它允许您限制写入目标的字符数。

The declaration and assignment:声明和赋值:

char name[1] = "smti prajapati";

Can never work because you assign a string with 14 characters plus the null byte to a char array that only has space for 1 character.永远无法工作,因为您将一个包含 14 个字符的字符串加上空字节分配给一个只有 1 个字符的空间的 char 数组。

The reason why you don't get errors or warnings with some ill-formed code can be explained by leeway given by C to the programmer, compilers are not mandated to issue warnings due to some types of ill-formed code like your own, this falls in the category of undefined behavior , and it's up to the programmer to avoid it.某些格式错误的代码不会出现错误或警告的原因可以通过 C 给程序员的余地来解释,由于某些类型的格式错误的代码(例如您自己的代码),编译器不会被强制发出警告,这属于未定义行为的类别,由程序员来避免它。

You could use:你可以使用:

char name[] = "smti prajapati";

This way the size of the array will be deduced when initialized.这样,数组的大小将在初始化时推导出来。

Or if you want to explicitly use a size:或者,如果您想明确使用尺寸:

char name[15] = "smti prajapati";

Note that I added an extra space for the null byte, it is mandatory if you want to handle name as a propper string.请注意,我为空字节添加了一个额外的空间,如果您想将name作为正确的字符串处理,这是强制性的。 Also note that once the char array is given a size, it will not change, so when you store other strings in name keep that in mind.还要注意,一旦给了 char 数组一个大小,它就不会改变,所以当你在name存储其他字符串时,请记住这一点。


The other problem is gets , you should never use it, it's a very dangerous function, it does not check the bounds of the destination buffer and therefore can easily cause a buffer overflow and that can cause all kinds of trouble .另一个问题是gets ,你不应该使用它,它是一个非常危险的函数,它不检查目标缓冲区的边界,因此很容易导致缓冲区溢出,从而导致各种麻烦 Recent compilers don't even support it anymore as it was deprecated and later removed from the C standard.最近的编译器甚至不再支持它,因为它已被弃用,后来从 C 标准中删除。

Even those which do still support it often issue warnings similar to:即使那些仍然支持它的人也经常发出类似于以下内容的警告:

warning: the `gets' function is dangerous and should not be used.警告:`gets' 函数是危险的,不应使用。

Here are two examples of the output of two modern compilers when you use it.下面是两个现代编译器在使用时的输出示例

Summarizing and concluding, use fgets instead:总结和总结,使用fgets代替:

fgets(name, sizeof name, stdin);

When you use arrays in C you are using a fixed memory space.当您在 C 中使用数组时,您使用的是固定的内存空间。 If I want to save "Hello World!"如果我想保存“Hello World!” in memory, I have to reserve an array of length 13. In C it looks as:在内存中,我必须保留一个长度为 13 的数组。在 C 中它看起来像:

char aString[13] = "Hello World!"

It may go as high as 20, but no lower than 13. Why?它可能高达 20,但不会低于 13。为什么? Strings have characters that we care and the last one is the null character (its value is 0).字符串具有我们关心的字符,最后一个是空字符(其值为 0)。 So, you need to reserve thirteen characters.因此,您需要保留 13 个字符。

In your code you are reserving only 1 byte (1 character).在您的代码中,您只保留了 1 个字节(1 个字符)。 At least, you have to reserve 15 bytes.至少,您必须保留 15 个字节。

Try this code:试试这个代码:

#include<stdio.h>

int main()
{
    char name[15]="smti prajapati";

    printf("Print the name througth the initilizing : ");
    puts(name);


    printf("Enter the name : ");
    gets(name);
    printf("Print the name througth the user : ");
    puts(name);

    return 0;
}

I compiled the code without problems.我编译代码没有问题。 Also, you are able to not specify the length and It is going to work right.此外,您可以不指定长度,它会正常工作。 The best solution is dynamic memory.最好的解决方案是动态内存。 Avoid gets().避免gets()。 It has security issues.它有安全问题。 Instead, use fgets().相反,使用 fgets()。

You have a number of misunderstandings.你有很多误解。 Let's look at them in order.让我们按顺序看看它们。

in the c language we know, the string is known as a null-terminated character** array在我们知道的 c 语言中,字符串被称为空终止字符**数组

In C, a string is a null-terminated array of char .在 C 中,字符串是以空char结尾的char数组。 It is very common to refer to a string using a character pointer, char * .使用字符指针char *来引用字符串是很常见的。 It may have been a typo when you said "character**", but type char ** is a pointer-to-pointer, which is something else.当你说“character**”时可能是一个错字,但类型char **是一个指向指针的指针,这是别的东西。

when we declare char name[5];当我们声明字符名称[5]时; it means the string store at least 4 characters value and null value.这意味着字符串存储至少 4 个字符的值和空值。

Correct.正确的。

char name[1]="smti prajapati";

This is incorrect.这是不正确的。 Your compiler correctly warned you: warning: initializer-string for array of chars is too long您的编译器正确警告您: warning: initializer-string for array of chars is too long

when we have entered a name that contains more than 5 characters it is accepted and prints all characters.当我们输入包含超过 5 个字符的名称时,它会被接受并打印所有字符。

Right.正确的。 Sometimes, when you break the rules, you get away with it.有时,当您违反规则时,您就可以逃脱惩罚。

In this case, the compiler does not throw the warning.在这种情况下,编译器不会抛出警告。

Right.正确的。 C does not always detect or complain about buffer overflow. C 并不总是检测或抱怨缓冲区溢出。 In general, it is your responsibility to make sure your arrays are allocated large enough for the strings you try to store in them.通常,您有责任确保为您尝试存储在其中的字符串分配足够大的数组。

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

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