File reading function

This is a discussion on File reading function within the C Programming forums, part of the General Programming Boards category; Hey guys, firstly I would like to apologize for asking what is most likely a fairly simple question; I've just ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    File reading function

    Hey guys, firstly I would like to apologize for asking what is most likely a fairly simple question; I've just not yet come across the problem being address.

    I am looking to define a function which accepts, as the input, the argv array to process it and prints text, returning nothing.

    My problem has been 1. every time I define a function that returns void and call it with just: functionName(variables); the compiler complains about duplicate definitions of the function. Is there a precedent I am unaware of for function calls?

    Also, likely due to my basic understanding of pointers, I am unable to pass the argv array as input to the function, probably as I don't know how to represent it in the function definition.
    Likely related to the previous question, how would I create a pointer that points to the same location as argv[]?

    Thanks in advanced for any help, my programming experience is limited to higher level languages like Java, perl, python etc all which do not require as explicit control of variables.

  2. #2
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19
    The following is one way of doing what you want in plain C89/ISOC90
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* prototype */
    void my_func(char *[], int);
    
    int main(int argc, char *argv[]) {
        my_func(argv, argc);
    
        return EXIT_SUCCESS;
    }
    
    void my_func(char *list[], int n) {
        int i;
        for(i=1;i<n;i++)
            printf("%2d: %s\n", i, list[i]);
    }
    HTH

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3
    Thank you very much for the reply, I now see what I was doing wrong.
    I was attempting *char [] and not char *[], I might have to find a detailed write up on pointer referencing.

  4. #4
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19
    Code:
    void my_func(char *[], int);
    can be written
    Code:
    void my_func(char **, int);
    as well because of C pointers and arrays nature.

    Since you already know other languages, not to get bored by the same introductory stuff, if and only if you are learning C89/ISOC90, I suggest you the evergreen K&R as the true and only reference manual.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    Excellent, thank you very much for the tip; I'll try track down a copy of it today

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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