WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: compiletc error  (Read 4059 times)

Offline ru60hz

  • Newbie
  • *
  • Posts: 35
compiletc error
« on: November 09, 2011, 01:21:50 PM »
I was trying to mimic a sample program in K & R C book it's giving me
this error;

tc@box:~$ gcc longest-line3.c
longest-line3.c:7:5: error: conflicting types for 'getline'
/usr/include/stdio.h:671:20: note: previous declaration of 'getline' was here
longest-line3.c:31:5: error: conflicting types for 'getline'
/usr/include/stdio.h:671:20: note: previous declaration of 'getline' was here

i tried to read what it was saying in stdio.h but i could not understand
because i'm not yet a programmer. i'm only still scratching the skin of C.
Tried it on my old version of gcc (GCC 4.1.2) its working.
could somebody help? or explain?

here is the code:
Code: [Select]
/* a program that reads a set of text lines and prints the longest */

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line size */

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

/* print the longest input line */
int main()
{
    int len;         /* current line length */
    int  max;         /* maximum length seen so far */
    char line[MAXLINE];      /* current input line */
    char longest[MAXLINE];   /* longest line save here */

    max = 0;

    while ((len = getline(line, MAXLINE)) > 0)
   if (len > max) {
       max = len;
       copy(longest, line);
   }
    if (max > 0)   /* there was a line */
   printf("%s", longest);
     return 0;
}

/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
    int c, i;

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

/* copy: copy 'from' into 'to': assume to is big enough */
void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to = from) != '\0')
   ++i;
}


Code: (c) [Select]

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: compiletc error
« Reply #1 on: November 09, 2011, 01:35:14 PM »
getline already defined in stdio.h at line 671 as error message says

Rename your getline to mygetline and retry.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline ru60hz

  • Newbie
  • *
  • Posts: 35
Re: compiletc error
« Reply #2 on: November 10, 2011, 01:29:30 PM »
ha, ha thanks. its working now.

actually i did that (renaming) also before but there were so many errors
appeared after that due to some typo error. and when i finished
correcting those problems, i came to the point of testing it on
old version of gcc and it work from there.

by the way, could you please explain the meaning of
" __wur "  in stdio.h?

GCC 4.6.1
Code: [Select]
extern _IO_ssize_t getline (char **__restrict __lineptr,
                            size_t *__restrict __n,
                            FILE *__restrict __stream) __wur;

because, it is also included in GCC 4.1.2
Code: [Select]
[code] extern _IO_ssize_t getline (char **__restrict __lineptr,
                            size_t *__restrict __n,
                            FILE *__restrict __stream) ;[

both are identical, except for " __wur " .
thanks


Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11581
Re: compiletc error
« Reply #3 on: November 10, 2011, 02:31:59 PM »
Hi ru60hz
It's a macro. It stands for  warn unused result  and can be used by the compiler to generate
warnings if you forget to check the result of a function call.

Offline ru60hz

  • Newbie
  • *
  • Posts: 35
Re: compiletc error
« Reply #4 on: November 11, 2011, 01:10:57 PM »
thanks for the reply.
aside from renaming, is there any other way of compiling
the above code without error? isn't it just like the function
'printf or putchar' that we can re-use if we wanted to?

Offline ru60hz

  • Newbie
  • *
  • Posts: 35
Re: compiletc error
« Reply #5 on: November 11, 2011, 02:20:06 PM »
i'm sorry if i ask too much question regarding the above errors.
i understand that it is already on the scope of this forum because
at first i thought it was a extension problem.
i just could not help it to ask. sorry.

in the GNU C programming tutorial it states like this:
"getline

The getline function is the preferred method for reading lines of text from a stream,
 including standard input. The other standard functions, including gets, fgets, and
 scanf, are too unreliable. (Doubtless, in some programs you will see code that uses
 these unreliable functions, and at times you will come across compilers that cannot
 handle the safer getline function. As a professional, you should avoid unreliable
 functions and any compiler that requires you to be unsafe.)"

i am not a professional programmer, just for the sake of learning, i just wanted to know
how to actually use this function.

i would really appreciate your comments about this.
thanks


Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11581
Re: compiletc error
« Reply #6 on: November 11, 2011, 02:42:54 PM »
Hi ru60hz
I take it that this is the tutorial you are following:
http://www.crasseux.com/books/ctutorial/getline.html
It contains an example of how to use  getline.

Offline ru60hz

  • Newbie
  • *
  • Posts: 35
Re: compiletc error
« Reply #7 on: November 11, 2011, 09:43:25 PM »
yes, this is the link, i already tried the example
there but it is also giving the same error. not actually
the same but it is also direct me to stdio.h.

but now, i think i found the solution, its in the
manual. i will try it tonight.

thanks

Offline ru60hz

  • Newbie
  • *
  • Posts: 35
Re: compiletc error
« Reply #8 on: November 12, 2011, 01:43:50 PM »
wow, that was great. i learned a lot from that error but the best thing
is i learned how to read the manual.
Hi ru60hz
I take it that this is the tutorial you are following:
http://www.crasseux.com/books/ctutorial/getline.html
It contains an example of how to use  getline.

here is the code to make the program on the above link to compile without error
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main()
{
  char *my_string;
  size_t nbytes = 100;
  ssize_t bytes_read;

  puts ("Please enter a line of text.");

  /* These 2 lines are the heart of the program. */
  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getline (&my_string, &nbytes, stdin);

  if (bytes_read == -1)
    {
      puts ("ERROR!");
    }
  else
    {
      puts ("You typed:");
      puts (my_string);
    }
first is to include "stdlib.h" for malloc and other things that needs it
second is to change "int nbytes" to "size_t nbytes"
and "int bytes_read;" to "ssize_t bytes_read;"

although i have not made it on K & R C book sample 'getline' program yet,
i consider it solved!

thanks to all your replies
Ruel