Strcpy () and string arrays

I need to save user input 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);

}

By running it on the terminal, I get a segmentation error, and in NetBeans I get main.c: 11: error: incompatible types when assigned. I also tried to transfer the whole story in order to keep the newest entry in the first position (history [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 leads to an exit like this.

If the input is "input"

History 0: input History 1: null et al.

If then the input is "new"

History 0: new History 1: new History 2: null et al.

Each time a new input is entered, there are pointers to a line break, but it only calls the newest value in the history array.

+3
source share
3

. , :

char history[10][100];

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

100 . , , . ( ), , .

+4

strcpy() , . strdup() (char history[10][100];). strcpy .

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

, "input" "input". , const ( , , ).

, :

strcpy(input,"input");

, , . .

Btw , . ? , ? -Wall -pedantic

0

Source: https://habr.com/ru/post/1768882/


All Articles