[英]How to copy the data within the pointer into a float array using memcpy()?
I wanted to copy what's within the amount
pointer into the Fquantity[]
array, but it shows me error "argument of type "float" is incompatible with parameter of type "void*"".我想将
amount
指针中的内容复制到Fquantity[]
数组中,但它显示错误““float”类型的参数与“void*”类型的参数不兼容”。 How should I make it work?我应该如何使它工作? Thank you.
谢谢你。
char buffer[BSIZE];
char d[] = "\t\n"; //delimeter
char *fcode(0), *amount(0), *hos(0), *next;
char Fcode[BSIZE][100]; //store in array to do comparison
float Fquantity[BSIZE]; //store in array to do accumulation
float total;
int ctr = 0, x = 1;
fopen_s(&fptr, "Dist.txt", "r");
while (fgets(buffer, BSIZE, fptr) != NULL) //split string in FILE and store into Fcode and Fquantity array
{
fcode = strtok_s(buffer, d, &next);
printf("\ncode: %s\n", fcode);
memcpy(Fcode[ctr], fcode, 5);
amount = strtok_s(NULL, d, &next);
printf("amount: %.1f\n", atof(amount));
memcpy(Fquantity[ctr], amount, 5); //Fquantity[ctr] shows the error
hos = strtok_s(NULL, d, &next);
printf("hospital: %s\n", hos);
ctr++;
}
fclose(fptr);
To use memcpy
function, you have to give it the pointers of both buffers, But in memcpy(Fquantity[ctr], amount, 5)
you gave it Fquantity[ctr]
which is a value of a variable not address of it.要使用
memcpy
function,你必须给它两个缓冲区的指针,但是在memcpy(Fquantity[ctr], amount, 5)
你给它Fquantity[ctr]
这是一个变量的值而不是它的地址。 To give it the address of Fquantity[ctr]
just add and &
before its name.要为其提供
Fquantity[ctr]
的地址,只需在其名称前添加和&
。 So the answer is memcpy(&Fquantity[ctr], amount, 5)
.所以答案是
memcpy(&Fquantity[ctr], amount, 5)
。
memcpy
is not the right tool for your purpose: you must convert the textual representation of the numbers in the internal binary float
format. memcpy
不适合您的目的:您必须将数字的文本表示转换为内部二进制float
格式。 You can use strtod()
or scanf()
for this.为此,您可以使用
strtod()
或scanf()
。
Note that char *fcode(0), *amount(0), *hos(0)
makes no sense in C.注意
char *fcode(0), *amount(0), *hos(0)
在 C 中没有意义。
Your system probably coerces you to use the so called safer string functions such as fopen_s
and strtok_s
, but it is useless to call fopen_s
or strtok_s
and not test their return values.您的系统可能会强制您使用所谓的更安全的字符串函数,例如
fopen_s
和strtok_s
,但是调用fopen_s
或strtok_s
而不测试它们的返回值是没有用的。
Here is a modified version using standard functions.这是使用标准功能的修改版本。
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_data(void) {
char buffer[256];
char delimiters[] = "\t\n";
char Fcode[BSIZE][100]; //store in array to do comparison
float Fquantity[BSIZE]; //store in array to do accumulation
char hos[100];
int ctr = 0;
if ((fptr = fopen("Dist.txt", "r")) == NULL) {
fprintf(stderr, "cannot open Dist.txt: %s\n", strerror(errno));
return -1;
}
while (fgets(buffer, sizeof buffer, fptr) != NULL) {
//split string in FILE and store into Fcode and Fquantity
if (sscanf(buffer, "%99[^\t\n]%f%99[^\t\n]",
Fcode[ctr], &Fquantity[ctr], hos) == 3) {
printf("code: %s\n", Fcode[ctr]);
printf("amount: %.1f\n", Fquantity[ctr]);
printf("hospital: %s\n", hos);
ctr++;
} else {
printf("invalid entry: %s", buffer);
}
}
fclose(fptr);
return ctr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.