- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company

Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
C pointers and arrays: [Warning] assignment makes pointer from integer without a cast [closed]
I'm having some trouble with pointers and arrays in C. Here's the code:
When I compile and run the code I get this warning:
[Warning] assignment makes pointer from integer without a cast [enabled by default]
For line number 9 (ap = a[4];) and the terminal crashes. If I change line 9 to not include a position (ap = a;) I don't get any warnings and it works. Why is this happening? I feel like the answer is obvious but I just can't see it.
3 Answers 3
In this case a[4] is the 5th integer in the array 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 when you try to de-reference it (by doing *ap ) you are trying to access a memory at address 45, which is an invalid address, so your program crashes.
You should do ap = &(a[4]); or ap = a + 4;
In c array names decays to pointer, so a points to the 1st element of the array. In this way, a is equivalent to &(a[0]) .

- 1 @njzk2 I think it is not needed. pointer arithmetic deals with the size. – Dipto Feb 18, 2014 at 15:45
- Oh, now I feel silly. I even read about that a few hours before. Thanks! – user2274889 Feb 18, 2014 at 19:01
- 1 a[4] is the fifth integer in the arry – john Mar 19, 2019 at 4:27
What are you doing: (I am using bytes instead of in for better reading)
You start with int *ap and so on, so your (your computers) memory looks like this:
lets take a look waht happens when (print short cut for ...print("$d", ...)
and so on, so a[0] is the same as *a, a[1] = *(a+1), ....
a[n] just reads easier.
now, what happens at line 9?
So the "warning" is not just a warning it's a severe error.

int[] and int* are represented the same way, except int[] allocates (IIRC).
ap is a pointer, therefore giving it the value of an integer is dangerous, as you have no idea what's at address 45.
when you try to access it ( x = *ap ), you try to access address 45, which causes the crash, as it probably is not a part of the memory you can access.
- 3 " int[] and int* are represented the same way" - only in a function parameter declaration. Otherwise, they are not represented the same way at all. An expression of array type will be converted to an expression of pointer type in most circumstances, but array types and pointer types are not the same at all. – John Bode Feb 18, 2014 at 15:58
- 45 is only accitently part of the addess range, luckily if so you search the failor a long time :-) – halfbit Feb 18, 2014 at 19:38
Not the answer you're looking for? Browse other questions tagged c arrays pointers warnings or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- The [amazon] tag is being burninated
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- Temporary policy: ChatGPT is banned
Hot Network Questions
- Movie with vikings/warriors fighting an alien that looks like a wolf with tentacles
- How can I find out which sectors are used by files on NTFS?
- Applications of super-mathematics to non-super mathematics
- How to handle missing value if imputation doesnt make sense
- Has 90% of ice around Antarctica disappeared in less than a decade?
- Follow Up: struct sockaddr storage initialization by network format-string
- Book about a good dark lord, think "not Sauron"
- What is pictured in this SHERLOC camera?
- Why are physically impossible and logically impossible concepts considered separate in terms of probability?
- Transgenic Peach DNA Splicing
- How many "grounds" are there and what is the difference?
- Recovering from a blunder I made while emailing a professor
- Small bright constellation on the photo
- Are the wavefunctions of electrons in conductors localised?
- How to use Multiwfn software (for charge density and ELF analysis)?
- A limit involving the quotient of two sums
- A-Z related to countries
- Largest Binary Area
- Are 腹違い (harachigai) and 種違い (tanechigai) rude terms to use when referring to half-siblings?
- How to make graphons pictures?
- Biodiversity through radiation
- Is there any way to orbit around the object instead of a 3D cursor?
- What's the difference between a power rail and a signal line?
- Is it correct to use "the" before "materials used in making buildings are"?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

- Today's Posts
- C and C++ FAQ
- Mark Forums Read
- View Forum Leaders
- What's New?
- Get Started with C or C++
- C++ Tutorial
- Get the C++ Book
- All Tutorials
- Advanced Search

- General Programming Boards
- C Programming
Assignment makes pointer from integer without a cast
- Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems
Thread: Assignment makes pointer from integer without a cast
Thread tools.
- Show Printable Version
- Email this Page…
- Subscribe to this Thread…

Search Thread
- Advanced Search
- Linear Mode
- Switch to Hybrid Mode
- Switch to Threaded Mode
- View Profile
- View Forum Posts

I keep getting this error message: "warning: assignment makes pointer from integer without a cast" for line 17 (which I've made red). I've gotten this message a few times before, but I can't remember how I fixed it, and I can't figure out how to fix this one. Any suggestions? Also, it's a code that will take a password entered by the user and then run several for loops until it matches the password. It prints what it's figured out each time it guesses a new letter. Code: #include <stdio.h> #include <string.h> int main(void) { int i, j; char password[25]; char cracked[25]; char *p; char guess = '!'; printf("Enter a password of 25 characters or less: \n"); scanf("%s", password); printf("Password is being cracked..."); for (i = 0, p = password[i]; i < 25; i++, p++) { for(j = 0; j < 90; j++) { if (*p == guess) { strcpy(p, cracked); printf("\t %s \n"); break; } guess++; } //end <search> for loop } //end original for loop return 0; }
Last edited by deciel; 12-13-2011 at 01:57 AM .

Code: for (i = 0, p = password[i]; i < 25; i++, p++) password[i] is the value at index i of password . You want the address of said value, so you want p = &password[i] (or, equivalently, p = password + i ).
Oh! Thank you, it worked!

Originally Posted by deciel I keep getting this error message: "warning: for (i = 0, p = password[i]; i < 25; i++, p++) [/CODE] Note: password[i] == password[0] == *password since this is the assignment portion of for loop and i is set to zero (0).

If you want to set a pointer to the beginning of an array, just use Code: p = password An array name is essentially a pointer to the start of the array memory. Note, for a null terminated string, you could just test for Code: *p //or more explicitly *p == '\0' Also, a 25-element char array doesn't have room for a 25 character string AND a null terminator. And, ask yourself, what's going on when I enter, say a 10 character password, and i > 10.
- Private Messages
- Subscriptions
- Who's Online
- Search Forums
- Forums Home
- C++ Programming
- C# Programming
- Game Programming
- Networking/Device Communication
- Programming Book and Product Reviews
- Windows Programming
- Linux Programming
- General AI Programming
- Article Discussions
- General Discussions
- A Brief History of Cprogramming.com
- Contests Board
- Projects and Job Recruitment

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011
Similar Threads
Assignment makes pointer from integer without a cast, warning: assignment makes integer from pointer without a cast, warning: assignment makes integer from pointer without a cast, ' assignment makes pointer from integer without a cast ".
- C and C++ Programming at Cprogramming.com
- Web Hosting
- Privacy Statement
- Intro Physics Homework Help
- Advanced Physics Homework Help
- Precalculus Homework Help
- Calculus Homework Help
- Bio/Chem Homework Help
- Engineering Homework Help
- Homework Help
- Engineering and Comp Sci Homework Help
C: warning assignment makes integer from pointer without a cast
- Thread starter eatsleep
- Start date Sep 13, 2013
- Sep 13, 2013
Answers and Replies
"RED" is not a character variable - it is a string. The pointer conversion is from the string pointer.
jedishrfu said: choice should be defined as: char *choice;
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.
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 ...
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.
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";
- Sep 14, 2013
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.
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, ... ).
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.
Suggested for: C: warning assignment makes integer from pointer without a cast
- Nov 10, 2015
- Mar 6, 2021
- Feb 2, 2023
- Dec 3, 2021
- May 18, 2022
- May 26, 2022
- Aug 10, 2020
- Yesterday, 7:59 PM
- Oct 14, 2020
- Jul 19, 2019
Hot Threads
- Engineering Differential amplifier confusion (BJTs + Operational Amp)
- Using complex numbers to solve for a current in this circuit
- Calculate the stresses in this 4-member wooden frame
- Op amp current sense circuit
- Engineering How do I know that the angular acceleration is the same for both wheels?
Recent Insights
- Insights How to Measure Internal Resistance of a Battery
- Insights When Lie Groups Became Physics
- Insights Why There Are Maximum Mass Limits for Compact Objects
- Insights Oppenheimer-Snyder Model of Gravitational Collapse: Implications
- Insights Oppenheimer-Snyder Model of Gravitational Collapse: Mathematical Details
- Insights When Discussing the Twin Paradox: Read This First

IMAGES
VIDEO
COMMENTS
A char is a form of integer in C. You are assigning it into a char [] which is a pointer. Hence "converting integer to pointer". Your strToLower makes all its changes in place, there is no reason for it to return anything, especially not a char. You should "return" void, or a char*.
I'm having some trouble with pointers and arrays in C. Here's the code: #include<stdio.h> int *ap; int a [5]= {41,42,43,44,45}; int x; int main () { ap = a [4]; x = *ap; printf ("%d",x); return 0; } When I compile and run the code I get this warning: [Warning] assignment makes pointer from integer without a cast [enabled by default]
If you want to set a pointer to the beginning of an array, just use Code: ? 1 p = password An array name is essentially a pointer to the start of the array memory. Note, for a null terminated string, you could just test for Code: ? 1 2 3 *p //or more explicitly *p == '\0'
What does "warning: assignment makes integer from pointer without a cast [Wint conversion]" mean in C (arrays, pointers, struct, warnings)? Ad by JetBrains Enjoy productive Java with IntelliJ IDEA. Discover instant and clever code completion, on-the-fly code analysis, and reliable refactoring tools. Download 6 Answers Best Maurice Bourdin
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 ... Sep 13, 2013 #6 nsaspook Science Advisor 1,207 2,278