问题描述
- c语言 指针数组作为函数参数问题
-
#include<stdio.h> #include<stdlib.h> void init_rec(day_record *arr[],FILE *file) { char temp[80]; int index=0; while((fscanf(file,"%s",temp))!=EOF) { arr[index]=NULL; arr[index]=(day_record * )malloc(sizeof(day_record)); if(arr[index]==NULL) { printf("结构地址分配错误n"); exit(1); } replace(',',' ',temp); inputrc(arr[index],temp); index++; } }
在gcc中编译 总是提示错误发生在void init_rec(day_record arr[],FILE *file)这一行
*错误内容是 pay.c:6: 错误:expected ‘)’ before ‘*’ token **
结构体是这么写的
//每日记录临时存储结构 typedef struct day_record { int worker_nu; char date[DATE_LEN]; char * loca; int work_nu; char * worker[WORK_LEN]; } day_record;
调用是这样的
#include<stdio.h> #include<stdlib.h> #include"pay.h" #include"pay.c" int main() { FILE * fp; day_record * record[31]; fp = fopen("pay","r"); if(fp==NULL) { printf("open failedn"); exit(1); } init_rec(record,fp); putdr(record[0]); fclose(fp); return 0; }
想不明白是怎么回事 求指教 谢谢大家
解决方案
我刚用 VC 编译一下是可以过的,并且能够运行。重新 REBUILID 一下吧
解决方案二:
day_record怎么定义的,是不是有多余的分号
解决方案三:
这个应该是语法的问题,自己检查下。
解决方案四:
试试:
void init_rec(day_record (*arr)[],FILE *file)
解决方案五:
我给写了一份完整的:
/*
Record test data:
11,2006/1/27,M.,90001,Bobert
4,2012/3/4,S.,90301,White M.
2,2011/3/7,C.,3401,John Fanck
6,2003/4/17,W.,21401,Lyn W.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATE_LEN 12
#define LOCA_LEN 256
#define WORKER_LEN 512
#define MAX_LOOP 1024
#define MAX_LINE MAX_LOOP
typedef struct day_record
{
int worker_nu;
char date[DATE_LEN];
char loca[LOCA_LEN];
int work_nu;
char worker[WORKER_LEN];
} day_record;
void putdr(day_record *rcd[], int c){
int i;
printf("No.t Work No.t Datet Locat Workern");
for(i=0; i<c; i++){
day_record &r = *rcd[i];
printf("%dt %dt %st %st %sn", r.worker_nu, r.work_nu, r.date, r.loca, r.worker);
}
}
void replace(char s, char d, char c[]){
int i=0;
while(1){
if( c[i]==0 ) break;
c[i++]==s? c[i-1]=d:0;
if( i>MAX_LOOP ) break;
}
}
void inputrc(day_record *rcd, char c[], char delima){
int i=0,j=0,k[5],ld,ll,lw;
while(1){
if( c[i]=='' || c[i]=='n' || i>MAX_LOOP ){
// NULL terminate with gets()/fscanf, n with fgets()
c[i] = 0;
k[j] = i;
break;
}else{
c[i++]==delima? k[j++]=i,c[i-1]=0:0;
}
}
day_record &r = *rcd;
i = sscanf(c,"%d", &r.worker_nu);
i+= sscanf( (char*)&c[k[2]], "%d", &r.work_nu);
ld = k[1]-k[0];
ll = k[2]-k[1];
lw = k[4]-k[3];
if( !(i==2 && ld<DATE_LEN && ll<LOCA_LEN && lw<WORKER_LEN) ){
printf("Data error! line #%d. %sn", __LINE__, c);
exit(__LINE__);
}
strncpy( r.date, (char*)&c[k[0]], ld+1);
strncpy( r.loca, (char*)&c[k[1]], ll+1);
strncpy( r.worker, (char*)&c[k[3]], lw+1); //plus NULL terminator
//printf("%dt %dt %st %st %sn", r.worker_nu, r.work_nu, r.date, r.loca, r.worker);
//sscanf(c, "%d %s %s %d %s", &r.worker_nu, r.date, r.loca, &r.work_nu, r.worker);
}
int init_rec(day_record *rcd[],FILE *file)
{
char temp[MAX_LINE];
int index=0;
while( fgets(temp,MAX_LINE,file)!=NULL) // don't fscanf(file,"%s",temp)!=EOF
{
rcd[index]=NULL;
rcd[index]=(day_record * )malloc(sizeof(day_record));
if(rcd[index]==NULL)
{
printf("failure of memory allocation. line #%dn", __LINE__);
exit(1);
}
//replace(',',' ',temp);
inputrc(rcd[index],temp, ',');
index++;
}
return index;
}
int main()
{
FILE * fp;
day_record * record[31];
fp = fopen("pay.txt","r");
if(fp==NULL)
{
printf("file pay.txt open failedn");
exit(1);
}
int c = init_rec(record,fp);
putdr(record, c);
fclose(fp);
return 0;
}
输出:
a.exe
No. Work No. Date Loca Worker
11 90001 2006/1/27 M. Bobert B.
4 90301 2012/3/4 S. White M.
2 3401 2011/3/7 C. John Fanck
6 21401 2003/4/17 W. Lyn W.
解决方案六:
这问题一般就是语法问题,主要是代码逻辑没问题就不是大问题
时间: 2024-09-29 19:17:57