3
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>

int main()
{
    int i, *ptr; 
    ptr = func();
    for(i=0;i<20;i++)
    {
        printf("%d", ptr[i]);
    }
    return 0;
}

int * func()
{
    int *pointer;
    pointer = (int*)malloc(sizeof(int)*20);
    int i;
    for(i=0;i<20;i++)
    {
        pointer[i] = i+1; 
    }
    return pointer;
}

ERROR: Conflicting type of func. Warning: Assignment makes Pointer from integer without a cast [enabled by default]

Why am I getting this error?

1
  • you should also check the value malloc returns for NULL Commented Oct 22, 2013 at 15:26

1 Answer 1

6

Because you're calling func() without first declaring it. This causes the compiler to assume it's going to return int, but then you store that integer in a pointer which is of course rather suspicious.

Fix by moving func() to above main(), so the definition is seen before the call, or introduce a prototype before main():

int * func();

Also, functions taking no arguments should be (void) in C, and please don't cast the return value of malloc() in C.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.