Skip to Content

Answered: Why int * a = 10 shows an error but char *b = “hello” is ok?


When I assign an integer value to the pointer ‘a’ (i.e. int *a = 10), I receive an error. However, when I assign a character value to the pointer ‘b’ (i.e. char *b = “hello”), no error is displayed. Could you please explain why this is the case?

Answer #1

The error occurs when you try to assign an integer value to a pointer of type int because a pointer variable is used to store the memory address of a variable, and an integer value cannot be converted to a memory address. However, when you assign a string literal to a char pointer, it assigns the memory address of the first character of the string to the pointer, which is a valid operation.

Answer #2

The literal 10 has an integer type. The variable ‘a’ is of type pointer to integer, which is incompatible.

On the other hand, the type of “hello” is character array, which is compatible with a ‘char *’ pointer to character.

Answer #3

When representing a pointer, we must send the address as the pointer value. For example, in int* a = 10; Here, 10 is a value that cannot be copied to a pointer type.

In the case of char* b = “hello”; we are passing the starting address of the “hello” string to the variable ‘b’. However, when attempting to assign char* b = ‘h’, an error is produced as we are assigning a value to the pointer variable ‘b’ instead of an address.

If you require an integer pointer, we can use it in the following:

int i = 10;
int* a = &i;

where ‘a’ will have address of ‘i’

Answer #3

Hello is a string represented as an array of characters, with the address of the first index stored in the pointer b. Compilers are designed to represent data as character arrays, even when not explicitly declared as such, by storing the address as a pointer variable. However, this feature is not available for integer data types.

Answer #4

The expression int *a = 10; is not analogous to char *b = ‘a’; (single quotes). This will result in the same error.

Now, what does char *b = “hello”; mean ?

In C++, there is a concept of String literal to provide ease of use. For example, when defining a variable that stores a name, one would use a String literal.

char var[] = {'R','a','m','\0'};

It is necessary to include the additional ‘\0’ character at the end of the string to guarantee proper handling of the name. This can be quite tedious. String literals provide a convenient solution, as they will automatically include the delimiter at the end. Therefore, you can use the following code instead of the aforementioned code:

char var[] = "Ram";

It is important to note that the name of an array is a pointer to its first element, meaning that the statement can be written as such:

char *var = "Ram";

Thus, the variable “var” is a pointer that points to the first element of the array {‘R’,’a’,’m’,’\0′}.

Alex Lim is a certified IT Technical Support Architect with over 15 years of experience in designing, implementing, and troubleshooting complex IT systems and networks. He has worked for leading IT companies, such as Microsoft, IBM, and Cisco, providing technical support and solutions to clients across various industries and sectors. Alex has a bachelor’s degree in computer science from the National University of Singapore and a master’s degree in information security from the Massachusetts Institute of Technology. He is also the author of several best-selling books on IT technical support, such as The IT Technical Support Handbook and Troubleshooting IT Systems and Networks. Alex lives in Bandar, Johore, Malaysia with his wife and two chilrdren. You can reach him at [email protected] or follow him on Website | Twitter | Facebook

    Ads Blocker Image Powered by Code Help Pro

    Your Support Matters...

    We run an independent site that is committed to delivering valuable content, but it comes with its challenges. Many of our readers use ad blockers, causing our advertising revenue to decline. Unlike some websites, we have not implemented paywalls to restrict access. Your support can make a significant difference. If you find this website useful and choose to support us, it would greatly secure our future. We appreciate your help. If you are currently using an ad blocker, please consider disabling it for our site. Thank you for your understanding and support.