Search code examples
chexcommand-line-argumentsc-stringsfunction-definition

Trouble understanding char* argv[] when passing it to a function, hex to decimal


I'm trying to make a program that converts hex to decimal, from user input in the console but I'm having trouble working with char and pointers.

int preberiVhod(char* cmd[]){
    char *hex = cmd;
    int size = strlen(cmd);
    //size_t len = sizeof(getCmd)/sizeof(*getCmd);

    long long decimal = 0, base = 1;
    int i = 0, value, length;
    int temp = 0;
    for(i = length--; i >= 0; i--)
    {
        if(hex[i] >= '0' && hex[i] <= '9')
        {
            decimal += (hex[i] - 48) * base;
            base *= 16;
        }
        else if(hex[i] >= 'A' && hex[i] <= 'F')
        {
            decimal += (hex[i] - 55) * base;
            base *= 16;
        }
        else if(hex[i] >= 'a' && hex[i] <= 'f')
        {
            decimal += (hex[i] - 87) * base;
            base *= 16;
        }
    }

return size
}
int main(int argc,char* argv[]) {


    int test = preberiVhod(argv);
    printf("%d", test);
    return 0;
}

I tried everything I could think off but I just cant seem to find the right answer on how to work with the char* argv[], passing it and then manipulating the values inside it.


Solution

  • The parameter argv represents an array of strings. Declared like

    char* argv[]
    

    it is implicitly converted to the type char **.

    But you need only one string (a user supplied command line parameter) of the array that you are going to process within the function preberiVhod.

    That is if you run your program like

    my_program 100
    

    then you need to process the element of the array argv[1] that has the type char *.

    So you should call the function preberiVhod at least like

    int test = preberiVhod( argv[1] );
    

    As the command line argument is not changed within the function then the function should be declared like

    int preberiVhod( const char *cmd );
    

    You are returning from the function the variable size

    return size
    

    (where you made a typo forgetting to append a semicolon) instead of the variable decimal (that moreover has the type long long instead of the return type int) that as it can be guessed represents the length of the passed string. Obviously this does not make a sense because you need to return a decimal value of a hex number stored in the passed string.

    Also it seems as you are expecting that the user will specify a non-negative hexadecimal number then the function should return an object of the type unsigned int.

    Here is a demonstrative program that shows how the function can be defined.

    #include <stdio.h>
    #include <ctype.h>
    
    unsigned int preberiVhod( const char *s )
    {
        const unsigned int Base = 16;
        unsigned int n = 0;
        
        for ( ; *s; ++s )
        {
            n *= Base;
            
            char c = toupper( ( unsigned char )*s );
            
            if ( '0' <= c && c <= '9' )
            {
                n += c - '0';
            }
            else if ( 'A' <= c && c <= 'F' )
            {
                n += c - 'A' + 10;
            }
        }
        
        return n;
    }
        
    int main(void) 
    {
        const char *s = "100";
        
        printf( "%u\n", preberiVhod( s ) );
        
        return 0;
    }
    

    The program output is

    256
    

    If the passed string contains invalid characters for a hexadecimal number representation and you decided just to skip them then the function can look as it is shown below. Also it is better to make the return type of the function as unsigned long long.

    #include <stdio.h>
    #include <ctype.h>
    
    unsigned long long int preberiVhod( const char *s )
    {
        const unsigned int Base = 16;
        unsigned int n = 0;
        
        for ( ; *s; ++s )
        {
            char c = toupper( ( unsigned char )*s );
            
            if ( '0' <= c && c <= '9' )
            {
                n *= Base;
                n += *s - '0';
            }
            else if ( 'A' <= c && c <= 'F' )
            {
                n *= Base;
                n += c - 'A' + 10;
            }
        }
        
        return n;
    }
        
    int main(void) 
    {
        const char *s = "100";
        
        printf( "%llu\n", preberiVhod( s ) );
        
        return 0;
    }