初学C语言编程,学习作为大学工作Ç - 冲突的类型错误/ realloc的
的一部分的代码如下所述 - 我一直在recieving一个“冲突的类型”错误调用函数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()函数吗?
谢谢
UPDATE:插入新代码,采取了评论 - 更多的错误
您能否在错误所在的行上添加一些注释?请将完整且完整的实际错误复制粘贴到问题主体中?也请花一些时间[阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask)。 –
请勿发送垃圾邮件标签。 [编辑]你的问题,并删除与问题无关的标签。 – user694733
只是一个预感,但不要命名结构“狗”和类型“狗”以及。由于您可能使用typedef名称,因此请调用struct“dogStruct”。对dog_array同样的事情。另外,您可能打算将狗实例传递给get_dog? – Neil