Tiny Core Linux
Tiny Core Extensions => TCE Q&A Forum => Topic started by: ru60hz 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:
/* 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;
}
-
getline already defined in stdio.h at line 671 as error message says
Rename your getline to mygetline and retry.
-
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
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] extern _IO_ssize_t getline (char **__restrict __lineptr,
size_t *__restrict __n,
FILE *__restrict __stream) ;[
both are identical, except for " __wur " .
thanks
-
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.
-
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?
-
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
-
Hi ru60hz
I take it that this is the tutorial you are following:
http://www.crasseux.com/books/ctutorial/getline.html (http://www.crasseux.com/books/ctutorial/getline.html)
It contains an example of how to use getline.
-
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
-
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 (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
#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