如何为C中的指针数组动态分配内存?

时间:2021-09-10 21:19:13

I need to allocate memory dynamically for an array of pointers.

我需要为指针数组动态分配内存。

Let us assume,

我们假设,

char *names[50];
char *element;

I used the following code to allocate memory dynamically which is producing error.

我使用以下代码动态分配内存,产生错误。

names=malloc(sizeof(char *));

Afterwards, i need to assign another character pointer to this one, say

然后,我需要为这个指定另一个字符指针,比方说

names=element;

I am getting error as ": warning: assignment from incompatible pointer type".

我收到错误为“:警告:从不兼容的指针类型分配”。

How can i resolve this?

我怎么解决这个问题?

3 个解决方案

#1


2  

names=malloc(sizeof(char *));

names = malloc(sizeof(char *));

Will allocate either 4 or 8 bytes (depending on your system). This doesn't make sense since your array was already sized at 50 entries in the declaration...

将分配4或8个字节(取决于您的系统)。这没有意义,因为您的数组已在声明中的50个条目中调整大小...

names=element;

名称=元件;

This is not how arrays are used in C. You have declared that there are 50 elements in "names" and each one is allocated as a different pointer to an array of characters. You need to decide which element in the array you want to assign. For eaxample:

这不是C中如何使用数组。您已声明“names”中有50个元素,并且每个元素都被分配为指向字符数组的不同指针。您需要确定要分配的数组中的哪个元素。例如:

char *test1 = "test string 1";
char *test2 = "test string 2";

names[0] = test1; // Assign pointer to string 1 to first element of array
names[1] = test2; // Assign pointer to string 2 to second element of array

#2


1  

If you want to dynamically allocate an array of N char *pointers, then you would use:

如果要动态分配N个char *指针的数组,那么您将使用:

char **names;

names = malloc(N * sizeof p[0]);

To assign the char * value element to the first element in the array, you would then use:

要将char * value元素分配给数组中的第一个元素,您将使用:

names[0] = element;

#3


1  

You may want to check this tutorial out: http://dystopiancode.blogspot.com/2011/10/dynamic-multidimensional-arrays-in-c.html It says how to allocate dynamic memory for arrays,matrices,cubes and hypercube and also how you can free it.

您可能需要查看本教程:http://dystopiancode.blogspot.com/2011/10/dynamic-multidimensional-arrays-in-c.html它说明如何为数组,矩阵,多维数据集和超立方体分配动态内存你也可以如何释放它。

#1


2  

names=malloc(sizeof(char *));

names = malloc(sizeof(char *));

Will allocate either 4 or 8 bytes (depending on your system). This doesn't make sense since your array was already sized at 50 entries in the declaration...

将分配4或8个字节(取决于您的系统)。这没有意义,因为您的数组已在声明中的50个条目中调整大小...

names=element;

名称=元件;

This is not how arrays are used in C. You have declared that there are 50 elements in "names" and each one is allocated as a different pointer to an array of characters. You need to decide which element in the array you want to assign. For eaxample:

这不是C中如何使用数组。您已声明“names”中有50个元素,并且每个元素都被分配为指向字符数组的不同指针。您需要确定要分配的数组中的哪个元素。例如:

char *test1 = "test string 1";
char *test2 = "test string 2";

names[0] = test1; // Assign pointer to string 1 to first element of array
names[1] = test2; // Assign pointer to string 2 to second element of array

#2


1  

If you want to dynamically allocate an array of N char *pointers, then you would use:

如果要动态分配N个char *指针的数组,那么您将使用:

char **names;

names = malloc(N * sizeof p[0]);

To assign the char * value element to the first element in the array, you would then use:

要将char * value元素分配给数组中的第一个元素,您将使用:

names[0] = element;

#3


1  

You may want to check this tutorial out: http://dystopiancode.blogspot.com/2011/10/dynamic-multidimensional-arrays-in-c.html It says how to allocate dynamic memory for arrays,matrices,cubes and hypercube and also how you can free it.

您可能需要查看本教程:http://dystopiancode.blogspot.com/2011/10/dynamic-multidimensional-arrays-in-c.html它说明如何为数组,矩阵,多维数据集和超立方体分配动态内存你也可以如何释放它。