views:

105

answers:

4

Right now I am only trying to get my getline() function to work. I have the code from the book and it seems to be identical, but I cant get it to compile. This is homework but this part should be just copying from the book.

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

//error list
#define ENDOFFILE = -1; 
#define TOOMANYNUMS = -2;
#define LIMIT = 256;

//functions declared
int get_line(char line[], int);

//main
main(){
char line[255];
int num[6];
printf("Please input numbers %c: ", line);
get_line(line,LIMIT);


}

//functions 
  int get_line(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;
  ++i; 
}
s[i]='\0';
return i;
}

Now (edited at 10:22) I only get one error:

18 - expected expression before equal

+2  A: 

Right now I am only trying to get my getline() function to work.

getline() is a name of Linux function, declared in the stdio.h. C compiler complains that there are two conflicting declarations.

Simply give your getline() function a different name.


Edit1: That:

#define ENDOFFILE = -1;

Should be

#define ENDOFFILE -1

No =, no ; needed for preprocessor directives.

Dummy00001
wow, ok thanks.
pisfire
But there are still errors left, for example "too few arguments" should be clear, you're calling `getline()` but there should be two parameters.
schnaader
@schnaader - No, the errors are because the previous declaration (in stdio.h) has the definition `ssize_t getline (char **lineptr, size_t *n, FILE *stream)` - conflicting types in `ssize` vs. `int` and `char **` vs `char *`, and two few arguments: two vs. three. The getline() call is a separate issue.
reemrevnivek
`getline()` is not a standard function. Implementations are free to define it as they wish (and a few did, in a few different ways).
pmg
@reemrevnivek: OK, you're right, too :) This is main source why the error is there, but even after renaming the function, it would still be there :)
schnaader
edited code after these comments
pisfire
+2  A: 

conflicting types for 'getline'

getline might be a function in your standard library, e.g. thisone. If you want to reimplement it, give it a different name.

too few arguments to function 'getline'

You are calling getline() in main() without any arguments, but a few lines above you state that getline takes a char[] and an int. call it like getline(line,sizeof line);

nos
edited code after these comments
pisfire
+1  A: 

The problem appears to be that the system you are compiling this on appears to have a getline() function already defined, and your definition is conflicting with that. It appears that glibc, the C library used on Linux, has a non-standard getline() function declared in stdio.h. It shouldn't be defined unless you include a line like #define _GNU_SOURCE to opt-in to including non-standard functions, but it may be that this is pre-defined based on how you are compiling your code.

The easiest solution would be to rename your function to something else, but you could also try and find in your compiler options why GNU extensions are being turned on.

Now that you've edited your code, your second problem is that your #define lines are wrong. You don't need an equal or semicolon; these are processed by the preprocessor, which has a different syntax than C, and all you need to do is write #define NAME VALUE.

The proper syntax would be:

#define ENDOFFILE -1
#define TOOMANYNUMS -2
#define LIMIT 256
Brian Campbell
The default mode for gcc is gnu89. To change that use `-std=` command line option. ( http://tigcc.ticalc.org/doc/comopts.html#SEC6 )
pmg
edited code after these comments
pisfire
A: 

you need to realize:

  1. it is general an error to write #define macro like what you wrote,because #define macro is simply string replacement,so this statement get_line(line,LIMIT) is actually get_line(line,=256;) processed by the compiler,then the compiling error occurred. Just change #define LIMIT =256; to #define LIMIT 256 would be ok.
  2. as is mentioned in the previous replies,never write codes that library provided,getline is a function defined in stdio.h.

hope this helps.

Tracy