初级到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;
}
}
这是我一直关注的任务:
Sample code I followed for add function as given by university
有人能够纠正我的代码,为什么我得到冲突类型错误? 和get_dog函数一样,我会在函数中正确调用它并正确格式化realloc()函数吗?
谢谢
更新:在评论中插入新代码 - 更多错误
答案 0 :(得分:1)
作业表达式:
Locator.CurrentMutable.InitializeReactiveUI();
错误 - 左侧的类型为array->ptr[array->size-1] = get_dog();
,但右侧为dog*
。
BTW,调用get_dog()时不带参数...
答案 1 :(得分:1)
以下错误在提供的代码中:
以下是更正后的代码但可能在逻辑上不正确。因为这个计划的意图不太清楚。
#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;
}
}