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;
}