I"m having some trouble understanding the difference in between assigning chars come this array. Ns globally declared

char charlist<7>;and climate proceded to do a function

int stringthing(){ for(int i=0; i and also then explain it in main. It works fine once I assign values through the because that loop, yet when i use

int stringthing() charlist<0> = "M"; charlist<1> = "T"; charlist<2> = "W"; charlist<3> = "R"; charlist<4> = "F"; charlist<5> = "S"; charlist<6> = "U"; return 0;and declare this in main, I obtain the error "assignment provides integer from pointer without a cast ". It"s this the very same thing?


Answers


70
Try an altering the price quotes in your 2nd example indigenous double-quotes come single-quotes. Right now you"re trying to set each selection element to a string quite than a character.

You are watching: Warning: assignment makes integer from pointer without a cast


Wednesday, September 1, 2021
*

answered 2 Months back
Lightness gyeongju in Orbit
18

No, that is not valid C and also has never been precious C. These instances are so-called constraint violations the the standard.

The conventional does not allow you to initialize/assign a reminder to an integer, nor an integer come a pointer. You have to manually force a form conversion v a cast:

int* ns = (int*) 0x1234;int ns = (int)p;If friend don"t use the cast, the password is not valid C and also your compiler is not permitted to permit the code pass without displaying a message. Special, this is regulation by the rule of simple assignment, C17 6.5.16.1 §1:

6.5.16.1 basic assignment

Constraints

One the the complying with shall hold:

the left operand has atomic, qualified, or unqualified arithmetic type, and the ideal hasarithmetic type;the left operand has an atomic, qualified, or unqualified variation of a structure or union typecompatible with the type of the right;the left operand has actually atomic, qualified, or unqualified tip type, and also (considering the typethe left operand would have actually after lvalue conversion) both operands are pointers come qualifiedor unqualified versions of compatible types, and also the kind pointed to by the left has actually all thequalifiers of the type pointed come by the right;the left operand has actually atomic, qualified, or unqualified guideline type, and also (considering the typethe left operand would have after lvalue conversion) one operand is a reminder to an item type,and the various other is a tip to a default or unqualified version of void, and also the type pointed toby the left has actually all the qualifiers the the form pointed to by the right;the left operand is an atomic, qualified, or unqualified pointer, and also the best is a null pointerconstant; orthe left operand has type atomic, qualified, or unqualified _Bool, and also the ideal is a pointer.

In instance of int* p = 0x12345678;, the left operand is a pointer and also the best is an arithmetic type.In situation of int ns = p;, the left operand is one arithmetic type and the right is a pointer.Neither of these fit in with any type of of the constraints cited above.

As for why int* p = 0; works, it is a unique case. The left operand is a pointer and the right is a null pointer constant. More info around the difference in between null pointers, null reminder constants and also the NULL macro.

Some things of note:

If you assign a raw deal with to a pointer, the pointer most likely needs to be volatile qualified, given that it points in ~ something like a hardware it is registered or one EEPROM/Flash storage location, that can adjust its components in run-time.

Converting a reminder to an creature is by no way guaranteed to work even with the cast. The conventional (C17 6.3.2.3 §5 and §6 says):

An integer may be convert to any kind of pointer type. Other than as previously specified, theresult is implementation-defined, can not be appropriately aligned, can not allude to anentity the the referenced type, and also might be a catch representation. 68)

Any pointer form may be convert to an creature type. Except as previously specified, the resultis implementation-defined. If the result cannot be stood for in the integer type, the actions isundefined. The an outcome need not be in the variety of values of any kind of integer type.

See more: When Is " Java Io Ioexception Connection Reset By Peer ? Tutorial: Connection Reset By Peer

Informative foot note:

68) The mapping functions for converting a reminder to an integer or an integer to a reminder are intended to be constant with the addressing framework of the execution environment.

In addition, the address from a pointer might be bigger than what will certainly fit inside an int, together is the instance for most 64 bit systems. As such it is far better to use the uintptr_t native