#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#define NR 5
#define NC 6
#define NCC 30
void cc(char arr[][NC],int locations[][2],char ch,int *num);
int main(){
FILE *fPtr;
char linStr[NC+1];
char a[NR][NC];
int loc[NCC][2];
char ch;
int i,j,num;
printf("lab 4(b) solution by <NAME>\n");
if((fPtr = fopen ("/home/unersame/Desktop/data.dat","r")) == NULL ){
printf("file data.dat could not be opened\n");
}else{
/* file processing follows */
for(i=0;i<NR;i++){
//fscanf(fPtr, "%c\n",linStr);
for(j=0; j<NC; j++){
fscanf(fPtr, "%c",&linStr[j]);
j<NC?a[i][j]=linStr[j]:0;
}
}
printf("Character array is:\n");
for(i=0;i<NR;i++){
for(j=0; j<NC; j++){
printf("%c", a[i][j]);
}
}
puts("");
printf("Enter character to search for\n");
scanf("%c",&ch);
cc(a,loc,ch,&num);
printf("The character %c occurred %d times\n",ch,num);
printf("The (row,column) index pairs for the locations of\n");
printf("the character %c follow:\n", ch);
for(i=0;i<num;i++){
printf("(%d,%d)\n",i,j);
fclose(fPtr);
}
return 0;
}
void cc(char a[][NC], int locations[NCC][2], char ch, int *num){
int i,j,k;
k=0;
for(i=0; i<NR; i++){
for(j=0; j<NC; j++){
//scanf("%c", &a[i][j]);
if(a[i][j]==ch?locations=loc:0){
locations[k][0]=i;
locations[k][1]=j;
*num=k;
k++;
}
}
//*num=k;
}
//*num=k;
}
}
控制台输出:
unersame @ ubuntu:〜$ gcc -o 42b /home/unersame/Desktop/42b.c
/tmp/cctIyUei.o:在函数main':
42b.c:(.text+0x1fe): undefined reference to
cc'中
collect2:错误:ld返回1退出状态
如果我评论函数调用(cc(a,loc,ch,&amp; num);)它将编译。有什么建议?这是我的第一个编程课程,而且我已经在这个问题上打了很长时间。
带函数调用的输出已注释掉:
Character array is:
231456
s3fgtr
wer56t
1233gh
Enter character to search for
3
The character 3 occurred 32766 times
The (row,column) index pairs for the locations of
the character 3 follow:
(0,6)
(1,6)
*** Error in `./42b': double free or corruption (top): 0x0000000000bc2010 ***
Aborted (core dumped)
unersame@ubuntu:~$ gcc -o 42b /home/unersame/Desktop/42b.c -std=c99
/tmp/ccl8WvHf.o: In function `main':
42b.c:(.text+0x1f1): undefined reference to `cc'
collect2: error: ld returned 1 exit status
答案 0 :(得分:0)
你有额外的&#34 ;;&#34;在cc
函数定义中:
void cc(char a[][NC], int loc[NCC][2], char ch, int *num); {
int i,j,k,loc[NCC][2],num;
char ch, arr[NR][NC];
k0;
答案 1 :(得分:0)
你的函数声明之后你有一个分号:
void cc(char a[][NC], int loc[NCC][2], char ch, int *num); {
这导致它成为声明,而不是函数的定义。我相信删除分号应该会使链接器错误消失,但坦率地说这很难说;如果您保持代码格式正确并缩进,那么您将有更轻松的时间。