Joe
Joe

Reputation: 401

Error messages are not clear to me

char name[10]="James";  //valid statement

char name[10];
strcpy(name,"james");   //valid statement

char name[10];
name[10]="james";       //invalid statement
*name="james";          // invalid statement

For above mentioned both invalid statment it says "error: assignment makes integer from pointer without a cast"

The error message is not clear. What is integer here? Which pointer is getting converted to integer.

char name[10];
name="james";   //invalid statement

error: incompatible types when assigning to type char[10] from type char

Please explain the error message to me. What exactly they ment.

Upvotes: 1

Views: 1703

Answers (2)

yadhukrishna
yadhukrishna

Reputation: 11

Here name[10]="james"; and *name="james";

These statements are attempting to assign a string literal "james" to a single character of the name array. In C, arrays are indexed starting from 0, so name[10] refers to the 11th element of the array name, which doesn't exist (since the array has a size of 10 and indices go from 0 to 9). The correct way to assign a string to name would be to use strcpy or initialize it directly with a string literal.

name="james";

This statement is trying to assign a string literal "james" to the array name. In C, you cannot assign a string directly to an array in this manner. Arrays in C are not assignable as a whole. Instead, you need to copy the contents of the string to the array using functions like strcpy.

Regarding the error messages:

"error: assignment makes integer from pointer without a cast":

In C, string literals like "james" are of type char *, which is a pointer to a character. When you try to assign a string literal directly to a character array element (name[10] or *name), the compiler interprets it as an attempt to assign a pointer to an integer, hence the error message mentioning conversion from pointer to integer without a cast. "error: incompatible types when assigning to type char[10] from type char":

This error message occurs because you're trying to assign a pointer (char *, which is the type of a string literal) to an array of characters (char[10]), which is not allowed in C. Arrays and pointers are different types in C, and you cannot assign one to the other directly. To fix these issues, you should use strcpy or initialize the array directly with a string literal, like this:

1 - char name[10] = "James"; // Valid statement

2 - char name[10]; strcpy(name, "James"); // Valid statement

Upvotes: -2

Paul R
Paul R

Reputation: 213060

The problem is here:

name[10]="james";

name[10] in this context is a char (which is a type of integer), while "james" is a pointer (to char). So you're trying to convert a pointer to a char, which is an invalid conversion.

Note that when you write:

char name[10]; you're defining a char array of size 10.

When you write just:

name[10]

you're referring to element index 10 of name, i.e. the 11th char in name (which is actually out of bounds - the valid char indices in name are name[0]..name[9]).

Upvotes: 2

Related Questions