getline() problem

A

arnuld

I am learning malloc() and realloc() and I got the code from here:

http://www.eskimo.com/~scs/cclass/notes/sx11c.html


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


enum MAXSIZE { ARRSIZE = 1000 };


int main( void )
{
char line_of_chars[ARRSIZE];
int *ip;
int nallocs, nitems;

nallocs = 100;
ip = malloc(nallocs * sizeof( int ));

if( ip == NULL )
{
fprintf( stderr, "out of memory\n");
exit(EXIT_FAILURE);
}



nitems = 0;
while( getline(line_of_chars, ARRSIZE) != EOF)
{
if( nitems >= nallocs )
{
int *newp;
nallocs += 100;
newp = realloc( ip, nallocs * sizeof(int));

if( newp == NULL )
{
fprintf( stderr, "out of memory\n");
exit(EXIT_FAILURE);
}

ip = newp;
}

ip[nitems++] = atoi(line_of_chars);
}


return EXIT_SUCCESS;
}


I am keep on getting this warnings when I try to compile this function:


/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra eskimo.c
eskimo.c: In function `main':
eskimo.c:30: warning: implicit declaration of function `getline'
/home/arnuld/programs/C $



from here I see that getline() is defined in <stdio.h>:

http://crasseux.com/books/ctutorial/getline.html


then why I am getting the warning ?
 
W

WANG Cong

arnuld said:
I am learning malloc() and realloc() and I got the code from here:
...[SNIP].....

http://www.eskimo.com/~scs/cclass/notes/sx11c.html
from here I see that getline() is defined in <stdio.h>:


Oh.. no, searching the archives I found that "Ben Pfaff" says that
getline() is not a standard C function:

<snip>

True. getline() is a GNU extension. If you want to use it, you must
define the macro _GNU_SOURCE before including <stdio.h>.
 
F

Flash Gordon

WANG Cong wrote, On 21/04/08 07:21:
arnuld said:
I am learning malloc() and realloc() and I got the code from here:
...[SNIP].....
http://www.eskimo.com/~scs/cclass/notes/sx11c.html
from here I see that getline() is defined in <stdio.h>:

Oh.. no, searching the archives I found that "Ben Pfaff" says that
getline() is not a standard C function:

<snip>

True. getline() is a GNU extension. If you want to use it, you must
define the macro _GNU_SOURCE before including <stdio.h>.

This is unlikely to help arnuld as that is not the getline function he
is looking for. The one arnuld is looking for is in an earlier chapter
of the notes where the full source for it is provided.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top