字符串数组C

时间:2020-08-12 19:39:27

标签: arrays c string struct malloc

我在函数中的两个printf上有问题,代码停在megainput[val1-1] = strdup(input);上,这是怎么回事? (val1的最小值为1)我没有编写readCommand函数,因为没有问题。最终程序是一个列表,其中每个node-> string都指向一个兆输入区域。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1025
char input[N];
char **megainput=NULL;
struct Node 
{
    char *string;
    struct Node* before; 
    struct Node* after; 
}*starter;
                                                            //creo il nodo di testa starter
void delete();
void change(int val1, int val2){
    int intervallo=val2-val1+1;
    struct Node* nodo = (struct Node*)malloc(sizeof(struct Node));
    fgets(input, 1025, stdin);
    megainput[val1-1] = strdup(input);   //error here
    printf("%s ",input);
    nodo->string = megainput[val1-1];
    printf("%s",nodo->string);
    
}
int main(int argc, char *argv[]) {
    starter = (struct Node *)malloc(sizeof(struct Node));
    starter->string=NULL;
    starter->before = NULL; 
    starter->after = NULL;
    char *command;                          //dubbio
    int *val1;
    int *val2;
    while (1)
    {
       readCommand(&command, &val1, &val2);
       change(val1, val2);
     }
    return 0;  
}

1 个答案:

答案 0 :(得分:1)

javac将字符串复制到strdup()中,但是您需要为指针数组input()分配内存。

megainput()