如何在C中通过malloc分配字符串的指针数组?

时间:2021-05-10 19:57:05

I have this struct in C Example:

我在C示例中有这个结构体:

typedef struct 
{
    const char * array_pointers_of_strings [ 30 ];
    // etc.
} message;

I need copy this array_pointers_of_strings to new array for sort strings. I need only copy adress.

我需要将array_pointers_of_string复制到用于排序字符串的新数组中。我只需要拷贝地址。

while ( i < 30 )
{
   new_array [i] = new_message->array_pointers_of_strings [i]; 
   // I need only copy adress of strings
}

My question is: How to allocate new_array [i] by malloc() for only adress of strings?

我的问题是:如何分配malloc()的new_array [i]只用于字符串的访问?

3 个解决方案

#1


11  

As I can understand from your assignment statement in while loop I think you need array of strings instead:

根据你在while循环中的赋值语句,我认为你需要字符串数组:

char** new_array;
new_array = malloc(30 * sizeof(char*)); // ignore casting malloc

Note: By doing = in while loop as below:

注:执行= in while循环如下:

new_array [i] = new_message->array_pointers_of_strings [i];

you are just assigning address of string (its not deep copy), but because you are also writing "only address of strings" so I think this is what you wants.

你只是在分配字符串的地址(不是深度拷贝),但是因为你也在写“字符串的唯一地址”所以我认为这是你想要的。

Edit: waring "assignment discards qualifiers from pointer target type"

编辑:“从指针目标类型中丢弃限定符”

you are getting this warning because you are assigning a const char* to char* that would violate the rules of const-correctness.

您将得到这个警告,因为您将const char*分配给char*,这会违反const-correct的规则。

You should declare your new_array like:

应该将new_array声明为:

const  char** new_array;      

or remove const in declaration of 'array_pointers_of_strings' from message stricture.

或者从消息结构中删除“array_pointers_of_strings”声明中的const。

#2


5  

This:

这样的:

char** p = malloc(30 * sizeof(char*));

will allocate a buffer big enough to hold 30 pointers to char (or string pointers, if you will) and assign to p its address.

将分配一个足够大的缓冲区来容纳30个指向char(如果您愿意,也可以是字符串指针)的指针,并分配给p它的地址。

p[0] is pointer 0, p[1] is pointer 1, ..., p[29] is pointer 29.

p[0]是指针0,p[1]是指针1,…p[29]是指针29。


Old answer...

旧的答案……

If I understand the question correctly, you can either create a fixed number of them by simply declaring variables of the type message:

如果我理解正确,您可以通过简单地声明类型消息的变量来创建一个固定的数量:

message msg1, msg2, ...;

or you can allocate them dynamically:

或者你可以动态分配它们:

message *pmsg1 = malloc(sizeof(message)), *pmsg2 = malloc(sizeof(message)), ...;

#3


3  

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

#define ARRAY_LEN 2
typedef struct
{
    char * string_array [ ARRAY_LEN ];
} message;

int main() {
    int i;
    message message;
    message.string_array[0] = "hello";
    message.string_array[1] = "world";
    for (i=0; i < ARRAY_LEN; ++i ) {
        printf("%d %s\n",i, message.string_array[i]);
    }

    char ** new_message = (char **)malloc(sizeof(char*) * ARRAY_LEN);
    for (i=0; i < ARRAY_LEN; ++i ) {
        new_message[i] = message.string_array[i];
    }
    for (i=0; i < ARRAY_LEN; ++i ) {
        printf("%d %s\n",i, new_message[i]);
    }
}

#1


11  

As I can understand from your assignment statement in while loop I think you need array of strings instead:

根据你在while循环中的赋值语句,我认为你需要字符串数组:

char** new_array;
new_array = malloc(30 * sizeof(char*)); // ignore casting malloc

Note: By doing = in while loop as below:

注:执行= in while循环如下:

new_array [i] = new_message->array_pointers_of_strings [i];

you are just assigning address of string (its not deep copy), but because you are also writing "only address of strings" so I think this is what you wants.

你只是在分配字符串的地址(不是深度拷贝),但是因为你也在写“字符串的唯一地址”所以我认为这是你想要的。

Edit: waring "assignment discards qualifiers from pointer target type"

编辑:“从指针目标类型中丢弃限定符”

you are getting this warning because you are assigning a const char* to char* that would violate the rules of const-correctness.

您将得到这个警告,因为您将const char*分配给char*,这会违反const-correct的规则。

You should declare your new_array like:

应该将new_array声明为:

const  char** new_array;      

or remove const in declaration of 'array_pointers_of_strings' from message stricture.

或者从消息结构中删除“array_pointers_of_strings”声明中的const。

#2


5  

This:

这样的:

char** p = malloc(30 * sizeof(char*));

will allocate a buffer big enough to hold 30 pointers to char (or string pointers, if you will) and assign to p its address.

将分配一个足够大的缓冲区来容纳30个指向char(如果您愿意,也可以是字符串指针)的指针,并分配给p它的地址。

p[0] is pointer 0, p[1] is pointer 1, ..., p[29] is pointer 29.

p[0]是指针0,p[1]是指针1,…p[29]是指针29。


Old answer...

旧的答案……

If I understand the question correctly, you can either create a fixed number of them by simply declaring variables of the type message:

如果我理解正确,您可以通过简单地声明类型消息的变量来创建一个固定的数量:

message msg1, msg2, ...;

or you can allocate them dynamically:

或者你可以动态分配它们:

message *pmsg1 = malloc(sizeof(message)), *pmsg2 = malloc(sizeof(message)), ...;

#3


3  

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

#define ARRAY_LEN 2
typedef struct
{
    char * string_array [ ARRAY_LEN ];
} message;

int main() {
    int i;
    message message;
    message.string_array[0] = "hello";
    message.string_array[1] = "world";
    for (i=0; i < ARRAY_LEN; ++i ) {
        printf("%d %s\n",i, message.string_array[i]);
    }

    char ** new_message = (char **)malloc(sizeof(char*) * ARRAY_LEN);
    for (i=0; i < ARRAY_LEN; ++i ) {
        new_message[i] = message.string_array[i];
    }
    for (i=0; i < ARRAY_LEN; ++i ) {
        printf("%d %s\n",i, new_message[i]);
    }
}