Can't pass argv as parameter

This is a discussion on Can't pass argv as parameter within the C Programming forums, part of the General Programming Boards category; The code.The 3rd parameter won't pass from the compiler.Why? I have the function in other file but i do not ...

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,651

    Can't pass argv as parameter

    The code.The 3rd parameter won't pass from the compiler.Why?
    I have the function in other file but i do not think this is a problem,right?
    Code:
    int input(int n ,const int argc, const char **argv)
    {
         if(argc != 1 && strcmp(argv[1] , "-" ) ==0 ) {
              n = atoi(argv[2]);
              if( n <= 0 )/*atoi may return 0 for invalid input,or the input may be zero*/
              {
                  printf("Sorry , invalid input or input is non-positive\n");
                  printf("Input format is ./<executableName> - <n> .Notce that \"-\" and");
                  printf(" <n> must be seperated by a whitespace\n");
                  printf("Input will be set to ten.\n");
                  n = 10;
              }
         }
         return n;
    }
    
    
    
    int main(int argc, char *argv[])
    {
         int n = 10;
         n = input( n , argc , argv);
         printf("%d\n",n);
         return 0;
    }
    the error
    Code:
    Macintosh-c8bcc88e5669-9:hw6 usi$ gcc -Wall  tail.c line.c -o t
    tail.c: In function ‘main’:
    tail.c:10: warning: passing argument 3 of ‘input’ from incompatible pointer type
    Macintosh-c8bcc88e5669-9:hw6 usi$
    Also googled and saw this
    c - Can&#39;t pass argv to function - Stack Overflow
    but i think i do the same

    What i am missing here?

  2. #2
    Left
    Join Date
    Oct 2011
    Posts
    816
    Quote Originally Posted by std10093 View Post
    The code.The 3rd parameter won't pass from the compiler.Why?
    Because const char ** and char *[] are not the exact same type, the compiler complains. (It is not an error, it is just a warning, because the two types are compatible. So technically, the compiler does let it slide, it just warns about it.)

    The best solution is to change the type of the third parameter in your function declaration.

    If you do not want to, the two types are compatible, so you can typecast the argv variable to match the type your function expects for the third parameter.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,651
    Oh yes!
    Yes i want to keep the const! How exactly am i going to do this?

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    The types are not compatible: that's precisely why the compiler is issuing a diagnostic. The fact that it's a warning simply means the compiler decided to go ahead anyway (lots of compilers allow, with a warning, conversions between any pointer types, but of course that doesn't mean all pointer types are compatible).

    There is no implicit conversion from char** to const char** in C because that would allow you to circumvent the type system:
    Code:
    const char foo = '\0';
    char *p;
    const char **cp = &p; /* this is invalid */
    *cp = &foo;
    *p = 'x'; /* modifying foo */
    You're not doing the same thing as the stackoverflow question: in that code, an invalid main() declaration was used, because argv was tagged with const there. Your (valid) main() declaration does not have const.

    I'd probably just live without the const in this case.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,651
    Quote Originally Posted by cas View Post
    I'd probably just live without the const in this case.
    Point taken.Thank you both

  6. #6
    Left
    Join Date
    Oct 2011
    Posts
    816
    Quote Originally Posted by std10093 View Post
    Oh yes!
    Yes i want to keep the const! How exactly am i going to do this?
    Code:
    n = input(n, argc, (const char **)argv);

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,651
    Yes that did not produce no warning.So now the question is :
    use const and have an ugly cast like this?
    OR
    do not use const and pass the parameter elegantly?

  8. #8
    Left
    Join Date
    Oct 2011
    Posts
    816
    Quote Originally Posted by std10093 View Post
    Yes that did not produce no warning.So now the question is :
    use const and have an ugly cast like this?
    OR
    do not use const and pass the parameter elegantly?
    A const in function parameter declaration helps the compiler to produce more efficient code. It is not required, and you can always work around it within the function by typecasting it to non-const type. (In case of a pointer, that would not make the target writable, though; if it is say a constant string, in readonly memory, trying to modify the data will lead to a segmentation fault on most architectures.)

    So, the question is purely one of style.

    In this particular case, the third parameter is the array of command-line arguments, which is traditionally specified as char *argv[]. However, you are allowed to use any equivalent type, including char **argv. Because const tells the compiler you won't try to modify the variable/the data a pointer refers to, you can always add const in the mix, up to const char *const argv[] or const char *const *const argv if you want.

    So, my advice, based purely on readability and convention, is to use char *argv[] consistently in both main and input declarations, and no cast.

    If you choose differently, I personally would not notice while reading the source. So, it really is an irrelevant point of style, in my opinion. Others may disagree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass parameter to a thread
    By binks in forum Windows Programming
    Replies: 32
    Last Post: 12-28-2011, 04:58 PM
  2. Replies: 2
    Last Post: 11-27-2011, 06:40 PM
  3. How pass NULL for a parameter in a function?
    By 6tr6tr in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 01:29 PM
  4. how would i pass on a vector pointer parameter
    By bugmenot in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2005, 01:51 AM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 01:08 PM

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