Tag: 数组

尝试将字符附加到字符数组

我正在尝试将字符“t”附加到值为“hello”的字符数组中,我正在确定数组大小,创建一个大于1个字符的新数组,分配新的字符和’\ 0’作为最后两个角色。 我不断打印旧值(你好)。 谢谢 #include #include void append(char * string,char ch) { int size; for (size=0;size<255;size++) { if (string[size]=='\0') break; } char temp[size+2]; strcpy(temp,string); temp[size+1]='t'; temp[size+2]='\0'; printf("the test string is: %s\n",temp); } int main() { char test[]="hello"; append(&test,'t'); return 0; }

使用指针知道数组的大小

如何使用malloc分配的指针知道数组的大小? #include int main(){ int *ptr = (int *)malloc(sizeof(int * 10)); printf(“Size:%d”,sizeof(ptr)); free(ptr_one); return 0; } 在这种情况下,我只得到指针的大小,即8.如何修改代码以获得40的数组大小。

如何从字符串数组C创建一个字符串

我正在学习arrays,并想知道是否有人可以帮助我。 我有一个字符串数组,需要创建一个新的字符串,它是所有数组元素的串联。 我遇到的问题是我只能打印我的数组中的第一个字符串,而不是所有字符串。 我知道我的数组中每个字符串的末尾都有一个null,那么我该如何解决这个问题呢? 也许2Darrays? 顺便说一下,我不允许使用string.h中的任何字符串操作函数。 谢谢。 #include #include int findLength(char array[]) { int i = 0; for (i = 0; array[i] != ‘\0’; i++) { } return i; } void arrayToString(char string[]) { int n = 0; int i = 0; int l = findLength(string); char *finalString; finalString = malloc(l * sizeof(char)); for (i […]

衰变的多维数组从函数返回

与(gcc)Multi-Dim Array或Double Pointer for Warning-free Compile相关 ,有没有办法从函数中返回所谓的“衰减数组指针”? 总结(假设2 dim数组)返回int (*a)[5]格式而不是int**格式? 据我所知,当返回int**指针被发送到另一个函数waiting (int*)[]参数时,它无法正常工作。

在C中填充来自字符串的2d数组

我想用一些数字填充二维数组。 数字采用网格格式,如下所示: 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 […]

如何将文本文件中的值分配给C中的数组结构?

我必须在C做选举。 共有7名候选人和365票。 我需要使用一组结构来做到这一点。 我需要从文本文件中读取候选人的每个名字和他们得到的票数。 最后我需要输出选举的胜利者。 以下是我的代码示例 #include #include #include struct candidates { char name[20]; int votes; }; int main() { //Counter int i = 0; int gVotes = 0; //Votes counter int v = 0; //Sploit Vote int spVote = 0; struct candidates electionCandidate[7]; FILE *fp; fp = fopen(“elections.txt”, “r”); for(i=0;i<7;i++) { char * aNames […]

无法将(int *)转换为int

我只是试图从包含所有数字的文本文件中读取元素,并将其作为参数传递给下面给出的“frequency()”函数。但是,它显示一个错误,说“int类型的参数是与类型(int *)的参数不兼容“。 我尝试了将(int *)转换为int的所有内容,但结果却很糟糕。发布的是我的C代码。 void main() { FILE*file = fopen(“num.txt”,”r”); int integers[100]; int i=0; int h[100]; int num; int theArray[100]; int n,k; int g; int x,l; while(fscanf(file,”%d”,&num)>0) { integers[i]=num; k =(int)integers[i]; printf(“%d\n”,k); i++; } printf (“\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? “); scanf(“\n%d”, &x);/*Stores Number […]

C:用于交换2D数组中的值的函数

我正在尝试编写一个函数来交换2D数组中的2个元素: void swap(int surface[][], int x1, int y1, int x2, int y2) { int temp = surface[x1][y1]; surface[x1][y1] = surface[x2][y2]; surface[x2][y2] = temp; } 但是当我尝试编译它(gcc)时,我收到此错误消息: Sim_Annealing.c: In function `swap’: Sim_Annealing.c:7: error: invalid use of array with unspecified bounds Sim_Annealing.c:8: error: invalid use of array with unspecified bounds Sim_Annealing.c:8: error: invalid use of array with unspecified […]

如何在不输入/空格字符的情况下扫描到2d char数组?

所以我必须制作一个迷宫程序,并开始我必须在迷宫中扫描到2darrays。 当我将chars放入数组时,当输入char和空格char总是占用数组中的第一个插槽时,会出现问题… 这是我的代码: int main(){ int row, col; int i,j,k,l; scanf(“%d”, &row); scanf(“%d”, &col); char** maze = (char**) calloc(row, sizeof(char*)); for ( k = 0; k < row; k++ ) { maze[k] = (char*) calloc(col, sizeof(char)); } for(i=0;i<row;i++){ for(j=0;j<col;j++){ scanf("%c",&maze[j][i]); } } for(k=0;k<row;k++){ for(l=0;l<col;l++){ printf("%c", maze[k][l]); } printf("\n"); } return 0; } 输出是: 使用enter char: […]

C编程; 生成具有随机数的二维矩阵,无需重复

我想通过在行和列中的元素中插入随机数而不重复来生成6×6矩阵。 到目前为止这是我的代码。 谢谢您的帮助! #include #include int main(void) { int array[6][6]; int rows, columns; int random; srand((unsigned)time(NULL)); for(rows=0;rows<6;rows++) { for(columns=0;columns<6;columns++) { random=rand()%36+1; array[rows][columns] = random; printf("%i",array[rows][columns]); } printf("\n"); } return 0; }