Closed.

You are watching: Assignment makes pointer from integer without a cast

This question is not reproducible or was brought about by typos. It is not presently accepting answers.
Want to boost this question? Update the question so it's on-topic for Stack Overflow.

Closed 7 years back.


I"m having actually some trouble with pointers and arrays in C. Here"s the code:

#includeint *ap;int a<5>=41,42,43,44,45;int x;int main() ap = a<4>; x = *ap; printf("%d",x); rerotate 0;When I compile and also run the code I acquire this warning:

assignment provides pointer from integer without a cast

For line number 9 (ap = a<4>;) and the terminal crashes. If I adjust line 9 to not include a position (ap = a;) I do not gain any warnings and also it works. Why is this happening? I feel like the answer is noticeable however I just can"t check out it.


c arrays pointers warnings
Share
Follow
asked Feb 18 "14 at 15:38
*

user2274889user2274889
34111 gold badge44 silver badges77 bronze badges
0
Add a comment |

3 Answers 3


Active Oldest Votes
33
In this case a<4> is the fifth integer in the range a, ap is a pointer to integer, so you are assigning an integer to a pointer and that"s the warning. So ap now holds 45 and as soon as you attempt to de-referral it (by doing *ap) you are trying to accessibility a memory at deal with 45, which is an invalid attend to, so your regime crashes.

You need to carry out ap = &(a<4>); or ap = a + 4;

In c array names decays to reminder, so a points to the first element of the variety.In this means, a is tantamount to &(a<0>).


Share
Follow
edited Mar 27 "19 at 16:03
answered Feb 18 "14 at 15:41
*

DiptoDipto
2,60511 gold badge2020 silver badges3838 bronze badges
3
Add a comment |
12
What are you doing: (I am utilizing bytes instead of in for much better reading)

You begin via int *ap and so on, so your (your computers) memory looks favor this:

-------------- memory supplied by some one else --------000: ?001: ?...098: ?099: ?-------------- your memory --------100: somepoint lets take a look waht happens as soon as (print brief reduced for ...print("$d", ...)

print a<0> -> 41 //no surpriseprint a -> 101 // bereason a points to the begin of the arrayprint *a -> 41 // again the first aspect of arrayprint a+1 -> guess? 102print *(a+1) -> whats behind 102? 42 (we all love this number)and so on, so a<0> is the exact same as *a, a<1> = *(a+1), ....

a simply reads much easier.

now, what happens at line 9?


Share
Follow
answered Feb 18 "14 at 16:08
*

halfbithalflittle
3,08722 gold badges3030 silver badges4040 bronze badges
Add a comment |
1
int<> and also int* are represented the very same way, other than int<> allocates (IIRC).

ap is a reminder, therefore offering it the value of an integer is dangerous, as you have no concept what"s at deal with 45.

See more: Easier Or More Easily ? If You Do That, You Can Accomplish Your Task _______

once you try to accessibility it (x = *ap), you attempt to access resolve 45, which reasons the crash, as it most likely is not a part of the memory you have the right to accessibility.


Share
Follow
answered Feb 18 "14 at 15:42
*

njzk2njzk2
37.3k66 gold badges6363 silver badges103103 bronze badges
2
Add a comment |

Not the answer you're looking for? Browse various other questions tagged c arrays pointers warnings or ask your own question.


The Overflow Blog
Featured on Meta
Linked
0
How to pass string/array to feature correctly? (stm32)
0
In C++ what does it suppose to include a number to a buffer (i.e. buffer+3)?
0
How to set the atmosphere variable of boy exact same as parent to usage execve in C?
Related
3143
Improve INSERT-per-second performance of SQLite
1209
Reference - What does this error intend in PHP?
7
Assignment renders pointer from integer without a actors and also other questions
1
Warning: assignment provides integer from reminder without a cast
1
Why does the compiler problem "warning: assignment renders integer from guideline without a cast"?
Hot Network Questions even more hot concerns
*

lang-c
Stack Overcirculation
Products
Company type of
Stack Exchange Netjob-related
website style / logo design © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.9.13.40199


Stack Overcirculation works finest via JavaScript enabled
*

Your privacy

By clicking “Accept all cookies”, you agree Stack Exadjust deserve to store cookies on your tool and also discshed information in accordance with our Cookie Policy.