gpt4 book ai didi

c - 动态地将内存分配给二维字符串数组

转载 作者:行者123 更新时间:2023-11-30 15:43:41 27 4
gpt4 key购买 nike

int ReadNames(char ***Names, int *r,int *c)    
{
int i, j, k;
char name[100];
printf("Number of Rows: ");
scanf("%d", r);

printf("Number of Columns: ");
scanf("%d", c);

Names=(char ***)malloc(sizeof(char **)*(*r));
for(i=0;i<(*r);i++)
*(Names+i)=(char **)malloc(sizeof(char *)*(*c));

for(i=0;i<(*r);i++)
for(j=0;j<(*c);j++)
{
fflush(stdin);
gets(name);
strcpy(*(*(Names+i)+j),name);
}
return 1;
}

我正在尝试将内存分配给二维字符串数组。后来我想按行和按列对它们进行排序,但是在分配内存时,程序没有响应。我在代码中做了什么吗?

在主函数中 readname 被称为

    ReadNames(&p,&r,&c)

其中 r 和 c 是编号。行和列。

最佳答案

您需要:

*Names = (char **)malloc(sizeof(char **) * (*r));

以及随之而来的变化。

您传递一个三指针以便能够返回一个双指针。您所做的就是丢失有关存储双指针的位置的信息。

被删除的评论有一定道理;还有一个误区。二维字符串数组意味着基本数据中有三层指针。并且您需要第四级指针来传递到函数中。

此外,使用 gets() 会导致灾难。不要永远(如从不,如从不)使用gets( ) 函数。即使在玩具节目中也是如此。它会让你养成坏习惯。第一个互联网蠕虫通过使用 gets() 的程序传播(Google 搜索“morris internet worm”)。

在 Unix 和其他基于 POSIX 的系统上,使用 fflush(stdin) 会导致未定义的行为。在 Windows 上,该行为由 Mi​​crosoft 定义。如果您在 Windows 上运行,那就没问题;如果没有,你就不是。

<小时/>

我想Three-Star Programming很糟糕!

这可能不是我这样做的方式,但它是将您编写的内容直接转换为有效的内容,以及测试它的 main() 程序,释放所有分配的内存。它假设 strdup() 可用;如果没有,那么编写它就很简单了。

示例输出:

Number of Rows: 2
Number of Columns: 3
R0C0: Row 1, Column 1.
R0C1: Ambidextrous Armless Individual.
R0C2: Data for the third column of the first row.
R1C0: Row 2, Column 1.
R1C1: Row 2, Column 2.
R1C2: Given that the number of rows is 2 and the number of columns is 3, this should be the last input!
Rows = 2, cols = 3.
[0,0] = <<Row 1, Column 1.>>
[0,1] = <<Ambidextrous Armless Individual.>>
[0,2] = <<Data for the third column of the first row.>>
[1,0] = <<Row 2, Column 1.>>
[1,1] = <<Row 2, Column 2.>>
[1,2] = <<Given that the number of rows is 2 and the number of columns is 3, this should be the last input!>>

工作四星级代码:

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

static void ReadNames(char ****Names, int *rows, int *cols)
{
char name[100];
printf("Number of Rows: ");
scanf("%d", rows);

printf("Number of Columns: ");
scanf("%d", cols);

int c;
while ((c = getchar()) != EOF && c != '\n')
;

*Names = (char ***)malloc(sizeof(char ***)*(*rows));
for (int i = 0; i < (*rows); i++)
(*Names)[i] = (char **)malloc(sizeof(char **)*(*cols));

for (int i = 0; i < (*rows); i++)
{
for (int j = 0; j < (*cols); j++)
{
printf("R%dC%d: ", i, j);
if (fgets(name, sizeof(name), stdin) == 0)
{
fprintf(stderr, "Unexpected EOF\n");
exit(1);
}
name[strlen(name)-1] = '\0'; // Zap newline
(*Names)[i][j] = strdup(name);
}
}
}

int main(void)
{
int rows;
int cols;
char ***data = 0;

ReadNames(&data, &rows, &cols);
printf("Rows = %d, cols = %d.\n", rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("[%d,%d] = <<%s>>\n", i, j, data[i][j]);
}

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
free(data[i][j]);
free(data[i]);
}
free(data);
return 0;
}

替代三星级代码

使用三级指针已经够糟糕的了;四是可怕的。该代码将自身限制为三个级别的指针。我假设 C99 兼容,因此可以在函数中方便时声明变量。使用 C89/C90 编译器(现已倒退 14 年)的更改非常简单。

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

static char ***ReadNames(int *r, int *c)
{
int i, j;
char name[100];
printf("Number of Rows: ");
scanf("%d", r);

printf("Number of Columns: ");
scanf("%d", c);

int x;
while ((x = getchar()) != EOF && x != '\n')
;

char ***Names = (char ***)malloc(sizeof(char ***)*(*r));
for (i = 0; i < (*r); i++)
Names[i] = (char **)malloc(sizeof(char **)*(*c));
for (i = 0; i < (*r); i++)
{
for (j = 0; j < (*c); j++)
{
if (fgets(name, sizeof(name), stdin) == 0)
{
fprintf(stderr, "Unexpected EOF\n");
exit(1);
}
name[strlen(name)-1] = '\0';
Names[i][j] = strdup(name);
}
}
return Names;
}

static void PrintNames(char ***Names, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
printf("%s ", Names[i][j]);
printf("\n");
}
}

int main(void)
{
int rows;
int cols;
char ***data = ReadNames(&rows, &cols);

PrintNames(data, rows, cols);

printf("Rows = %d, cols = %d.\n", rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("[%d,%d] = <<%s>>\n", i, j, data[i][j]);
}

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
free(data[i][j]);
free(data[i]);
}
free(data);
return 0;
}

输出示例

Number of Rows: 3
Number of Columns: 4
R1C1
R1C2
R1C3
R1C4-EOR
R2C1
R2C2
R2C3
R2C4-EOR
R3C1
R3C2
R3C3
R3C4-EOR
R1C1 R1C2 R1C3 R1C4-EOR
R2C1 R2C2 R2C3 R2C4-EOR
R3C1 R3C2 R3C3 R3C4-EOR
Rows = 3, cols = 4.
[0,0] = <<R1C1>>
[0,1] = <<R1C2>>
[0,2] = <<R1C3>>
[0,3] = <<R1C4-EOR>>
[1,0] = <<R2C1>>
[1,1] = <<R2C2>>
[1,2] = <<R2C3>>
[1,3] = <<R2C4-EOR>>
[2,0] = <<R3C1>>
[2,1] = <<R3C2>>
[2,2] = <<R3C3>>
[2,3] = <<R3C4-EOR>>

两个程序都在 valgrind 下干净运行.

关于c - 动态地将内存分配给二维字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com