0

My question is regarding the step of 'Validating the Key' in Caesar of Pset2 , while checking whether the key given by user consists of digits.

Here is my code :



#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main (int argc, string argv[])
{
    //Regarding command line argument 
  int x;
    if ( argc==2)
    {
        for(int i =0 ,n=strlen(argv);i<n;i++)
        {
            if(isdigit(argv[1][i]))
            {
                
            }
            //Convert the string to integer
            x = atoi(argv[1]);
            else
            {
            
                printf("Usage: %s key\n",argv[0]);
            }
        }
        printf("Success\n");
         //Print out key entered
        printf("%i\n",x);
    
    }
    else
    {
        printf("Usage: %s key\n",argv[0]);
        return 1;
    }
   
}

I am getting the following errors :

caesar.c:14:32: error: incompatible pointer types passing 'string *' (aka 'char **') to parameter of type 'const char *'; dereference with * [-Werror,-Wincompatible-pointer-types]
        for(int i =0 ,n=strlen(argv);i<n;i++)
                               ^~~~
                               *
/usr/include/string.h:385:35: note: passing argument to parameter '__s' here
extern size_t strlen (const char *__s)
                                  ^
caesar.c:22:13: error: expected expression
            else
            ^
2 errors generated.
make: *** [<builtin>: caesar] Error 1
0

You have two errors, so let's look at the cause of each: caesar.c:14:32: error: incompatible pointer types passing 'string *' (aka 'char**') to parameter of type 'const char *

This is caused because argv is a multidimensional array. argv[0] is the name of the program and argv[1] is the key. You cannot pass a multidimensional array to a function that is expecting a one-dimensional array (remember that a string is an array of characters).

caesar.c:22:13: error :expected expression

This is caused because you have a hanging else statement, which is an else that has something between it and the if statement that it belongs to. In your case, your two statements are separated by x = atoi(argv[1]);

Hope that helps. If so, feel free to accept the answer with the check mark beside it. But if not, please let me know and I will try to help further.

2
  • Close. argv[0] is the program name and argv[1] is the key. I'll let you edit your own answer.
    – Cliff B
    Mar 3 at 19:07
  • Yes, thanks. I forgot to convert to the 0-based indexes from the ordinal numbers my brain was using. Mar 3 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .