-1

I am new in C and literally trying to return pointer from my function to the pointer variable and have this "[Warning] assignment makes pointer from integer without a cast" no idea why compiler defines it as an int. Can't declare my function before main as well, it throws this "undefined reference to `free_block'".

#include <stdio.h>
#include <stdlib.h>
    struct block{
    int num;
};

int main(int argc, char *argv[]) {
    struct block *b;
    b = free_block();

    struct block *free_block(){
        struct block *b = NULL;
        return b;
    }
    return 0;
}

Thank you

Yea, my fault I know not too much about c syntax and had no idea about nested functions, soz. But what could be wrong in this case: I am trying to make my own memory allocator without using malloc or calloc functions. In my code I have the same Warning on the line with pointer = free_space_get(size);, here I have no more nested func(), my methods defined before main(), but still have no idea do I have to declare my functions or no, coz in the answer given to me it worked fine as soon as functions were defined before the main().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct header{
        size_t size;
        struct header *next;
        unsigned int free;
    };
void *m_alloc(size_t  size){
        size_t total_size;
        void *block;
        struct header *pointer;
        if(!size)  
            return NULL;
        pointer = free_space_get(size);
        if(pointer){
            pointer->free = 0;
            return (void*)(pointer + 1);
        }

    }
struct header *get_free_space(size_t size){
        struct header *b = NULL;
        return b;
    }

int main() {
    return 0;
}
John John
  • 11
  • 2
  • 3
    Two issues: First of all functions must be declared before they are called. Secondly, C doesn't have nested functions. I recommend you get [a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Sep 30 '17 at 14:37
  • 2
    You need to define or declare the function before you call it. – Barmar Sep 30 '17 at 14:37
  • 1
    You should have also gotten a warning about an implicit declaration of the function. That should have given you a clue as to why it defines it as an int. – Barmar Sep 30 '17 at 14:38
  • 1
    "*Can't declare my function before main as well*" -- yes you can. In fact, this would be the only correct way. Apart from a function just returning `NULL` unconditionally saves no purpose at all, everything else was already said I guess. –  Sep 30 '17 at 14:42
  • 1
    You cannot define a function inside another function in C. – pjpj Sep 30 '17 at 14:48
  • How does this even get upvoted? This is not the place for "guided learning" of a language, but for practical programming problems and questions. For learning the language, there are enough books, tutorials etc. available ... –  Sep 30 '17 at 14:55
  • this part of the posted code: `struct block *b; b = free_block(); struct block *free_block(){ struct block *b = NULL; return b; }` can be replaced with a simple: `struct block *b = NULL;` – user3629249 Sep 30 '17 at 15:11

1 Answers1

0

Your code can be re-written as

#include <stdio.h>
#include <stdlib.h>

struct block{
    int num;
};

struct block *free_block(){
        struct block *b = NULL;
        return b;
}

int main(int argc, char *argv[]) {

    struct block *b;
    b = free_block();

    if(b == NULL) // Checking whether pointer is returned
        printf("\n Recieved NULL \n");

    return 0;
}
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108