简体   繁体   English

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

[英]How to allocate array of pointers for strings by malloc in C?

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.我需要将此 array_pointers_of_strings 复制到用于排序字符串的新数组。 I need only copy adress.我只需要复制地址。

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] ?

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:注意:通过在 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 正确性规则。

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

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变量来创建固定数量的它们:

message msg1, msg2, ...;

or you can allocate them dynamically:或者您可以动态分配它们:

message *pmsg1 = malloc(sizeof(message)), *pmsg2 = malloc(sizeof(message)), ...;
#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]);
    }
}

is it mandatory for you to use Malloc?您是否必须使用 Malloc? Because Calloc is the function in the C Standard Library which will make the job:因为 Calloc 是 C 标准库中的函数,它将完成这项工作:

"The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory" . “calloc() 函数为每个大小为字节的 nmemb 元素数组分配内存,并返回一个指向已分配内存的指针” (Source: here ) (来源:这里

I'm just creating a hash table and it has an array of pointers to nodes, and a easy way to do it is this:我只是创建一个哈希表,它有一个指向节点的指针数组,一个简单的方法是这样的:

hash_table_t *hash_table_create(unsigned long int size){
hash_table_t *ptr = NULL;

ptr = malloc(sizeof(hash_table_t) * 1);
if (ptr == NULL)
    return (NULL);

ptr->array = calloc(size, sizeof(hash_node_t *)); #HERE
if (ptr->array == NULL)
    return (NULL);
ptr->size = size;
return (ptr);}

Hope it works for you guys!希望它对你们有用!

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

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