【问题标题】:C - Conflicting Types Error/ReallocC - 冲突类型错误/重新分配
【发布时间】:2017-05-29 06:47:58
【问题描述】:

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

对于下面提到的代码 - 我在调用函数 get_dog line 23line 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() 函数吗?

谢谢

更新:已插入新代码,采用 cmets - 更多错误

【问题讨论】:

  • 您能否在错误所在的行添加一些 cmets?请将实际错误完整完整地复制粘贴到问题正文中?也请花些时间read about how to ask good questions
  • 请不要发送垃圾标签。 Edit您的问题并删除与问题无关的标签。
  • 只是一种预感,但不要将结构命名为“dog”,也不要将类型命名为“dog”。由于您可能会使用 typedef 名称,因此将结构称为“dogStruct”。 dog_array 也是一样。另外,您可能打算将 dog 实例传递给 get_dog?
  • scanf("%s", &amp;r_dog.dog_name); 中删除&amp;,因为数组的名称已经转换为指向其第一个元素的指针。
  • 你想用add(dog_array *array);做什么?去掉它。并将case '1'case '2'等更改为case 1case 2等。

标签: c gcc memory-management


【解决方案1】:

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

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

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

#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;
    }
}

【讨论】:

    【解决方案2】:

    赋值表达式:

    array->ptr[array->size-1] = get_dog();
    

    错了——左边的类型是dog*,而右边的类型是dog

    顺便说一句,调用 get_dog() 时不带参数...

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 2016-02-07
      相关资源
      最近更新 更多