gpt4 book ai didi

c - 如何在 C 中扩展二维动态数组?

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:43 24 4
gpt4 key购买 nike

我有这个代码:

FILE *fr,*fr2,*fr3; 
fr = fopen("med.txt","r");
fr2 = fopen("moje.txt","w");
fr3 = fopen("zaloha.txt","rw");

int pRadku, nc, inword, pMezery;
int pSlov = 0;
inword = 0;
pRadku = 1;
inword = 0;
int radky = 20;
int sloupce = 50;
nc = pMezery = 0;
int pocitadlo = 0;
int pocitadlo2 = 0;
int i,j;
char c;

int tex = 255;

char text;
for(i= 0; i<tex ;i++){
text[i]='\0';
}

char **pole;
int pocet = 1000;
char *p_pom1, *p_pom2, **p_nove;
pole = (char **)malloc(pocet * sizeof(char));
for(i=0; i<pocet; i++){
pole[i] = (char*)malloc(tex * sizeof(char));
}





while(( c=fgetc(fr)) != EOF){


++nc;
if(c == '\n'){
++pRadku;
}
if(c ==' '){
pMezery++;
}
if(c == ' ' || c == '\n' || c == '\t'){
inword = 0;
}else if(inword == 0){
inword = 1;
++pSlov;
}



if(pocitadlo >= (pocet-1)){

int pomPocet = pocet;
pocet+=1000;
pole = (char **)realloc(pole, pocet * sizeof(char));

}



if((c != ' ')){
if(c != '\0'){
if(c != '\n'){
if(c != '\t'){
if(c != '.'){
if(c != ','){
text[pocitadlo2]=c;
pocitadlo2++;
}
}
}
}
}
}





if((c == ' ')){
text[pocitadlo2] = '\0';
for(i=0;i<tex;i++){
pole[pocitadlo][i] = text[i];
}
for(i=0;i<tex;i++){
text[i]='\0';
}
pocitadlo2=0;
pocitadlo++;

}else if(c == '\0'){
text[pocitadlo2] = '\0';
for(i=0;i<tex;i++){
pole[pocitadlo][i] = text[i];
}
for(i=0;i<tex;i++){
text[i]='\0';
}
pocitadlo2=0;
pocitadlo++;

}else if(c == '\n'){
text[pocitadlo2] = '\0';
for(i=0;i<tex;i++){
pole[pocitadlo][i] = text[i];
}
for(i=0;i<tex;i++){
text[i]='\0';
}
pocitadlo2=0;
pocitadlo++;

}else if(c == '.'){
text[pocitadlo2] = '\0';
for(i=0;i<tex;i++){
pole[pocitadlo][i] = text[i];
}
for(i=0;i<tex;i++){
text[i]='\0';
}
pocitadlo2=0;
pocitadlo++;

}else if(c == ','){
text[pocitadlo2] = '\0';
for(i=0;i<tex;i++){
pole[pocitadlo][i] = text[i];
}
for(i=0;i<tex;i++){
text[i]='\0';
}
pocitadlo2=0;
pocitadlo++;

}

}

我的问题是我是否正确扩展了我的二维数组?还是应该以其他方式进行?

这个程序从一个文件中读取 300 000 个单词并将它们保存到一个数组中。

感谢您的宝贵时间。

编辑**

int row = 5;
int column = 5;
int countr = 0;
int countr2 = 0;

int c;
int **array;

array = (int **)malloc(size * sizeof(int*));
for(i=0; i<row; i++){
pole[i] = (int*)malloc(column * sizeof(int*));
}

while(read letters form file (c=fgetc()) != EOF{

if(coutr>=(size-1)){
row+=10;
array = (int **)realloc(array, row * sizeof(int*));
}

array[countr][countr2] = c;
countr2++;
if(c == '\0' || c == ' '){
countr2=0;
countr++;
}
}

最佳答案

此代码已损坏,无法诊断出问题所在。例如,

int tex = 255;

char texttex
for(i= 0; tex ;i++){
text[i]='\0';
}

char texttex 之后缺少一个分号并且是一个无限循环,因为 tex 始终为 255。请提供完整的最小可编译代码。如果您希望别人为您花时间,您应该先为我们花一些时间。谢谢。

关于c - 如何在 C 中扩展二维动态数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14100468/

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