I have created a simple program. basically I created struct pointer in main and two functions. One function is simply allocating space for ads->title
which is char array. Assume at compile time I don't know the sizeof title, the second function is allocating memory for struct so this function can take any type of struct and not just single struct type. But when I compile the program with -Wall Wextra -Werror
I get plenty of errors like this
warning: assignment to ‘char’ from ‘void *’ makes integer from pointer without a cast [-Wint-conversion]
11 | *arr= malloc(sizeof(char)*n);
and many more.
All I am trying to do this with pointers and void pointers
#include <stdio.h>
#include <malloc.h>
struct ads{
int id;
char *title;
char *name;
};
void get_alloc_string(char *arr,int n)
{
*arr= malloc(sizeof(char)*n);
}
void get_alloc_single_struct(void **arr)
{
arr=malloc(sizeof(struct ads));
}
int main()
{
struct ads *data1;
//data1->id=102;
get_alloc_single_struct(data1);
get_alloc_string(data1->title,10);
data1->title="fawad khan";
data1->id=102;
printf("%s %d\n",data1->title,data1->id);
//get_alloc_string(data1->title);
return 0;
}