0

I'm still trying to wrap my head around pointers and strings in C for a class I'm taking. In the below example, g_reservations[][] is a global variable (not ideal, I know, but I cant change that).

error: Warning C4047 'function': 'const char *' differs in levels of indirection from 'char' EconoAirBeta ... 299

*passenger is a pointer, so do create a pointer to the global? That seems unnecessary...

how do i make this work? I feel I'm missing some incredibly easy concept my brain cant seem to grasp....

unsigned int FindSeatWithPassenger(const char *passengerName)
{
    unsigned int seat = 0;
    for (seat = 0; seat < NUM_SEATS; ++seat)
    {
        if ( strncmp(passengerName, g_reservations[seat][0], NAME_LENGTH) != 0) //error here with global variable
        {
            return seat;
            break;
        }
    }
    return '\0';
}

Global declaration:

#define NAME_LENGTH 10
#define NAME_BUFFER_LENGTH ( NAME_LENGTH + 1 )
char g_reservations[NUM_SEATS][NAME_BUFFER_LENGTH];
7
  • Coyld you please show how g_reservations[seat][0] matrix is declared ? Is it char** ? Commented Nov 9, 2019 at 16:30
  • edited above to include global declaration... Commented Nov 9, 2019 at 16:33
  • 3
    Use g_reservations[seat] in strncmp. The second index denotes the characters of the string you want to compare. Commented Nov 9, 2019 at 16:39
  • Note: stncmp returns 0 when equal! Commented Nov 9, 2019 at 16:42
  • 1
    Note: at the end you are formally returning a char, whereas the function should return an usigned int. At the end, use return 0; Commented Nov 9, 2019 at 16:43

1 Answer 1

3

Wrong type

Just as the error says.

g_reservations[seat][0] is a char.

int strncmp(const char *s1, const char *s2, size_t n) expects a char * for s2.

Use &g_reservations[seat][0] or simply g_reservations[seat]

strncmp(passengerName, g_reservations[seat], NAME_LENGTH)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.