Search code examples
cfunctionpointersfunction-declaration

How do I call function that has pointer before it


I am struggling with calling function that has pointer before it. We have school project to code our own malloc version. Problem is following:

I call function like this: char *ptrr = (char*)memory_alloc(10);

Function header is this: void *memory_alloc(unsigned int size)

And i get fault like this: Cast from pointer to integer of different size.

Thanks for any ideas.


Solution

  • Your compiler suspects that you are trying to cast a pointer to an integer.
    Usually, we get this warning when we try to write something like this:

    void myFunction (int i) 
    {
        // ....
    }
    
    int main()
    {
        void *p; // = NULL, malloc, calloc, ...
    
        myFunction ( (int) p);
    
        return 0;
    }
    

    I suggest you look in your code (ideally post it in your question), if you don't have a cast like this.