简体   繁体   English

strcpy()和字符串数组

[英]strcpy() and arrays of strings

I need to store the input from a user into an array of strings. 我需要将用户的输入存储到字符串数组中。

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

char *history[10] = {0};

int main (void) {

    char input[256];

    input = "input";

    strcpy(history[0], input);

    return (EXIT_SUCCESS);

}

Running it on the terminal I get a Segmentation Fault and in NetBeans I get main.c:11: error: incompatible types in assignment. 在终端上运行它我得到一个分段错误,在NetBeans中我得到main.c:11:错误:赋值中不兼容的类型。 I also tried to shift all the history to store the newest input into the first position (history[0]). 我还尝试将所有历史记录转移到将最新输入存储到第一个位置(历史[0])。

history[9] = history[8];
history[8] = history[7];
history[7] = history[6];
history[6] = history[5];
history[5] = history[4];
history[4] = history[3];
history[3] = history[2];
history[2] = history[1];
history[1] = history[0];
history[0] = input;

But this causes output like this. 但这导致这样的输出。

If input is "input" 如果输入是“输入”

History 0: input History 1: null etc. 历史0:输入历史1:null等

If then input is "new" 如果那么输入是“新的”

History 0: new History 1: new History 2: null etc. 历史0:新历史1:新历史2:null等

Each time a new input is entered the pointers to the string shift but it causes only the newest value to be saved in the history array. 每次输入新输入时,指向字符串移位的指针,但它只会将最新值保存在历史数组中。

You need to allocate space for the string. 您需要为字符串分配空间。 This can be done several ways, the two leading contenders look like: 这可以通过多种方式完成,两个主要竞争者看起来像:

char history[10][100];

and

char *history[10];
for (j = 0;  j < 10;  ++j)
    history [j] = malloc (100);

The first statically allocates ten character buffers at 100 characters each. 第一个静态分配10个字符缓冲区,每个缓冲区100个字符。 The second, as you wrote it, statically allocates ten pointers to characters. 第二个,就像你写的那样,静态地分配十个指向字符的指针。 By filling in the pointer with dynamically allocated memory (which could each be arbitrary lengths), there is memory to read the string later. 通过用动态分配的内存(每个都可以是任意长度)填充指针,稍后会有内存来读取字符串。

strcpy() does not allocate new memory area for the string, it only copies data from one buffer to another. strcpy()不为字符串分配新的内存区域,它只将数据从一个缓冲区复制到另一个缓冲区。 You need to allocate new buffers using strdup() or create the array pre-allocated ( char history[10][100]; ). 您需要使用strdup()分配新缓冲区或创建预分配的数组( char history[10][100]; )。 In this case, do not try to move pointers and use strcpy to copy the data. 在这种情况下,不要尝试移动指针并使用strcpy来复制数据。

main.c:11: error: incompatible types in assignment
(Code: input = "input";)

This happens because you try to make the array 'input' point to the string "input". 发生这种情况是因为您尝试使数组“input”指向字符串“input”。 This is not possible, since the array is a const pointer (ie the value it points to can not change). 这是不可能的,因为数组是一个const指针(即它指向的值不能改变)。

The correct way to do what you are trying is: 做你正在尝试的正确方法是:

strcpy(input,"input");

Of course this is a minor problem, the major problem has already been posted twice. 当然这是一个小问题,主要问题已经发布两次。 Just wanted to point it out. 只是想指出来。

Btw I don't know how you even compile this when running it on the terminal. 顺便说一句,我不知道你在终端上运行它时如何编译它。 Don't you get an error? 你不会收到错误吗? Maybe just a warning? 也许只是一个警告? Try compiling with -Wall -pedantic 尝试使用-Wall -pedantic进行编译

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

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