C: warning assignment makes integer from pointer without a cast

Click For Summary

Discussion Overview

The discussion revolves around a C programming warning related to assigning string literals to a character variable, specifically addressing the error "warning assignment makes integer from pointer without a cast." Participants explore the implications of using string literals, pointer types, and the appropriate declarations for handling string assignments in C.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant attempts to assign string literals like "RED" to a character variable, leading to a warning about type conversion.
  • Another participant clarifies that "RED" is a string, not a character, and suggests that choice should be defined as a pointer to a character.
  • Some participants propose defining choice as const char* choice to prevent illegal assignments to string literals.
  • There is a discussion about whether the pointer assignment itself is undefined behavior or if it only applies to modifying the string literal.
  • Several participants note that the behavior may depend on compiler strictness and architecture, particularly in embedded systems.
  • One participant emphasizes that while assigning a pointer to a string literal is legal, attempting to modify the string through the pointer is undefined behavior.
  • Another participant mentions that declaring the pointer as const char* can help catch errors at compile time when attempting to modify the string.

Areas of Agreement / Disagreement

Participants express differing views on the legality and implications of assigning string literals to pointers, with no consensus reached on the best approach or the nature of undefined behavior in this context.

Contextual Notes

Participants reference the C89 standard regarding string literals and their storage duration, highlighting that while accessing a string literal via a pointer is permissible, modifying it is not. There are also mentions of potential compiler behavior variations based on architecture.

eatsleep
Messages
42
Reaction score
0
C: "warning assignment makes integer from pointer without a cast"

1. I am trying to assign a color to the variable choice if it is equal to one of the 3 input numbers.

Code:
if(pred==1) {
		choice = "RED";
		}
	else if(pred==2) {
        	choice = "GREEN";
		}
	else if(pred==3) {
        	choice = "BLUE";
           	}
I have already initilized choice as a character variable and pred as an integer variable. I am not sure what I am missing but I keep getting the error "warning assignment makes integer from pointer without a cast".
 
Last edited by a moderator:
Physics news on Phys.org
"RED" is not a character variable - it is a string. The pointer conversion is from the string pointer.
 
choice should be defined as:

char *choice;

if you used "char choice;" then C thinks of char as an 8-bit integer.
 
jedishrfu said:
choice should be defined as:

char *choice;
That should be const char* choice, not just char* choice.

Assigning to a pointer to a string such as "RED" is illegal (undefined behavior), so it's best to make the pointer a type that does not accept assignments.
 
D H said:
That should be const char* choice, not just char* choice.

Assigning to a pointer to a string such as "RED" is illegal (undefined behavior), so it's best to make the pointer a type that does not accept assignments.

Doesn't this depend on what he's trying to do?

Suppose this choice is in some sort of input loop where first its RED and then its BLUE ...
 
jedishrfu said:
Doesn't this depend on what he's trying to do?

Suppose this choice is in some sort of input loop where first its RED and then its BLUE ...

The 'const char* choice' also tells the compiler that the 'data' at locations RED, BLUE, etc ... pointed to by 'choice' will not change so it can possibility optimize operations or memory storage using those locations.

EDIT: Looking at the C/C++ styles just to double check myself:
const char* == char const *
 
Last edited:
jedishrfu said:
Doesn't this depend on what he's trying to do?

Suppose this choice is in some sort of input loop where first its RED and then its BLUE ...
There's nothing wrong with that. const char * choice (or char const * choice, same thing) means that choice can be used on the left hand side of an assignment statement but that choice[1] cannot.

You are apparently thinking of char * const choice = "RED";, but that's a completely different data type. With this declaration, choice can only be assigned a value in the declaration statement. However, choice[1]='D'; is perfectly legal with this declaration. You can combine the two restrictions with the declaration const char * const choice = "RED";
 
D H said:
Assigning to a pointer to a string such as "RED" is illegal (undefined behavior), so it's best to make the pointer a type that does not accept assignments.
It's my understanding that literal strings are like statics, and exist from start to termination of a program, so why should assigning a pointer to a literal string be undefined? Trying to change a value in the literal string via the pointer would be illegal / undefined behavior, but the pointer assignment shouldn't be an issue.
 
D H said:
There's nothing wrong with that. const char * choice (or char const * choice, same thing) means that choice can be used on the left hand side of an assignment statement but that choice[1] cannot.

You are apparently thinking of char * const choice = "RED";, but that's a completely different data type. With this declaration, choice can only be assigned a value in the declaration statement. However, choice[1]='D'; is perfectly legal with this declaration. You can combine the two restrictions with the declaration const char * const choice = "RED";

Thanks for the explanation, but I was referring to char * as the simplest solution to the OP question.

char *choice = "RED";

He can use choice for whatever he wants.
 
  • #10
rcgldr said:
It's my understanding that literal strings are like statics, and exist from start to termination of a program, so why should assigning a pointer to a literal string be undefined? Trying to change a value in the literal string via the pointer would be illegal / undefined behavior, but the pointer assignment shouldn't be an issue.

It depends on the computer architecture and how strict the compiler is. With something like a uC with (modified) 'Harvard' architecture pointers to program literal strings that might be in ROM or flash and pointers to data in ram might require different memory access methods or pointer sizes, so a ram pointer assignment might be illegal or at least a warning.
 
  • #11
rcgldr said:
It's my understanding that literal strings are like statics, and exist from start to termination of a program, so why should assigning a pointer to a literal string be undefined? Trying to change a value in the literal string via the pointer would be illegal / undefined behavior, but the pointer assignment shouldn't be an issue.

nsaspook said:
It depends on the computer architecture and how strict the compiler is.
The point I was making is how string literals are defined in the C89 and later standards. From the C89 standard, section 3.1.4:

A character string literal has static storage duration and type "array of char", and is initialized with the given characters. A wide string literal has static storage duration and type "array of wchar_t", and is initialized with the wide characters corresponding to the given multibyte characters. ...

Identical string literals of either form need not be distinct. If the program attempts to modify a string literal of either form, the behavior is undefined.


So it would seem that only an attempt to modify a string is undefined, not the usage of a pointer to access a literal string. The type "array of char" or "array of wchar_t" would be the same regardless of where the string literal was stored (ROM, RAM, code section of a program, ... ).
 
  • #12
rcgldr said:
The point I was making is how string literals are defined in the C89 and later standards. From the C89 standard, section 3.1.4:
...
So it would seem that only an attempt to modify a string is undefined, not the usage of a pointer to access a literal string. The type "array of char" or "array of wchar_t" would be the same regardless of where the string literal was stored (ROM, RAM, code section of a program, ... ).

In general I agree on Von Neumann/Princeton architecture, C89 compliance pulls a little slight of hand here by hiding the details using "fat pointers" aka "generic pointers" on Harvard architecture but there are still many C compilers for embedded systems that are not strict C89 on this point (The Microchip C18 compiler is an example) for efficiency on small systems.
http://stackoverflow.com/questions/13484050/pointer-for-rom-variable-in-a-ram-variable
 
Last edited:
  • #13
rcgldr said:
It's my understanding that literal strings are like statics, and exist from start to termination of a program, so why should assigning a pointer to a literal string be undefined? Trying to change a value in the literal string via the pointer would be illegal / undefined behavior, but the pointer assignment shouldn't be an issue.
I wasn't clear with my previous post.

There's nothing wrong per se with char* ptr; ...; ptr="RED"; ...; ptr="BLUE"; What's wrong is assigning into that pointer: *ptr = 'A';. What's worse is that most compilers won't report this as an error. You don't find out until runtime.

Declaring the variable as a const char* pointer (or char const*, same thing) and assignments such as ptr="RED"; are still legal, but now *ptr = 'A'; becomes a compile-time error.
 
  • #14
D H said:
I wasn't clear with my previous post.

There's nothing wrong per se with char* ptr; ...; ptr="RED"; ...; ptr="BLUE"; What's wrong is assigning into that pointer: *ptr = 'A';. What's worse is that most compilers won't report this as an error. You don't find out until runtime.

Declaring the variable as a const char* pointer (or char const*, same thing) and assignments such as ptr="RED"; are still legal, but now *ptr = 'A'; becomes a compile-time error.

I agree. I was assuming the OP had enough knowledge not to shoot himself in the foot with C. I used to teach C to others in my company as part of inhouse training and that assumption wasn't always true.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K