Like Tree2Likes
  • 2 Post By CommonTater

Compilation warning question

This is a discussion on Compilation warning question within the C Programming forums, part of the General Programming Boards category; Here's yet another noob question because... well.. I'm a noob at C programming. I have the following code: Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Compilation warning question

    Here's yet another noob question because... well.. I'm a noob at C programming.
    I have the following code:

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_PATH_LEN 128
    
    void readFile(char *name);
    char filename[MAX_PATH_LEN];
    
    int main(int argc, const char *argv[])
    {
       int argcount;
       for (argcount = 1; argcount < argc; argcount++) {
          if (strcmp(argv[argcount], "-f") == 0) {
             argcount++;
             if (argcount >= argc) {
                printf("ERROR! No filename specified.\n");
                exit(EXIT_FAILURE);
             }
             else {
                strcpy(filename, argv[argcount]);
             }
          }
       }
    
       printf("My filename: %s\n", filename);
       readFile(&filename);
       exit(EXIT_SUCCESS);
    }
    
    void readFile(char *name)
    {
       printf("Filename: %s\n", name);
    }
    If I compile this code it works but I get the following warning:
    mytest.c: In function ‘main’:
    mytest.c:28: warning: passing argument 1 of ‘readFile’ from incompatible pointer type


    If I removed the for loop and hard coded the argv[value] like this:
    Code:
    int main(int argc, const char *argv[])
    {
       strcpy(filename, argv[2]); // Assume it's called with -f and a filename following
       printf("My filename: %s\n", filename);
       readFile(&filename);
       exit(EXIT_SUCCESS);
    }
    I do not get a compilation warning.

    I assume the warning is generated because gcc doesn't see a value for argv[#] at compile time, but am I not coding this right? Is there a way I should be coding it to parse argv that wouldn't generate a warning at compile time? Or is my assumption of what the warning means completely wrong?

    Thanks in advance for any help!
    Last edited by codevyper; 04-25-2011 at 07:51 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,726
    filename is an array of char. When passed as an argument, it is converted to a pointer to char, which is what you want. But you passed &filename instead, which is a pointer to an array of MAX_PATH_LEN chars. Hence, just write readFile(filename).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... you should check argc (the number of parameters) before starting your loop...

    argc might be 0 and your loop would never catch it.

    The easy way, if you need 2 parameters is to check ...
    Code:
    if (argc < 2)
      { printf("Bad command line\n\n");
         exit (1); }

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Thanks guys. That was the trick. No more compile error. I may be a noob, but I'm still trying to learn how to write clean code that compiles with no errors.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codevyper View Post
    Thanks guys. That was the trick. No more compile error. I may be a noob, but I'm still trying to learn how to write clean code that compiles with no errors.
    Now comes one of the hardest lessons in programming .... "Compiles" does not mean "Works"....
    codevyper and Salem like this.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    To get past the noob level and to the advanced beginner level, you need to compile code without any warning (with warnings turned on).

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about a compiler warning
    By sashaKap in forum C Programming
    Replies: 4
    Last Post: 03-17-2009, 09:47 AM
  2. Quick Compilation Question
    By chacham15 in forum C Programming
    Replies: 10
    Last Post: 10-12-2008, 08:15 PM
  3. Quick Warning Question
    By Paul22000 in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 05:20 PM
  4. Simple C++ Compilation Question
    By mercury529 in forum C++ Programming
    Replies: 14
    Last Post: 07-30-2006, 09:40 PM
  5. Compiler warning question
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-05-2003, 04:39 AM

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