Thread: Invalid storage class for function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    18

    Invalid storage class for function

    Hey again,
    So this time I am trying to make a function to call twice instead of repeating the chunk of code twice. When I was not using the function the code worked fine so it seems like I set up the function wrong. Here is what I have when the 'invalid storage class' error occurs:
    Code:
    static void getAndPrint (char mainFile[], FILE *search);
    
    void getAndPrint (char mainFile[], FILE *search)
    {
        char data1[256];
        char data2[256];
        char line[256];
        char term[12] = "FindMe";
        char dest[] = "/home/user/Desktop/folder/";
        strcat (dest, mainFile);
        FILE *data;
        data = fopen (dest, "w");
    
        if (search != NULL)
        {
             while (fgets (line, sizeof(line), search)
             {
                   if ( strstr (line, term) != NULL)
                   {
                        fgets (data1 -2, sizeof(data1), search);
                        fprintf (data, "%s\n", data1);
                        fgets (data2 -2, sizeof(data2), search);
                        fprintf (data, "%s\n", data2);
                   }
             }
        }
      fclose(search);
    }
    Thank you for your time.
    Dominic

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,840
    On which line does your compiler report that error and what is the exact error message?

    My guess would be that the problem has something to do with you forward declaring getAndPrint to be static, but as I don't normally forward declare my static functions, I don't dare to say that that is the problem without some investigation of my own.
    Last edited by laserlight; 10-15-2012 at 10:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,316
    > fgets (data1 -2, sizeof(data1), search);
    What are you hoping to achieve with this out of bound pointer to your buffer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    From the example code, it would be on line 3 after I have made the prototype. The error is "invalid storage class for function 'getAndPrint' ". If I dont declare the protoype static I get errors: "colficting types for 'getAndPrint", "previous implicit declaration of 'getAndPrint' was here", "static declaration of 'getAndPrint' follows non-static declaratation".

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Do you just need to include the word "static" in front of your function definition (and not just in the prototype)?

    Other problems:

    Since dest is defined like this:
    char dest[] = "/home/user/Desktop/folder/";
    it does not have the storage to concatenate more chars into:
    strcat (dest, mainFile);
    Try this instead:
    char dest[100] = "/home/user/Desktop/folder/";
    and use strncat to prevent possible buffer overflow
    strncat (dest, mainFile, sizeof dest - strlen(dest) - 1);

    And in your fgets, why are you subtracting 2 from data1 and data2? That's undefined.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,840
    Also, you seem to be confused between the search and data FILE pointers.

    Quote Originally Posted by InicDominus
    From the example code, it would be on line 3 after I have made the prototype. The error is "invalid storage class for function 'getAndPrint' ". If I dont declare the protoype static I get errors: "colficting types for 'getAndPrint", "previous implicit declaration of 'getAndPrint' was here", "static declaration of 'getAndPrint' follows non-static declaratation".
    It might be good for you to reduce your program to the smallest and simplest program that you expect to compile but which demonstrates this problem, and post it. As it stands, it is possible to take your example code, fix an unrelated syntax error, then have a compilable (though incorrect) program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Salem, it gathers a whole line of data from a file after finding a keyword. I know it may be sloppy as I have never used C before but it works fine.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,840
    Quote Originally Posted by InicDominus
    I know it may be sloppy as I have never used C before but it works fine.
    It may appear to work fine, yet still be horribly wrong. One of the effects of undefined behaviour is that the code works perfectly until the moment that you really need it to work perfectly (e.g., when your program is being graded or when it is being demonstrated to the client).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,316
    > I know it may be sloppy as I have never used C before but it works fine.
    So said the blind man who ran across the road, and survived the first time he tried it.

    Yes, it is sloppy, and you'll get your ass handed to you on a plate at some point in the form of a segfault.
    Feel free to carry on as you are, until I tell you to add
    #include <I_told_you_so.h>

    Never make the mistake of assuming "works" == "bug free" when it comes to C programming. All sorts of crap can seem to "work" in the beginning, only to crash and burn later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Alright thank you for the advice. I will go fix that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storage class errors
    By suryak in forum C Programming
    Replies: 4
    Last Post: 10-16-2011, 02:42 AM
  2. storage class specifier
    By -EquinoX- in forum C Programming
    Replies: 5
    Last Post: 11-06-2008, 04:07 PM
  3. Dynamic storage class?
    By Xinok in forum C++ Programming
    Replies: 12
    Last Post: 12-20-2005, 01:14 PM
  4. storage class specified for parameter
    By mart_man00 in forum C Programming
    Replies: 2
    Last Post: 06-01-2003, 02:44 PM
  5. Storage Class
    By Blanket in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 09:54 PM

Tags for this Thread