Skip to Content

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

Question

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′}.

    Ads Blocker Image Powered by Code Help Pro

    It looks like you are using an adblocker.

    Ads keep our content free. Please consider supporting us by allowing ads on pupuweb.com