Dice4321
Dice4321

Reputation: 83

char pointers: invalid conversion from 'char*' to 'char'?

I cut out all the unnecessary code so no one gets too bored with my question... So I cant get the char array to work! on the last few lines of

*whatname = guyname; 
*whatlastname = lastname;

I get an error saying invalid conversion from 'char*' to 'char'. Help would be much appreciated!

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void getname(char *whatname, char *whatlastname);

int main()
{
    int option = 0;
    char guyname = 'x';
    char lastname = 'x';
    bool name_entered = false;

    do{
        printf("1. Enter name.\n");
        printf("2. Enter exam scores.\n");
        printf("3. Display average exam scores. \n");
        printf("4. Display summary. \n");
        printf("5. Quit. \n");
        scanf("%i", &option);

        if( option == 1 )
        {
            name_entered = true;
            getname(&guyname, &lastname);
        }
    else if( option == 4 )
    {
        {
            printf("%s %s based on your exam scores of \n",guyname, lastname);
        }
        else
        {
            printf("Please enter your name in option 1 and you exam scores in option 2 before continuing.\n");
        }
    }
        else if( option == 5 )
        {
            printf(" Come back with a better grade next time.");
            break;
        }
    }while (!(option >5 || option <1));
    return 0;
}

void getname (char *whatname, char *whatlastname)
{
    char guyname[32];
    char lastname[32];
    printf("Enter your first and last name : ");
    scanf("%s %s", &guyname, &lastname);

    guyname[0] = toupper( guyname[0] );
    int len = strlen(guyname);
    for(int i=1; i<len ; i++)
    {
        guyname[i] = tolower( guyname[i]);
    }

    lastname[0] = toupper( lastname[0] );
    int len1 = strlen(lastname);
    for(int k=1; k<len1; k++)
    {
        lastname[k]= tolower( lastname[k]);
    }

    printf("Your name is %s %s\n", guyname, lastname);
    *whatname = guyname;
    *whatlastname = lastname;
}

Upvotes: 6

Views: 43160

Answers (6)

R Sahu
R Sahu

Reputation: 206567

Dealing with char, char*, and char [] in C is a little confusing in the beginning.

Take a look at the following statements:

char str1[] = "abcd";
char const* str2 = "xyz";
char* cp = str1;
char c = *cp;

The first statement and the second statement are identical in their behavior. After the first statement is executed, str1 points to a location that contains 4 characters, in consecutive order. If you think of the memory locations for the string, you might see something like:

+---+---+---+---+
| a | b | c | d |
+---+---+---+---+

str1 points to the address where a is stored. There is a similar arrangement for storing the string "xyz" and str2 points to the address where x is stored.

In the third statement, you are creating cp and making it point where str1 is pointing. After that statement, both cp and str1 point to the same string - "abcd".

*cp evaluates to the character that exists at the address that cp points to. In this case, it will be 'a'.

In the fourth statement, you are initializing c with 'a', the character that exists at the address pointed to by cp.

Now, if you try a statement

*cp = str2;

it is a compiler error. *cp simply dereferences the address of cp. You can put a char at that location, not str2, which is a char*.

You can execute

*cp = *str2;

After that, the objects in the memory that str1 and cp point to will look like:

+---+---+---+---+
| x | b | c | d |
+---+---+---+---+

If you want to copy the string from the address pointed to by str1 to the address pointed to by cp, you can use the standard library function strcpy.

strcpy(cp, str2);

You have to be careful about using strcpy because you have to have enough valid memory to copy to. In this particular example, if you tried

char str3[2];
strcpy(str3, cp);

you will get undefined behavior since there isn't enough memory in str3 to be able to copy "abcd".

Hope that made sense.

Here's a modified version of your code that should work:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void getname(char *whatname, char *whatlastname);

int main()
{
    int option = 0;
    char guyname[32];
    char lastname[32];
    bool name_entered = false;

    do{
        printf("1. Enter name.\n");
        printf("2. Enter exam scores.\n");
        printf("3. Display average exam scores. \n");
        printf("4. Display summary. \n");
        printf("5. Quit. \n");
        scanf("%i", &option);

        if( option == 1 )
        {
            name_entered = true;
            getname(guyname, lastname);
        }
        else if( option == 5 )
        {
            printf(" Come back with a better grade next time.");
            break;
        }
    }while (!(option >5 || option <1));
    return 0;
}

void getname (char *whatname, char *whatlastname)
{
    char guyname[32];
    char lastname[32];
    printf("Enter your first and last name : ");
    scanf("%31s %31s", guyname, lastname);

    guyname[0] = toupper( guyname[0] );
    int len = strlen(guyname);
    for(int i=1; i<len ; i++)
    {
        guyname[i] = tolower( guyname[i]);
    }

    lastname[0] = toupper( lastname[0] );
    int len1 = strlen(lastname);
    for(int k=1; k<len1; k++)
    {
        lastname[k]= tolower( lastname[k]);
    }

    printf("Your name is %s %s\n", guyname, lastname);
    strcpy(whatname, guyname);
    strcpy(whatlastname,lastname);
}

Upvotes: 17

roshan kc
roshan kc

Reputation: 1

There shouldn't be & in scanf of string.

Upvotes: 0

keshlam
keshlam

Reputation: 8058

You have a pair of values in the main which are single characters.

You're passing in pointers to those characters, then dereferencing them (which gives you single characters again), then trying to assign character pointers to them. That definitely isn't going to work.

Even if you did,

whatname = guyname; 
whatlastname = lastname;

which would be type-correct, it wouldn't do what you seem to want, which is to change the values in the calling function. It would only change the values of the argument variables in the subroutine.

If you're trying to return two strings found in the subroutine, change the calling code's variables to:

char *guyname = "x";
char *lastname = "x";

and change the function arguments to

void getname (char **whatname, char **whatlastname)

Then your assignment will be type-correct, and will change the pointers in the calling function.

Upvotes: 0

Gadol21
Gadol21

Reputation: 106

first of all, using * on a pointer will dereference it (you did it on the last two lines) secondly, you are sending to the function a pointer to a single char, not an array of chars so if you will write to its memory you will write on some other variable's memory. What you should have done is declaring guysname and lastname as char arrays

char guysname[32];

and in your function just read your input directly into the parameters you recieve:

scanf("%s %s", whatname, whatlastname);

replace all instances of guysname and lastname for whatname and whatlastname. get rid of the last lines. If you want to keep the char arrays inside the function anyway, just use

strcpy(whatname,guysname)

and the same for the last name.

Upvotes: -1

pm100
pm100

Reputation: 50110

lot of things wrong here. You need to think about where the two string are going to be stored.

You are trying to return variable on the stack of getname - even if you fix the C code that wont work.

a) getname needs to be getname(char**, char**)

b) whatname and whatLastname need to be char *

c) call getname with (&whatname &whatLastName)

d) strdup the strings in getname into the two params (*whatname,...)

Upvotes: 0

ciphermagi
ciphermagi

Reputation: 748

You can't assign a char value to a char pointer, and you can't dereference an Lvalue.

whatname = &guyname;
whatlastname = &guylastname;

This is what you're looking for. However, it appears that you're just starting to play with pointers, and you may need to read up on them a little more before you start experimenting.

Upvotes: -2

Related Questions