【问题标题】:Array of strings C字符串数组 C
【发布时间】:2020-12-02 14:56:37
【问题描述】:

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

#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;  
}

【问题讨论】:

  • 你从来没有为megainput分配任何内存,你不能分配给megainput[val1-1]
  • @Barmar 我读到 strdup 也是这样,我错了吗?
  • 为字符串分配内存,而不是字符串指针数组。

标签: arrays c string struct malloc


【解决方案1】:

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

#define N 1025
char input[N];
char **megainput=NULL;
size_t megainput_max = -1;
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, N, stdin);
    if (val1-1 > megainput_max) { // Need to add more to megaainput
        char **temp = realloc(megainput, val1 * sizeof(*megainput));
        if (!temp) {
            printf("Memory allocation failure\n");
            exit(1);
        }
        megainput = temp;
        megainput_max = val1 - 1;
    }
    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;  
}

【讨论】:

  • 我是否还需要通过引用函数来传递 megainput_max 以便我能记住最大值?
  • 不,它是一个全局变量,就像megainput
  • 仍有问题。我需要在 realloc 之前做一个 malloc 吗?我读到只有在你已经使用过 malloc 的情况下才能使用 Realloc。
  • @AaronCettolin 如果指针是NULL,您也可以这样做。那么就相当于malloc()。所以第一次迭代会做初始分配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多