gpt4 book ai didi

C - 冲突类型错误/重新分配

转载 作者:行者123 更新时间:2023-11-30 18:48:54 25 4
gpt4 key购买 nike

C 编程初学者,学习作为大学工作的一部分

对于下面提到的代码 - 我在调用函数 get_dog 第 23 行第 32 行时收到了“冲突类型”错误

错误:

    task11-1.c:23:35: error: assigning to 'dog' (aka 'struct dog') from incompatible
task11-1.c:19:30: error: expected ';' after expression
new_array = &(*new_array)realloc(array->ptr,array->size*sizeof(new_array));
^
;
task11-1.c:23:37: warning: implicit declaration of function 'get_dog' is invalid
in C99 [-Wimplicit-function-declaration]
array->ptr[array->size-1] = get_dog(*new_array);
^
task11-1.c:23:35: error: assigning to 'struct dog' from incompatible type 'int'
array->ptr[array->size-1] = get_dog(*new_array);
^ ~~~~~~~~~~~~~~~~~~~
task11-1.c:19:30: warning: ignoring return value of function declared with
'warn_unused_result' attribute [-Wunused-result]
new_array = &(*new_array)realloc(array->ptr,array->size*sizeof(new_array));
^~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
task11-1.c:32:12: error: conflicting types for 'get_dog'
struct dog get_dog(struct dog_array *array){
^
task11-1.c:23:37: note: previous implicit declaration is here
array->ptr[array->size-1] = get_dog(*new_array);
^
task11-1.c:34:24: error: member reference base type 'int ()' is not a structure
or union
scanf("%s", get_dog.dog_name);
~~~~~~~^~~~~~~~~
task11-1.c:36:24: error: member reference base type 'int ()' is not a structure
or union
scanf("%d", get_dog.dog_id);
~~~~~~~^~~~~~~
task11-1.c:38:24: error: member reference base type 'int ()' is not a structure
or union
scanf("%d", get_dog.dog_age);
~~~~~~~^~~~~~~~
task11-1.c:40:12: error: returning 'int ()' from a function with incompatible
result type 'struct dog'
return get_dog;

代码:

#include <stdio.h>
#include <stdlib.h>

struct dog{
char dog_name[20];
int dog_id;
int dog_age;
};

struct dog_array{
int size;
struct dog *ptr;
};

void add(struct dog_array *array){ //dog_array *array was intended as a parameter as shown in sample code
int *ptr;
struct dog *new_array;
array -> size++;
new_array = &(*new_array)realloc(array->ptr,array->size*sizeof(new_array));
if (new_array)
{
array->ptr = new_array;
array->ptr[array->size-1] = get_dog(*new_array);
}
else
{
printf("Out of Memory! Cannot add dog details!\n");
array->size--;
}
}

struct dog get_dog(struct dog_array *array){
printf("Enter Employee Name: ");
scanf("%s", get_dog.dog_name);
printf("Enter ID: ");
scanf("%d", get_dog.dog_id);
printf("Enter Salary: ");
scanf("%d", get_dog.dog_age);

return get_dog;
}

int main(){
int input;
struct dog_array array={0, NULL};

printf("Enter in an option:\n");
printf("1. Add to Array\n");
printf("2. Print all Array\n");
printf("3. Exit Program\n");
scanf("%d",&input);

switch(input){
case 1:
printf("You have selected option 1\n");
add(&array);
break;
case 2:
printf("You have selected option 2\n");
//print_data(dog);
break;
case 3:
printf("You selected to exit, exiting...\n");
return 0;
}
}

这是我一直在执行的任务:

Task Requirements

Sample code I followed for add function as given by university

有人能够纠正我的代码,为什么我会收到冲突类型错误吗?还有 get_dog 函数,我是否可以在函数中正确调用它并正确格式化 realloc() 函数?

谢谢

更新:插入了新代码,在评论中采用 - 更多错误

最佳答案

提供的代码中有以下错误:

  • 函数“structdog get_dog(structdog_array *array)”应在使用前声明。
  • 对于类型转换和 sizeof 运算符,请使用类型而不是变量。
  • 函数“structdog get_dog(structdog_array *array)”中未声明“get_dog”变量

以下是更正的代码,但可能在逻辑上不正确。因为这个程序的意图不太明确。

#include <stdio.h>
#include <stdlib.h>

struct dog{
char dog_name[20];
int dog_id;
int dog_age;
};

struct dog_array{
int size;
struct dog *ptr;
};

struct dog get_dog(struct dog *array);

void add(struct dog_array *array){ //dog_array *array was intended as a parameter as shown in sample code
//int *ptr;
struct dog *new_array;
array -> size++;
new_array = (struct dog*)realloc(array->ptr,array->size*sizeof(struct dog));
if (new_array)
{
array->ptr = new_array;
array->ptr[array->size-1] = get_dog(new_array);
}
else
{
printf("Out of Memory! Cannot add dog details!\n");
array->size--;
}
}

struct dog get_dog(struct dog *array){
struct dog get_dog= *array;
printf("Enter Employee Name: ");
scanf("%s", get_dog.dog_name);
printf("Enter ID: ");
scanf("%d", get_dog.dog_id);
printf("Enter Salary: ");
scanf("%d", get_dog.dog_age);

return get_dog;
}

int main(){
int input;
struct dog_array array={0, NULL};

printf("Enter in an option:\n");
printf("1. Add to Array\n");
printf("2. Print all Array\n");
printf("3. Exit Program\n");
scanf("%d",&input);

switch(input){
case 1:
printf("You have selected option 1\n");
add(&array);
break;
case 2:
printf("You have selected option 2\n");
//print_data(dog);
break;
case 3:
printf("You selected to exit, exiting...\n");
return 0;
}
}

关于C - 冲突类型错误/重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44236143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com