int get_rates(Date start_at, Date end_at,unsigned n_currencies, char *currencies[], Rate *result){
char link[1024];
char * simbolos=envirgular(currencies,n_currencies);
char * linkp=link;
snprintf(link, sizeof(link), "https://api.exchangeratesapi.io/history?start_at=%d-%d-%d&end_at=%d-%d-%d&symbols=%s"
,start_at.year,start_at.month,start_at.day,end_at.year,end_at.month,end_at.day,simbolos);
start_at.day++;
size_t datasize;
char *a=http_get_data(linkp,&datasize);
int numerodedias=getnumberofdays(a);
result=realloc(result,numerodedias*sizeof(Rate));
Date datas[numerodedias];
char * info=strstr(a,"rates");
char * data=malloc(10*sizeof(char));
char * year=malloc(4*sizeof(char));
char * day_month=malloc(2*sizeof(char));
char * value=malloc(6*sizeof(char));
for(int j=0;j<numerodedias;j++){
result[j].values=calloc(n_currencies,sizeof(char*));
result[j].names=calloc(n_currencies,sizeof(char*));
info=strstr(info,"-");
info=info-4;
strncpy(data,info,10);
strncpy(year,data,4);
datas[j].year=atoi(year);
data=data+5;
strncpy(day_month,data,2);
datas[j].month=atoi(day_month);
data=data+3;
strncpy(day_month,data,2);
datas[j].day=atoi(day_month);
for(int i=0;i<n_currencies;i++){
info=strstr(info,currencies[i]);
info=info+5;
strncpy(value,info,6);
result[j].values[i]=value;
result[j].names[i]=currencies[i];
}
for(int i=0;i<2;i++){
printf("\nValor%s\nMoeda%s\n",result[j].names[i],result[j].values[i]);}
result[j].data.day=datas[j].day;
result[j].data.month=datas[j].month;
result[j].data.year=datas[j].year;
//printf("\nDia:%d-%d-%d %s=%s %s=%s\n",result[j].data.day,result[j].data.month,result[j].data.year,result[j].names[0],result[j].values[0],result[j].names[1],result[j].values[1]);
}
return 0;
}
typedef struct Data{
int year;
int month;
int day;
}Date;
typedef struct SingleRate{
Date data;
char **names;
char **values;
}Rate;你好,堆栈溢出社区,我正在学习C语言在我的课堂上,我被要求做这个小项目,在一个网站上搜索使用libcurl的一些信息,某些货币和价值在确定的一天。我到了一个地步,我可以继续因为这个错误,我一直在互联网上寻找相同错误的答案,但我似乎没有找到它会很高兴,如果有人能向我解释什么我做错了谢谢。
发布于 2019-02-07 17:18:44
警告:
char * year=malloc(4*sizeof(char));
...
strncpy(year,data,4);
datas[j].year=atoi(year);年份没有结束空字符的位置,在这种情况下,atoi有一个未定义的行为。
同样适用于day_month
char * day_month=malloc(2*sizeof(char));
...
strncpy(day_month,data,2);
datas[j].month=atoi(day_month);其他可能出现的问题:
result[j].values[i]=value;您总是在循环中复制相同的char *,如果您假设您有不同的值--这是假的。由于通过strncpy设置了空字符,因此存在这样的风险:它没有结束空字符。也许你以后也会释放它(char * value=malloc(6*sizeof(char));)。因此,您可能必须将其放大,但必须使用缺失的空字符进行警告。
https://stackoverflow.com/questions/54578628
复制相似问题