I am very very blurry about when to use pointers or not.

I am writing a function (given below), and trying to pass in two variables, statenumber and state. I have them being passed in as chars, and not char *s. I get error messages both ways. I dont know if I need to change them to pointers or not.

I have statenumber set as a char, because my professors specified that it needed to be "an 8 bit integer", and said I should use char. I do not understand why it need to be a char. I think that integers are saved as 4 bit numbers, or something like that. Can anyone explain why I would need it to be a char to be 8 bits? Also, is it possible at all to save a number into char??

Function:

void writestate(char statenumber, char state){

switch (statenumber) {
                case 0: state = "Alabama";
                case 1: state = "Alaska";
                case 2: state = "Arizona";
                case 3: state = "Arkansas";
                case 4: state = "California";
                case 5: state = "Colorado";
                case 6: state = "Connecticut";
                case 7: state = "Delaware";
                case 8: state = "Florida";
                case 9: state = "Georgia";
                case 10: state = "Hawaii";
                case 11: state = "Idaho";
                case 12: state = "Illinois";
                case 13: state = "Indiana";
                case 14: state = "Iowa";
                case 15: state = "Kansas";
                case 16: state = "Kentucky";
                case 17: state = "Louisiana";
                case 18: state = "Maine";
                case 19: state = "Maryland";
                case 20: state = "Massachusetts";
                case 21: state = "Michigan";
                case 22: state = "Minnesota";
                case 23: state = "Mississippi";
                case 24: state = "Missouri";
                case 25: state = "Montana";
                case 26: state = "Nebraska";
                case 27: state = "Nevada";
                case 28: state = "New Hampshire";
                case 29: state = "New Jersey";
                case 30: state = "New Mexico";
                case 31: state = "New York";
                case 32: state = "North Carolina";
                case 33: state = "North Dakota";
                case 34: state = "Ohio";
                case 35: state = "Oklahoma";
                case 36: state = "Oregon";
                case 37: state = "Pennsylvania";
                case 38: state = "Rhode Island";
                case 39: state = "South Carolina";
                case 40: state = "South Dakota";
                case 41: state = "Tennessee";
                case 42: state = "Texas";
                case 43: state = "Utah";
                case 44: state = "Vermont";
                case 45: state = "Virginia";
                case 46: state = "Washington";
                case 47: state = "West Virginia";
                case 48: state = "Wisconsin";
                case 49: state = "Wyoming";
        }
}

This is where I declare my variables:

int main(){
        char name[25] = "unknown", rating[25] = "unknown", state[25] = "unknown", statenumber[2];
        int lobbyists=0, prevemploys=0, number=0;
        double networth=0, tarp=0, contributions=0;

This is where I ask the user for statename:

case 2:
                        printstates();
                        scanf("%s",statenumber);
                        writestate(statenumber,state);
                        printf("State: %s\n\nEnter 9 to display options.\n\n",state);
                        break;

>>case 0: state = "Alabama";

You need to pass state as a pointer to a pointer, and statenumber should probably be an integer instead of char.

void writestate(int statenumber, char** state)
{
    switch(statenumber)
    {
         case 0: *state = "Alabama"; break;
         <snip>
    }
}

int main()
{
    char* state = 0;
    writestate(0, &state);
}

If you want state to be a character array as you posted it, then use strcpy instead of = to set its value

void writestate(char statenumber, char state[])
switch (statenumber[0])
{
case '0': strcpy(state, "Alabama); break;

Note that since you declared statenumber as a character array then you have to use '0' in the switch statement. That also limits the range of state to only 9 states -- '0' to '9'. For that reason its better to declare statenumber as an integer.

Okay that makes some sense. How come I have to use a pointer to a pointer? Why can't I just use regular char?

Also, I he said specifically that he wanted the state to be saved as "an 8-bit integer, 0 = Alabama,…49 Wyoming." But a regular int is only saved as 4 bits or something like that.

Okay that makes some sense. How come I have to use a pointer to a pointer? Why can't I just use regular char?

Also, I he said specifically that he wanted the state to be saved as "an 8-bit integer, 0 = Alabama,…49 Wyoming." But a regular int is only saved as 4 bits or something like that.

How come I have to use a pointer to a pointer? Why can't I just use regular char?

You have to use a pointer to a pointer because the function call will make a copy of it on the stack and you want the address of the char pointer to get the const char * value.

For the integer value you could use char instead of int, it really doesn't matter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.