初學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