2

I am currently in pset2 and working on initials. I have been trying to implement the code

string initial = toupper(name[0]);

It gives an error saying

incompatible integer to pointer conversion initializing 'string' (aka 'char *') with an expression of type 'int'.

I have checked the man pages and was surprised to find that the touuper function takes in and returns integer value.

But how does it work perfectly fine in the following code line?

printf("%c", toupper(name[0]));
4

The Truth Behind chars

Characters are represented using numbers underneath the hood. Each character has a value known as an ASCII value. Learn more about ASCII values!

A variable of type char is capable of storing the ASCII value (i.e., the numerical representation) of a character. It can store values that range from -128 to 127 because it is typically one byte long (see why?).

The Relation Between chars and ints

A value of type char can be implicitly converted to a value of type int since int values can typically range from -2,147,483,648 to 2,147,483,647 on most 32-bit systems. So it is safe to convert a char to an int. Consider the example below

char c = 'A'; // stores the value 65 (i.e., the ASCII value of 'A')
int i = c; // stores the value of c (in this case, 65) converted to an int

Similarly, a value of type int can be implicitly converted to a value of type char. However, since char values have smaller range, if you perform such conversion, you're risking data loss. Here's an example

int i = 66; 
char c = i; /* stores the value 66 (i.e., the ASCII value of 'B')
               no data loss here since 66 is within char range */

int j = 129; // stores the value 129 (NOTICE: the value is out of the char values range)
char d = i; // stores the value -127 (the value wrapped around)

char Values Interpretation

The %c conversion character means "interpret the corresponding argument as a character (e.g., a letter or a digit, or a symbol) not as a number". You can still interpret the value of a char variable to a number (e.g., an int). Consider the following piece of code

char c = 'A'; // stores the value 65 (i.e., the ASCII value of 'A')
printf("%c\n", c); // prints A
printf("%i\n", c); // prints 65

Converting ints to strings (aka char *s)

As for your warning/error, variables of type char * are pointer type variables. They are meant to store memory addresses. But here is the thing: even though memory addresses are typically numbers, there is NO implicit conversion from an int to a pointer type (i.e., you have to do that explicitly if you will). You will learn more about pointers as you proceed with the course.

What you are basically trying to do is that you are trying to assign a value of type int (i.e., the value returned by toupper(name[0]) in this case) to a variable of type char * which I don't really think that this is what you want to do and which the compiler complains about because of the reason shown above.

Fix

If you want to extract the first character of name, convert it to uppercase and store it, you may do the following

string name = "Jas";
char c = toupper(name[0]); 
1

That is because string in fact is a char* or what is the same a pointer. I supose that in your case name[0] is the first character of a string, So what you are trying to do in the first line is assign to the pointer "initial" the value of a char, that is what is giving you the error.

You should use:

char initial = toupper(name[0]);

I fact in the second line you are using %c that is the placeholder for a char, not for a string.

The integer that toupper accepts is the ascii value of the letter contained in the char you want to convert to upper case, and returns the integer that represents the ascii value of the letter converted to upper case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .