我可以在功能中创建动态数组并返回它吗?
我试图获取某些元素(在这种情况下,它超出了某些价格) 添加到另一个数组,然后在函数(sortPrice)的末尾返回此新数组。
之后,我想将此新数组分配给 main():“ product *sortedprice”中的刚创建的数组,
但我得到了此错误:
“ primererenizpit1.c:in function'main': PrimerEnizpit1.c:104:17:错误:分配给'产品 * {aka struct product *}'的类型不兼容的类型。 sortedprice = sortprice(products,50);“
我理解它的函数类型和数组的类型,但我确实需要帮助。
只是要清楚。我对这个某些错误有一个疑问,所有其他代码都只是为了信息预先感谢您。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Product
{
char name[30];
int code;
char date[11];
float price;
} product;
void addProduct(product *products, FILE *fp)
{
products = (product *)realloc(products, (3 + 1) * sizeof(product));
scanf("%s", products[3].name);
scanf("%d", &products[3].code);
scanf("%s", products[3].date);
scanf("%f", &products[3].price);
fp = fopen("in.bin", "wb");
if (fp == NULL)
{
printf("Error.\n");
exit(1);
}
if (fwrite(&products[3], sizeof(product), 1, fp) != 1)
{
printf("Error.\n");
exit(1);
}
fclose(fp);
}
product sortPrice(product *products, float setPrice)
{
product *newProducts;
int newSize = 1;
newProducts = (product *)calloc(newSize, sizeof(product));
int pos = 0;
for (int i = 0; i < 3; i++)
{
if (setPrice < products[i].price)
{
strcpy(newProducts[pos].name, products[i].name);
newProducts[pos].code = products[i].code;
strcpy(newProducts[pos].date, products[i].date);
newProducts[pos].price = products[i].price;
newSize++;
newProducts = (product *)realloc(newProducts, newSize * sizeof(product));
}
pos++;
}
return newProducts;
}
void saveToTxt(FILE *fp)
{
char savedName[30];
int savedCode;
char savedDate[11];
float savedPrice;
product savedProduct;
fp = fopen("in.bin", "rb");
fread(&savedProduct, sizeof(product), 1, fp);
fclose(fp);
printf("Binary Item:\n Name: %s\n ID: %d\n Expire Date: %s\n Price: %f\n", savedProduct.name, savedProduct.code, savedProduct.date, savedProduct.price);
// TXT FILE
fp = fopen("outTxt", "w");
fprintf(fp, "%s %d %s %f", savedProduct.name, savedProduct.code, savedProduct.date, savedProduct.price);
fclose(fp);
fp = fopen("outTxt", "r");
fscanf(fp, "%s %d %s %f", savedName, &savedCode, savedDate, &savedPrice);
fclose(fp);
printf("Text Item:\n Name: %s\n ID: %d\n Expire Date: %s\n Price: %f\n", savedName, savedCode, savedDate, savedPrice);
}
int main()
{
// Dynamic Array
product *products, *sortedPrice;
products = (product *)calloc(3, sizeof(product));
// File
FILE *f;
f = fopen("in.bin", "wb");
fclose(f);
// TXT FILE
FILE *fp;
fp = fopen("outTxt", "w");
fclose(fp);
addProduct(products, f);
sortedPrice = sortPrice(products, 50);
printf("%s - %d - %s - %f\n", sortedPrice->name, sortedPrice->code, sortedPrice->date, sortedPrice->price);
saveToTxt(fp);
return 0;
}
Im trying to get certain elements (in this case which are over some price) added to another array and then return this new array at the end of the function (sortPrice) .
After that I want to assign this new array to a freshly created one in main() : "product *sortedPrice"
But I get this error:
"primerenIzpit1.c: In function 'main':
primerenIzpit1.c:104:17: error: incompatible types when assigning to type 'product * {aka struct Product *}' from type 'product {aka struct Product}'
sortedPrice = sortPrice(products, 50);"
I understand its something with the types of the func and the array but I really need help.
Just to be clear. I have a question about this certain error all the other code is there just for information. Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Product
{
char name[30];
int code;
char date[11];
float price;
} product;
void addProduct(product *products, FILE *fp)
{
products = (product *)realloc(products, (3 + 1) * sizeof(product));
scanf("%s", products[3].name);
scanf("%d", &products[3].code);
scanf("%s", products[3].date);
scanf("%f", &products[3].price);
fp = fopen("in.bin", "wb");
if (fp == NULL)
{
printf("Error.\n");
exit(1);
}
if (fwrite(&products[3], sizeof(product), 1, fp) != 1)
{
printf("Error.\n");
exit(1);
}
fclose(fp);
}
product sortPrice(product *products, float setPrice)
{
product *newProducts;
int newSize = 1;
newProducts = (product *)calloc(newSize, sizeof(product));
int pos = 0;
for (int i = 0; i < 3; i++)
{
if (setPrice < products[i].price)
{
strcpy(newProducts[pos].name, products[i].name);
newProducts[pos].code = products[i].code;
strcpy(newProducts[pos].date, products[i].date);
newProducts[pos].price = products[i].price;
newSize++;
newProducts = (product *)realloc(newProducts, newSize * sizeof(product));
}
pos++;
}
return newProducts;
}
void saveToTxt(FILE *fp)
{
char savedName[30];
int savedCode;
char savedDate[11];
float savedPrice;
product savedProduct;
fp = fopen("in.bin", "rb");
fread(&savedProduct, sizeof(product), 1, fp);
fclose(fp);
printf("Binary Item:\n Name: %s\n ID: %d\n Expire Date: %s\n Price: %f\n", savedProduct.name, savedProduct.code, savedProduct.date, savedProduct.price);
// TXT FILE
fp = fopen("outTxt", "w");
fprintf(fp, "%s %d %s %f", savedProduct.name, savedProduct.code, savedProduct.date, savedProduct.price);
fclose(fp);
fp = fopen("outTxt", "r");
fscanf(fp, "%s %d %s %f", savedName, &savedCode, savedDate, &savedPrice);
fclose(fp);
printf("Text Item:\n Name: %s\n ID: %d\n Expire Date: %s\n Price: %f\n", savedName, savedCode, savedDate, savedPrice);
}
int main()
{
// Dynamic Array
product *products, *sortedPrice;
products = (product *)calloc(3, sizeof(product));
// File
FILE *f;
f = fopen("in.bin", "wb");
fclose(f);
// TXT FILE
FILE *fp;
fp = fopen("outTxt", "w");
fclose(fp);
addProduct(products, f);
sortedPrice = sortPrice(products, 50);
printf("%s - %d - %s - %f\n", sortedPrice->name, sortedPrice->code, sortedPrice->date, sortedPrice->price);
saveToTxt(fp);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论