0

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;

}
halfer
  • 19,824
  • 17
  • 99
  • 186
user786
  • 3,902
  • 4
  • 40
  • 72
  • probably you should typecast the void ptr to char* – Abhilekh Gautam Sep 09 '21 at 06:01
  • 5
    @AbhilekhGautam No [*dont't* cast `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Sep 09 '21 at 06:02
  • 1
    Your `get_alloc_string` makes no sense. If you're trying to emulate pass-by-reference (instead of the more natural and simple `return`) you need to pass a pointer to the pointer using the pointer-to operator `&`. When you do `*arr = malloc(...)` you try to assign the pointer returned by `malloc` to `arr[0]` which makes no sense. – Some programmer dude Sep 09 '21 at 06:03
  • The warning means: "this isn't valid C, pointers must be assigned to pointers". See ["Pointer from integer/integer from pointer without a cast" issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues) and related: [What must a C compiler do when it finds an error?](https://software.codidact.com/posts/277340), [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Sep 09 '21 at 06:15
  • It seems like you are attempting some sort of polymorphism. In C, one must be vary specific with that sort of thing. What are you trying to do, exactly? – Neil Sep 09 '21 at 07:16
  • `sizeof(char)` is guaranteed to be `1` – tstanisl Sep 09 '21 at 07:31
  • @Neil u mean what the type of application. It will keep simple classifieds ad info data program I like to save info. It could be any data. Because I like this idea that I can use in number of other things I like to do – user786 Sep 09 '21 at 08:17
  • Key-Value string pairs like XML? Will you modify these strings, or just replace them? Is it going to be a map, or multimap? It would be more efficient if the entries were in one place in memory. – Neil Sep 09 '21 at 19:26

1 Answers1

3

Both allocation functions are wrong, but for different reasons:

void get_alloc_string(char *arr,int n)
{
    *arr= malloc(sizeof(char)*n);
}

That should be:

void get_alloc_string(char **arr,int n)
                        /* ^ extra indirection */
{
    *arr= malloc(sizeof(char)*n);
}

As you want to return the pointer.

void get_alloc_single_struct(void **arr)
{
    arr=malloc(sizeof(struct ads));
}

That should be:

void get_alloc_single_struct(void **arr)
{
    *arr=malloc(sizeof(struct ads));
 /* ^ extra indirection */
}

As you want again to return the pointer.

Personally, I'd change the function to something like this:

char *get_alloc_string(size_t n)
{
    return malloc(sizeof(char)*n);
}

And similar for get_alloc_single_struct().

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • What if I don't know the type of struct ads at compile time in get_alloc_single_struct function? – user786 Sep 09 '21 at 06:23