Drew
Drew

Reputation: 25

Using toupper with a structure in C

What I am trying to do is to have the user input their information. Like state for example. I need to process this state abbreviation and output it as capital letters. I'm confused at how to do that because I am using structures. When I use what I am using below it tells me they are incompatible and it doesn't work. What should I do differently. I've tried pretty much everything. This is in C.

for (i = 0; i < 3 != '\0'; i++) {
    people[i].state = toupper(people[i].state);
}

Upvotes: 0

Views: 1212

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

It seems you mean the following

for ( i = 0; i < 3; i++ )
{
    for ( char *p = people[i].state; *p; ++p ) *p = toupper( ( unsigned char )*p );
}

Or if you have a single object of the structure type then something like

for ( i = 0; i < 3; i++ )
{
    people.state[i] = toupper( ( unsigned char )people.state[i] );
}

or even

for ( i = 0; i < 3 && people.state[i] != '\0'; i++ )
{
    people.state[i] = toupper( ( unsigned char )people.state[i] );
}

Upvotes: 4

Related Questions