阅读背景:

如何限制用户输入字符串的大小或动态分配内存

来源:互联网 

So I have this piece of code:

所以我有这段代码:

void main()
{
    char word[21]={0}; // changed from 20 to 21 because of '

So I have this piece of code:

所以我有这段代码:

void main()
{
    char word[21]={0}; // changed from 20 to 21 because of '\0'.
    // do {
    scanf("%s", &word);
    // } while (strlen(niz)>20);  this line is useless because program crashes anyway
}
  1. Question: The thing is I want to limit the number of characters user wants to input to 20. Sure I could reserve more memory for word[] but if I went over certain number the program would crash again. How do you get around this in C?

    问题:问题是我想限制用户想要输入的字符数为20.当然我可以为word []保留更多内存但是如果我超过一定数量,程序将再次崩溃。你如何在C中解决这个问题?

  2. Question: Is there any way I can allocate needed memory for the string just after user enters it. For example if user enters string that has length of 999 can I allocate sizeof(string) * strlen(word) + 1 bytes of memory?

    问:有没有办法在用户输入字符串后为字符串分配所需的内存。例如,如果用户输入长度为999的字符串,我可以分配sizeof(字符串)* strlen(字)+ 1字节的内存吗?

Note: if strlen() exceeds certain limit I want to prompt user to enter a new string again.

注意:如果strlen()超出了某个限制,我想提示用户再次输入一个新字符串。

5 个解决方案

#1


2  

You can limit the string length by using fgets:

您可以使用fgets限制字符串长度:

fgets(word, sizeof(word), stdin);  

For second part:
Unfortunately, In C you can't allocate the exact amount of memory needed for the string entered. The reason is that you need to know the length of string before allocating memory for it and for this you must have to store that string in a buffer.

对于第二部分:不幸的是,在C中,您无法分配输入字符串所需的确切内存量。原因是在为它分配内存之前需要知道字符串的长度,为此必须将该字符串存储在缓冲区中。

If you want to enter a string of any size and save memory at the same time, then use realloc.
Test program:

如果要输入任意大小的字符串并同时保存内存,请使用realloc。测试程序:

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

int main(void)
{
    char *s = malloc(1);
    printf("Enter a string: \t"); // It can be of any length
    int c;
    int i = 0;
    do
    {
        c = getchar();
        s[i++] = c;
        s = realloc(s, i+1);
    }while(c != '\n' && c != EOF);

    s[i] = '\0';
    printf("Entered string: \t%s", s);
    return 0;
}

#2


1  

Some versions of scanf support m modifier. For example in glibc>=2.7. They allocate necessary buffer for the user:

某些版本的scanf支持m修饰符。例如,在glibc> = 2.7中。他们为用户分配必要的缓冲区:

   char *p;
   int n = scanf("%ms", &p);
   if (n == 1) {
       printf("read: %s\n", p);
       free(p);
   } else {
       // error
   }

There is also older a modifier which does the same.

还有一个较旧的修饰符也是如此。

#3


0  

Put a character constant in front of s in %s:

在%s中将字符常量放在s前面:

scanf("%20s", &word);

Your second question: in short No. You need memory space to take the input. A common solution however, to reduce allocations is do do something like:

你的第二个问题:简而言之。你需要内存空间来接受输入。但是,一个常见的解决方案是减少分配,例如:

char tmp[1000];
char *strptr;

scanf("%s", &temp);
strptr = malloc(sizeof(char)*(strlen(tmp)+1));

but that will only save you space if you call this function 1000s of times.

但如果你多次调用这个函数,那只能节省你的空间。

#4


0  

You can limit the input by using fgets instead.

您可以使用fgets来限制输入。

fgets(word, sizeof(word), stdin);

if later need to extract things from the string use sscanf

如果以后需要从字符串中提取东西使用sscanf

#5


0  

Use command line arguments.. main(int argc, char *argv[ ])

使用命令行参数.. main(int argc,char * argv [])

Here argc will tell you how many arguments are passed when you are giving executable.

这里argc会告诉你在给出可执行文件时传递了多少个参数。

argv will give you the data or string.. Just give it to a 'char *'.

argv会给你数据或字符串..只需将它赋予'char *'即可。


'. // do { scanf("%s", &word); // } while (strlen(niz)>20); this line is useless because program crashes anyway } voi



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: