gpt4 book ai didi

在c中复制矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:50 24 4
gpt4 key购买 nike

所以我得到了这个:char table1[10^length][length];char table2[10^length][length];

而且编译器不让我点这个:表1=表2;

我的代码应该接受一个带有通配符的字符串(只有?)

并在输出中给出所有可能的字符串(好吧,它是一个更大项目的一部分,但可以说在终端中打印它们——如果我这样做,我可以处理其余的)

完整代码如下:

int ** wildReplacement(char *wildAM,int length)
{
length=7;
char * temp;
strcpy(wildAM,"123?23");
//getchar();
int i=0;
int j=0;
int k=0;
int l=0;
int limit=0;
int wildCharsNum;
char *nlptr= NULL;

char table1[10^length][length];
char table2[10^length][length];


wildCharsNum=charOccurenciesInStr(wildAM,'?');
strcpy(temp,wildAM);
strcpy(table1[0],wildAM);

printf("\n!!WildChars In wildAM:%d",wildCharsNum);


while(k<wildCharsNum)
{
l=0;
while(l<=limit)
{
strcpy(temp,table1[l]);
i=0;
nlptr = strchr(temp, '?');//ka8e fora vriskei to epomeno ?
if (nlptr)
{
for(j=1;j<10;j++)
{
*nlptr = myItoc(j);//antikatastasi tou ? me digits sto temp
strcpy(table2[i],temp);
i++;
}
}
l++;
}
table1=table2;
limit=i;
k++;
}

printf("\nWild Numbers out:");
for(i=0;i<limit;i++)
{
printf("\n%s",table1[i]);
}

}

我应该 malloc 吗:

char ** table1
char ** table2

table1=malloc(sizeof(char)*10^length*lenght)
table2=malloc(sizeof(char)*10^length*lenght)

程序如何知道每条记录何时结束

那么这意味着什么:表 1[1] ?可能什么都没有...

提前致谢

最佳答案

当您malloc 一个二维数组时,您无法构造与编译器为数组声明所做的相同的东西。 char a[100][50] 是 5000 char 的连续内存块。当您下标时,编译器能够进行行/列数学运算,因为它在编译时知道大小。当您 malloc 一个等效的二维数组时,您必须提供一个行数组来索引行。这就是你如何回答你的问题“程序如何知道每条记录何时结束”:

char **b;
int i;
int rows = 100;
int cols = 50;
/* using sizeof(*b) is a good habit because there's only */
/* one place where you have to change type information */
b = malloc(rows * sizeof(*b));
/* now create a bunch of rows */
for (i = 0; i < rows; ++i) {
b[i] = malloc(cols * sizeof(**b));
}

请注意,由于 rows 索引数组,这比声明的数组占用更多内存。

作为优化,您可以避免循环中重复的 malloc,并从 rows * cols * sizeof(**b) 的单个大型分配中分配内存。

注意编写一个匹配函数来释放你的数组。

关于在c中复制矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022618/

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