0

I created a function which returns a pointer to an object of a self-made structure. Then, I declared another pointer which I set equal to a pointer returned by the the aforementioned function. I get the error "Assignment makes pointer from integer without a cast" - but I do not understand why I should be casting... because all of these pointers were declared to be of the same type.

In disk.h I defined the following:

struct generic_attribute{
    char *name;
    int current_value;
    int previous_value;
    //int time_series[500];
};

In disk.c, I made a constructor for generic_attribute like so:

#include "disk.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

struct generic_attribute* construct_generic_attribute(char* name, int current_value){
    struct generic_attribute *ga_ptr;
    //ga_ptr = (struct generic_attribute*) malloc (sizeof(struct generic_attribute));
    ga_ptr = malloc (sizeof (struct generic_attribute));
    ga_ptr -> name = name;
    ga_ptr -> current_value = current_value;
    ga_ptr -> previous_value = 0;
    return ga_ptr;
}

In disk_test.c I want to test this constructor:

#include "disk.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

void test_generic_attribute_constructor(char* name, int current_value){
    struct generic_attribute* ga_ptr;
    ga_ptr = construct_generic_attribute(name, current_value);
    printf("%i\n", construct_generic_attribute(name, current_value));
}

int main() {
    test_generic_attribute_constructor("test", 2000);
}

I get the error on this line:

ga_ptr = construct_generic_attribute(name, current_value);

I do not understand why. ga_pointer was declared as a pointer to type struct generic_attribute. So was the return of function construct_generic_attribute. I am new to C, so I might be misunderstanding how all of this works.

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
RebeccaK375
  • 871
  • 3
  • 17
  • 28
  • 3
    For one thing, you are using the wrong format specifier - [Correct format specifier to print pointer (address)?](http://stackoverflow.com/questions/9053658/correct-format-specifier-to-print-pointer-address) – crashmstr Jun 24 '15 at 17:21
  • You have not enabled strict enough warnings on your compiler, or you are ignoring them. – jxh Jun 24 '15 at 17:24
  • @DieterLücking Is not a specific fault of `msvc`. C11 and forerunners are backward compatible with very old versions of C. But ok, warnings will be welcome (and AFAIK gcc warns you by default). – ABu Jun 24 '15 at 17:46

3 Answers3

4

You did not declare construct_generic_attribute() in disk.h, so upon seeing the function call the compiler assumes it has the default signature -- that is, int construct_generic_attribute();.

Thanks for questioning this instead of blindly adding the cast (which could have seemed to work !)

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • @rakeb.void not at all, sorry ! – Quentin Jun 24 '15 at 17:27
  • Again, `disk_test.c`, not `disk_test.h` xD – ABu Jun 24 '15 at 17:28
  • Thank you for your answers. I tried this declaration in both: disk.h and disk.c and both worked. Is including it in disk.c a convention? or is there another reason for this choice? – RebeccaK375 Jun 24 '15 at 17:29
  • @Peregring-lk wait, what ? Now you confused me. The definition is in `disk.c`, so the prototype should be in `disk.h`. This answer is a trainwreck x) – Quentin Jun 24 '15 at 17:30
  • @user2707197 you probably mean `disk.h` and `disk_test.c` (`disk.c` already has the function signature where you defined it). As this function is to be used with your `generic_attribute` struct, it makes more sense to declare it along with the struct. On a technical level, there's no difference as long as the compiler encounters the signature before the function call. – Quentin Jun 24 '15 at 17:35
  • Ah ok hahahaha. I'm sorry. Both alternatives are possible: adding the function declaration again in `disk_test.c`, or directly in `disk.h` which is already included (the best alternative, portable and so on). And I thought initially you meant the first option, and so, there is the confusion. – ABu Jun 24 '15 at 17:41
  • @Peregring-lk this, and my lack of coffee (which is now fixed !) :p – Quentin Jun 24 '15 at 17:49
  • Ahhh, I see now! All cleared up thanks to your lively discussion. – RebeccaK375 Jun 24 '15 at 17:51
2

You are missing the function declaration in your header file, disk.h.

add the declaration: struct generic_attribute* construct_generic_attribute(char* ,int);

and the compiler will know the return type of your function is a pointer.

Caleb An
  • 366
  • 1
  • 10
1

add following line in disk_test.c :

struct generic_attribute* construct_generic_attribute(char* ,int);

Quentin
  • 62,093
  • 7
  • 131
  • 191
NightWatcher
  • 148
  • 10
  • 1
    @Quentin That is fine if the disk_test.c is in the same library (program). Not exposing internals, but getting maintainability issues. –  Jun 24 '15 at 17:28
  • @DieterLücking I completely failed here. Sorry for the mess ! – Quentin Jun 24 '15 at 17:31