C character array error

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Posts: 1
Joined: Thu Apr 22, 2010 11:45 pm UTC

C character array error

Postby Archbounds » Thu Apr 22, 2010 11:56 pm UTC

I am learning C with "The C Programming Language, Second Edition" and am getting a strange error when trying to use a getline function. Here's the code:

Code: Select all

#include <stdio.h>

#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);


int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];

max = 0;
while ((len = getline(line, MAXLINE)) > 0)
   if (len > max) {
      max = len;
if (max > 0)
   printf("%s", longest);
return 0;

int getline(char s[], int lim)

int c, i;

for (i = 0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
   s[i] = c;
if (c == '\n') {
   s[i] = c;

void copy(char to[], char from[])

int i;

i = 0;
while ((to[i] = from[i]) != '\0')

It's word for word, and it's not compiling correctly. Here's the error:

chararray.c:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
chararray.c:30: error: conflicting types for ‘getline’
/usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
chararray.c: In function ‘copy’:
chararray.c:52: warning: ‘return’ with a value, in function returning void
chararray.c: In function ‘getline’:
chararray.c:53: error: expected declaration or statement at end of input

Is it just because my book is outdated, or is it a problem with my compiler? I'm just using gcc.

Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: C character array error

Postby fazzone » Fri Apr 23, 2010 12:10 am UTC

Well, as it happens, GNU gcc has some nonstandard I/O functions. Among them is getline. Their page about is http://www.gnu.org/s/libc/manual/html_node/Line-Input.html. So their version of getline is called the same thing, but takes different types of arguments, so the compiler isn't sure which you mean. The easiest way to fix this is to disable GNU extensions (I forget how to do this) or to just call your function something different.

User avatar
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: C character array error

Postby phlip » Fri Apr 23, 2010 12:29 am UTC

fazzone wrote:disable GNU extensions (I forget how to do this)

Pass the -ansi parameter to gcc. Among other things:
man gcc wrote:The macro "__STRICT_ANSI__" is predefined when the ...ansi option is used. Some header files may notice this macro and refrain from declaring certain functions or defining certain macros that the ISO standard doesn...t call for; this is to avoid interfering with any programs that might use these names for other things.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}

Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests