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.