简体   繁体   English

是否可以在 C 中创建动态指针数组?

[英]Is it possible to create a dynamic array of pointers in C?

Basically I need to save a certain number of strings received from stdin in a dynamic list, but the number of strings is different for each node of my list.基本上我需要将从stdin接收到的一定数量的字符串保存在一个动态列表中,但是我的列表的每个节点的字符串数量是不同的。 What I'm trying to do is to have a double pointer to char , so that I can have an array of strings (an array of pointers to char ).我想要做的是拥有一个指向char的双指针,这样我就可以拥有一个字符串数组(指向char的指针数组)。 But maybe it doesn't work this way.但也许它不会以这种方式工作。

Simplifying I have:简化我有:

typedef struct table {
    int rowsNumber;
    char **array;
    struct content * next;
} content;

table* node=NULL;


int main() {
    ...
    nodecreate();
    ..
}

Then I need to create the table, but here is where I'm getting errors.然后我需要创建表,但这里是我遇到错误的地方。

void nodecreate()
{
    char line[MAXLENGHT];
    scanf("%d", table->rowsNumber);
    for (i=0; i<rowsNumber, i++) {
        fgets(line, MAXLENGHT, stdin);
        node->array[i] = malloc(strlen(line) * sizeof(char));
        strcpy(node->array[i], line);
    }
}

These declarations这些声明

typedef struct table {
    int rowsNumber;
    char **array;
    struct content * next;
} content;

table* node=NULL;

are incorrect.不正确。

For example in this typedef declaration例如在这个 typedef 声明中

typedef struct table {
    int rowsNumber;
    char **array;
    struct content * next;
} content;

there are declared the type struct table its alias content and one more type struct content .声明了类型struct table的别名content和另外一种类型struct content The types struct table and struct content are two different types.类型struct tablestruct content是两种不同的类型。

For this declaration对于这个声明

table* node=NULL;

the compiler shall issue a message that the name table is not declared.编译器将发出一条消息,指出名称table未声明。

It seems you mean the following看来你的意思是以下

typedef struct table {
    int rowsNumber;
    char **array;
    struct table *next;
} content;

content *node = NULL;

In this function在这个函数中

void nodecreate()
{
char line[MAXLENGHT];
scanf("%d",table->rowsNumber);
for(i=0;i<rowsNumber,i++){
    fgets(line,MAXLENGHT,stdin);
    node->array[i]=malloc(strlen(line)*sizeof(char));
    strcpy(node->array[i],line);
}

where a closing brace is absent there is used the null pointer node.在没有右大括号的地方使用空指针节点。 So the function will at least invoke undefined behavior.因此该函数至少会调用未定义的行为。 The same problem exists with the data member node->array that has an indeterminate value.具有不确定值的数据成员node->array也存在同样的问题。

To use the function strcpy you need to reserve also a character for the terminating zero character like要使用函数strcpy您还需要为终止零字符保留一个字符,例如

    node->array[i]=malloc(strlen(line)*sizeof(char) + 1);

Also pay attention to that after this call of scanf还要注意在调用scanf

scanf("%d",table->rowsNumber);

the following first call of fgets will read an empty string (containing only the new line character '\\n' ) because the input buffer will contain the new line character '\\n' .以下fgets第一次调用将读取一个空字符串(仅包含换行符'\\n' ),因为输入缓冲区将包含换行符'\\n' You have to remove it.你必须删除它。

In any case the function does not make sense because the function as it is followed from its name nodecreate shall create a new node.在任何情况下,该函数都没有意义,因为从其名称nodecreate跟随的函数将创建一个新节点。 So this means as the function does not have parameters that the function shall return a created node that can be added to the list.所以这意味着由于函数没有参数,函数应该返回一个可以添加到列表中的创建节点。

malloc the array of pointers first.首先 malloc 指针数组。

//assuming you malloc'd the table somewhere before calling this...
void nodecreate()
{
char line[MAXLENGHT];
scanf("%d",table->rowsNumber);

node->array = malloc(sizeof(char*)*table->rowsNumber);
for(i=0;i<table->rowsNumber,i++){
    fgets(line,MAXLENGHT,stdin);
    node->array[i]=malloc(strlen(line)*sizeof(char));
    strcpy(node->array[i],line);
}

//don't forget to free both every char * and the char ** somewhere...

You cant malloc array[i] if you havent already allocated some memory to array如果您尚未为数组分配一些内存,则不能 malloc array[i]

You need to do 3 steps:你需要做3个步骤:

Step 1: Malloc the table node第一步:Malloc表节点

node = (content*)malloc(sizeof(table));
node->rowsNumber = 5; // change to whatever you want

Step2: Malloc the array of pointers: Step2:分配指针数组:

node->array = (char**)malloc(sizeof(char*)*(node->rowsNumber));

Step3: Malloc individual strings in array Step3:分配数组中的单个字符串

for(i=0;i<node->rowsNumber,i++){
    node->array[i]=malloc(sizeof(char) * 8);// Change 8 to whatever size you want
}

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

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