简体   繁体   中英

C programming: pass long integer to function and return a string

C programming: I need to pass a long integer (N) to a function and return a word (string) 'valid' or 'invalid', but i am struggling to corrrectly cast convert the values. Is the code not working because i need to use pointers? Can you please asvise how I should modify the code and why?

#include <stdio.h>
int digitmatch(long N); // function to match the first two digits of the card
int isvalid(long N);
int main()
{
   long N;
   N =    5212888888881881;
   printf("\n digit match: %d",digitmatch(N));
   printf("\n validity: %s",isvalid(N));
}  

int digitmatch(long N)
{
    long digit;
    digit = N;
    while (digit >= 100) 
    {
        digit /= 10;
    }
   return digit;
}

char isvalid(long N)
{  
   char valid[20];

    if (digitmatch(N) == 51 || digitmatch(N) == 52) 
    {
        valid = "MasterCard";
    }
    else
    {
        valid = "invalid";
    }
   return valid;
}

many thanks

In C language a string is by convention a null terminated char array. And a char is just a single character or byte. So you want isvalid to return not a char, but a string.

But arrays are not first class objects, and a function cannot return an array, nor can a programmer assign to an array. You can only return a pointer on the first element of an array or assign it to another pointer. But this leads to next problem, because automatic objects declared in a function reach their end of life on function return.

In your case, you could declare 2 static (const) arrays for the returned strings:

char* isvalid(long N);    // initial declaration must match the definition...
...

const char* isvalid(long N)
{
    static char master[] = "MasterCard";// static arrays won't reach end of life at return
    static char invalid[] = "invalid";
    const char *valid;

    if (digitmatch(N) == 51 || digitmatch(N) == 52) 
    {
        valid = master;
    }
    else
    {
        valid = invalid;
    }
   return valid;
}

const is used here to inform the caller that it should not change anything through the returned pointer.

In C, strings are of type const char* or char* , so your function should return that:

const char* isvalid(long N)
{  

    if (digitmatch(N) == 51 || digitmatch(N) == 52) 
    {
        return "MasterCard";
    }
    else
    {
        return "invalid";
    }
}

See This:

#include <stdio.h>

const char* fun();          //return pointer to string

int main()
{
    printf("%s",fun());
    return 0;
}

const char* fun()
{
    const char *ret = "Hello! World";  //This string is not placed in stack it is placed in .rodata, So it is not destroyed upon function return.
    return ret;
}

In your case:

const char* isvalid(long N)
{  
    if (digitmatch(N) == 51 || digitmatch(N) == 52) 
    {
        return "MasterCard";
    }
    else
    {
        return "invalid";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM