【发布时间】:2022-01-15 15:51:51
【问题描述】:
我正在用 c 编写一个程序,但在为指向 char 的指针数组分配内存时遇到了困难,我以后需要对这个数组进行排序。数组应该在其中存储字符,然后我想按字母顺序对这些字符进行排序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char collection[50];
} data_col;
int main(int argc, char const *argv[])
{
data_col * array [10] = malloc(sizeof(data_col));
for (size_t i = 0; i < 10; i++)
{
scanf("%c", &array[i]->group);
}
return 0;
}
【问题讨论】:
-
您的代码有很多错误,无法猜测您想要实现的目标。请描述你想要什么。至少提供可以编译的代码。 -- 至少描述一下为什么需要指针数组。
-
编辑问题
-
@Alessa 为什么你决定需要一个指针数组而不是结构类型的对象数组?
-
如果要对它进行排序,那么使用指针数组是有意义的;您可以通过交换两个元素的指针而不是交换 50 个字节的数据来交换两个元素。
标签: c memory-management malloc dynamic-memory-allocation