C语言 如何通过C中的malloc为字符串分配指针数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15686890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to allocate array of pointers for strings by malloc in C?
提问by user1779502
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_strings 复制到用于排序字符串的新数组。我只需要复制地址。
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] ?
回答by Grijesh Chauhan
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*可能违反常量,正确的规则。
You should declare your new_array like:
你应该像这样声明你的 new_array :
const char** new_array;
orremove constin declaration of 'array_pointers_of_strings' from message stricture.
或const从消息限制中删除'array_pointers_of_strings' 的声明。
回答by Alexey Frunze
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 pits 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)), ...;
回答by Hal Canary
#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]);
}
}
回答by RicarHincapie
is it mandatory for you to use Malloc? Because Calloc is the function in the C Standard Library which works for the job:
您是否必须使用 Malloc?因为 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". (Source: here)
“calloc() 函数为每个大小为字节的 nmemb 元素数组分配内存,并返回一个指向已分配内存的指针”。(来源:这里)
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!
希望它对你们有用!

