简体   繁体   English

在C中使用带指针的数组

[英]Using an array with pointers in C

Hello i'm practising C and i have a little problem with the following code. 您好我正在练习C,我对以下代码有一点问题。 First of all, my program just reads the input from the user and if there is memory availble, it stores it, otherwise it makes nothing. 首先,我的程序只是读取用户的输入,如果有可用的内存,它会存储它,否则它什么也没做。

I have an array of pointers to char called "lines" and an array of chars for the temporary storage of the input called "line". 我有一个名为“lines”的char指针数组和一个用于临时存储输入的字符数组,称为“line”。

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

#define MAXWIDTH 81
#define MAXLINES 100

int main ()

{
    char* lines [MAXLINES];   
    char line[MAXWIDTH];
    int i ;
    int n ;


Then i will check if my array of pointers has space and the input is non-zero. 然后我将检查我的指针数组是否有空格且输入是否为非零。 I do it in a for-loop to fill the array and normally the for-loop should stop when i type in nothing and just press enter or when the array is full. 我在for循环中填充数组,通常for-loop应该在我输入任何内容时停止,只需按Enter或数组已满。 If there is space i will check if there is enough memory in the space where the pointer is pointing to. 如果有空格,我将检查指针所指向的空间是否有足够的内存。 If that's ok (!= NULL), the program copies the input from gets(line) in the memory. 如果没关系(!= NULL),程序将从内存中的gets(line)复制输入。

for (n = 0; n < MAXLINES && gets(line) != NULL; n++)    

        {
            if ((lines[n] = malloc(strlen(line) + 1)) == NULL)  
                exit (1);
            strcpy(lines[n], line);                             
        }


The rest of the code is just for the output and freeing of the memory. 其余代码仅用于输出和释放内存。

    for (i = 0; i < n; i++)
        {
            puts(lines[n-i-1]);
            free(lines[n-i-1]);
        }


    return 0;
}


Now the problemis , that the program runs without any errors, but it's not working as i want. 现在的问题是,程序运行没有任何错误,但它不能按我的意愿运行。 It is just performing a infinte loop where i can type in as long as i want, what i want without any reaction. 它只是执行一个infinte循环,我可以输入,只要我想要,我想要什么,没有任何反应。

gets doesn't return NULL when you type an empty line, if that's what you tried to check for. 当您键入空行时, gets不会返回NULL,如果这是您尝试检查的内容。 It will still be an empty string. 它仍然是一个空字符串。 You'll need to check if the first character is a \\0 if you want to look for empty lines. 如果要查找空行,则需要检查第一个字符是否为\\0

On a side note, gets is extremely unsafe since it will overrun your buffer if your line is too long and cause evil bugs. 另一方面, gets是非常不安全的,因为如果你的线路太长而导致恶臭,它会超出你的缓冲区。 Use fgets instead, it lets you specify the size of your buffer. 而是使用fgets ,它允许您指定缓冲区的大小。 (Note that fgets will add a \\n to the end of your string, even if it's an empty line.) (注意fgets会在字符串的末尾添加\\n ,即使它是空行。)

Well for a start, I suggest reading the following about why you should avoid using gets() . 好吧,首先,我建议您阅读以下有关为什么要避免使用gets() I suggest using scanf() or fgets() instead... 我建议使用scanf()fgets()代替......

http://www.gidnetwork.com/b-56.html http://www.gidnetwork.com/b-56.html

then note that you're doing a loop to 100 taking input, and only after all 100 are you outputting. 然后注意你正在做一个循环到100输入,并且只有在你输出所有100之后。 So you'll need to enter 100 lines of input currently before you see anything... 因此,在您看到任何内容之前,您需要输入100行输入...

You're not checking for an empty line. 你没有检查空行。

You need something like: 你需要这样的东西:

if('\0' == line[0])
{
    break;
}

And use fgets() not gets(). 并使用fgets()而不是gets()。 It's safer. 它更安全。 However you then need to do: 但是,您需要这样做:

if('\n' == line[0] || '\r' == line[0])
{
    break;
}

Well, how do you terminate your input? 那么,你如何终止你的输入? Entering an empty line won't help because line will be "" not NULL . 输入一个空行无济于事,因为行""不是NULL Have you tried pressing Ctrl+Z in the console (if it's windows)? 您是否尝试在控制台中按Ctrl + Z(如果是Windows)?

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

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