0

why the error

#include <stdio.h>

int main(void)
{
    int *p, size, i;
    FILE *fp;

    fp = fopen("input.txt","r");
    fscanf(fp, "%d", &size);

    p = (int*)malloc(size*sizeof(int));  //error
    for (i = 0; i <size; i++)
        fscanf(fp, "%d", &p[i]);

    for (i = size-1; i>= 0; i--)
        printf("%d\n", p[i]);

    free(p);
    fclose(fp);
    return 0;
}

i'm using "Geany" on ubuntu

and on Geany compiler :

fileName.c:11:2: warning implicit declaration of function 'malloc' [-Wimplicit-function-declatation] fileName.c:11:12: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default] fileName.c:18:12: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration] fileName.c:18:12: warning: incompatible implicit declaration of built-in function 'free' [enabled-by default] compilation finished successfully

Alamin
  • 65
  • 1
  • 5
  • Stop casting the return value of malloc. If you stop doing that this would be an error as it should be instead of a warning. You do not need to (nor should you) cast to or from a `void*` in C. The conversion is safe and implicit. – Ed S. Mar 26 '13 at 21:54
  • 1
    @Mahesh: They should be errors and, in C99, they would be errors. – Ed S. Mar 26 '13 at 21:55
  • @EdS. By the time I realized and deleted, you commented :) – Mahesh Mar 26 '13 at 21:56
  • @EdS. - gcc/clang will still only warn in C99 mode for implicit declarations, unless you supply `-pedantic-errors` – teppic Mar 26 '13 at 22:02
  • Thanks you all, how can i compiling from the terminal on Ubuntu – Alamin Mar 26 '13 at 22:19
  • @Alamin - `gcc -o foo foo.c -Wall` – teppic Mar 26 '13 at 22:22
  • @teppic Thanks, but what `c99 -W 64 proble1.c -o problem_1` from [link](http://http://stackoverflow.com/questions/2193634/setting-std-c99-flag-in-gcc) – Alamin Mar 27 '13 at 01:18
  • @Alamin, using c99 instead of gcc is fine – teppic Mar 27 '13 at 01:24

2 Answers2

6

You're missing the following header include:

#include <stdlib.h>

The prototypes for malloc and free are defined in the stdlib.h header file which you missed out.

If you're unsure which header files to include for some standard C functions, you could always use man to figure it out.

For this case man malloc would have shown the required header file to be included.

BTW, in your code you're not checking if fp is NULL after fopen.

fopen can and will fail if the file does not exist or you do not have permissions to open the file (for reading in your case).

fp = fopen("input.txt","r");
if (fp == NULL)
{
    printf("Error opening input.txt\n");
    return -1;
}
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • but in Terminal it say:Segmentation fault (core dumped) ------------------ (program exited with code: 139) Press return to continue – Alamin Mar 26 '13 at 22:00
  • is that because i'm working on ubuntu – Alamin Mar 26 '13 at 22:03
  • @Alamin - I've updated the answer with a probable reason for the segmentation fault. You should be checking the value of `fp` after `fopen` to make sure it is not `NULL`. Otherwise fscanf tries to read from a `NULL` file pointer. – Tuxdude Mar 26 '13 at 22:14
  • Thanks alot for your informative reply – Alamin Mar 26 '13 at 22:24
0

This:

warning implicit declaration of function 'xxxxxx' [-Wimplicit-function-declatation] 

Always means the same thing. The compiler can't find the function you're talking about. Most of the time that's because you forgot the correct header file.

If you type man <function name>, in this case man malloc into your terminal (or google) you'll get a page telling you what header files you need. In this case <stdlib.h>, include it and the warning will go away.

Mike
  • 47,263
  • 29
  • 113
  • 177