why can't i do this?

This is a discussion on why can't i do this? within the C Programming forums, part of the General Programming Boards category; I'm trying to make a brief header function for getline() (called "linein") so that I can go: Code: char *line; ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    why can't i do this?

    I'm trying to make a brief header function for getline() (called "linein") so that I can go:
    Code:
    char *line;
    line = linein();
    Only I would like to include the stream, so instead the second line could be:
    Code:
    line=linein("stdin");
    This is what the function I wrote looks like:
    Code:
    char *linein (char *stream) {
            int len=0;
            char *line;
            line = NULL;
            getline(&line,&len,&stream);
            return line;
    }
    This compiles fine and actually runs, but the line returned contains nothing (nor does it test as "== NULL").

    The problem is obviously to do with passing a "stream" argument to getline. Is there a proper way?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Read up on strings and files. You lack a general understanding how either of those two really work in C. It's complicated to put it all together, and to explain the misconceptions would require us to basically write the function out for you.

    Also, I'm not entirely sure where you're getting getline() from.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    If you're using linux, read this: http://linux.die.net/man/3/getline

    1. You would have to #define _GNU_SOURCE to make it work right, so use an alternative function.
    2. And your "char * stream" should probably be "FILE * stream".
    3. The last parameter of getline is being passed wrong too.
    4. declare "char * line;" as "char * line = NULL;"
    5. A header function?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Smile it works now

    Yes, I'm new to C and my question was definately about passing variable types. Sorry, I had forgotten getline() is GNU-specific.

    Anyway I did get it to work such that "char *line=linein(stdin);" returns a correct pointer. Two lines had to change in the "header function" (by which I meant a function in a header file, what are you suppose to call such a thing?):
    Code:
     31 char *linein (FILE *stream) {
     32         int len=0;
     33         char *line = NULL;
     34         getline(&line,&len,stream);
     35         return line;
     36 }
    Namely lines 31 and 34. This works with "disk" file streams too obviously, and saves setting an integer which makes me really lazy. Thanks, mingerso...
    Last edited by MK27; 08-02-2008 at 01:33 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Typically you would just have a prototype for the function in the header.
    See:
    http://en.wikipedia.org/wiki/Header_file
    and
    http://wiki.gamedev.net/index.php/Pr...ile_Convention

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I presume I will understand things differently later, but after reading the wikipedia thing on header files, I have to say it seems pointless to include prototypes in one file for definitions that appear in another.

    Perhaps I should call it an "include" file? After all, what I have is something which just contains a bunch of (personalized) regularly used functions (error checked malloc, chomp, etc.) so I can include them with "#include <mine.h>"...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    It's not pointless.
    When your program grows large, you split your functions into different files.
    Then the compiler needs to know about those functions when you call them, so to avoid code duplication, you put them in an "include file" (also known as a header) and include it so the compiler can see the function prototypes.
    Although calling functions without prototypes is valid in C, it's a very, very bad thing™. So don't do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    I presume I will understand things differently later, but after reading the wikipedia thing on header files, I have to say it seems pointless to include prototypes in one file for definitions that appear in another.

    Perhaps I should call it an "include" file? After all, what I have is something which just contains a bunch of (personalized) regularly used functions (error checked malloc, chomp, etc.) so I can include them with "#include <mine.h>"...
    So, in other words, you're making a library.

    That's actually the best example of the point of headers -- take the standard library and consider, oh, stdio.h. stdio.h is going to contain the prototype for printf, so that you can actually use the function. But stdio.h isn't going to contain the code for printf; depending on your system, source code for printf may be unavailable. But you've got a library that's compiled, so you don't need to have the source code for that; and you've got the header file, so you (and your compiler!) know how to use the functions in the library.

    (Plus, this system allows for any kind of meaningful standards across platforms; the libraries themselves are going to be radically different from architecture to architecture, but since the headers are (more-or-less) standardized, you won't have to change any of your code to recompile on a new architecture.)
    Last edited by tabstop; 08-03-2008 at 10:42 AM.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    The main problem you will probably run into is having multiple objects that use the header with the function and linking all of the objects into the main executable. You basically are trying to redefine the function everytime the header is included... and on linux gcc you'd get a message like this:
    "multiple definition of `bad_function'".... "first defined here"

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21