- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
- OverflowAI GenAI features for Teams
- OverflowAPI Train & fine-tune LLMs
- Labs The future of collective knowledge sharing
- About the company Visit the blog
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.
Get early access and see previews of new features.
Error "'strcpy' makes pointer from integer without a cast"
The above code is just part of my full code. When I compiled my full program happened an error strcpy' makes pointer from integer without a cast which is at line
I have referred to the post at here makes pointer from integer without a cast with strcpy after I tried to use strncpy and follow the method showed in the post I still fail in compilation.Hope you can guide me with my problem.Thanks.
- compiler-errors
- you try to copy something on a int and not on a char * – Alexis Commented Jul 23, 2013 at 6:54
- 1 Looks like you're trying to copy one int to another. Can't you just go: p3int = _PhaseShiftPin[i]->_PSPvalue; – nemasu Commented Jul 23, 2013 at 6:57
- Please go through This link again and have a look on type of arguments. – Dayal rai Commented Jul 23, 2013 at 6:58
- try char *p3int = NULL instead of int p3int = 0 ... – someone Commented Jul 23, 2013 at 6:59
2 Answers 2
strcpy is inappropriate for this problem. It takes two arguments. The first is a pointer to char , and the second is a pointer to const char . It then copies the string pointed to by the second pointer into the memory pointed to by the first.
In your case, you are not copying a string, you are copying an int. Your last line should be:
However, the C99 standard reserves identifiers that begin with an underscore ('_') followed by an uppercase letter or another underscore for the implementation. Therefore you must not name your identifiers this way.
If you look at the definition of strcpy you can see
But your _PhaseShiftPin[i]->_PSPvalue is an integer and p3int also.
you can use memcpy
or simply as said in a comment
if you try to copy p3int into _PhaseShiftPin[i]->_PSPvalue
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Sign up or log in
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
Not the answer you're looking for? Browse other questions tagged c compiler-errors or ask your own question .
- The Overflow Blog
- Community Products Roadmap Update, October 2024
- Meet the AI native developers who build software through prompt engineering
- Featured on Meta
- Preventing unauthorized automated access to the network
- Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
- Feedback Requested: How do you use the tagged questions page?
- Proposed designs to update the homepage for logged-in users
Hot Network Questions
- In the 18th century Letters of Recommendation were used as a means of introduction. Was there anything equivalent used in 17th century Europe?
- How can I draw a simple house?
- Question about the way networks are described vs their modern implementations
- How do I make a sedentary culture more adventurous or exploratory?
- Can a specific molecule have more than one point group assigned/associated with it?
- Top and bottom caption in a tabularray table
- “I know what’s best but I do the opposite” translation?
- What is the fastest fixed landing gear airplane?
- Roman Numerals Look and Say in rust (BIO 2020 Q1)
- Tikz Drawing: Coordinate System with Origin on Curve
- Bash - Get certain parameters from a command and put them in a string
- On Copyrighting Hook-Augmented Melodies from the Public Domain
- Why can I define a std::string instance that is constinit? Isn't constinit forbidden if an object requires dynamic initialization?
- What is an intuitive explanation for the East-West component of the Coriolis Force?
- jq create object with property name from variable
- Is there any global settings to add \limits to evey \sum?
- What is the situation when a contract is altered during transmission?
- Can sets that can't be expressed as Cartesian products become expressible as such by converting to polar coordinates? (Munkres Topology Exercise 1.10)
- How to draw vector on 3D surface
- Can a carbon crank be safely put into a vice?
- Definition of vector
- Finding a formula which involves iteration
- Rust beneath paintwork on steel top tube
- Does it make sense to mature a yeast dough made from gluten-free flour?
- GitHub Login
- Twitter Login
Welcome to Coder Legion Community
With 352 amazing developers, connect with, already have an account log in.
- Share on Facebook
- Share on Twitter
- Share on Reddit
- Share on LinkedIn
- Share on Pinterest
- Share on HackerNews
Assignment makes integer from pointer without a cast in c
Programming can be both rewarding and challenging. You work hard on your code, and just when it seems to be functioning perfectly, an error message pops up on your screen, leaving you frustrated and clueless about what went wrong. One common error that programmers encounter is the "Assignment makes integer from pointer without a cast" error in C.
This error occurs when you try to assign a value from a pointer variable to an integer variable without properly casting it. To fix this error, you need to make sure that you cast the pointer value to the appropriate data type before assigning it to an integer variable. In this article, we will dive deeper into the causes of this error and provide you with solutions to overcome it.
What makes this error occur?
I will present some cases that triggers that error to occur, and they are all have the same concept, so if you understanded why the failure happens, then you will figure out how to solve all the cases easily.
Case 1: Assignment of a pointer to an integer variable
In this simple code we have three variables, an integer pointer "ptr" , and two integers "n1" and "n2" . We assign 2 to "n1" , so far so good, then we assign the address of "n2" to "ptr" which is the suitable storing data type for a pointer, so no problems untill now, till we get to this line "n2 = ptr" when we try to assign "ptr" which is a memory address to "n2" that needs to store an integer data type because it's not a pointer.
Case 2: Returning a Pointer from a Function that Should Return an Integer
As you can see, it's another situation but it's the same idea which causes the compilation error. We are trying here to assign the return of getinteger function which is a pointer that conatains a memory address to the result variable that is an int type which needs an integer
Case 3: Misusing Array Names as Pointers
As we might already know, that the identifier (name) of the array is actually a pointer to the array first element memory address , so it's a pointer after all, and assigning a pointer type to int type causes the same compilation error.
The solutions
The key to avoiding the error is understanding that pointers and integers are different types of variables in C. Pointers hold memory addresses, while integers hold numeric values. We can use either casting , dereferencing the pointer or just redesign another solution for the problem we are working on that allows the two types to be the same. It all depending on the situation.
Let's try to solve the above cases:
Case 1: Solution: Deferencing the pointer
We need in this case to asssign an int type to "n2" not a pointer or memory address, so how do we get the value of the variable that the pointer "ptr" pointing to? We get it by deferencing the pointer , so the code after the fix will be like the following:
Case 2: Solution: Choosing the right data type
In this case we have two options, either we change the getinteger returning type to int or change the result variable type to a pointer . I will go with the latter option, because there are a lot of functions in the C standard library that returning a pointer, so what we can control is our variable that takes the function return. So the code after the fix will be like the following:
We here changed the result variable from normal int to an int pointer by adding "*" .
Case 3: Solution: Using the array subscript operator
In this case we can get the value of any number in the array by using the subscript opeartor ([]) on the array with the index number like: myarray[1] for the second element which is 2 . If we still remember that the array identifier is a pointer to the array first memory, then we can also get the value of the array first element by deferencing the array identifier like: *myarray which will get us 1 .
But let's solve the case by using the subscript opeartor which is the more obvious way. So the code will be like the following:
Now the number 1 is assigned to myint without any compilation erros.
The conclusion
In conclusion, the error "assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast" arises in C programming when there is an attempt to assign a memory address (held by a pointer) directly to an integer variable. This is a type mismatch as pointers and integers are fundamentally different types of variables in C.
To avoid or correct this error, programmers need to ensure they are handling pointers and integers appropriately. If the intent is to assign the value pointed by a pointer to an integer, dereferencing should be used. If a function is meant to return an integer, it should not return a pointer. When dealing with arrays, remember that the array name behaves like a pointer to the first element, not an individual element of the array.
Please log in to add a comment.
- Sep 8, 2023 | |
- Jun 4 | |
- Jun 1 | |
- May 6 | |
- Apr 9 |
- Send feedback
More From Phantom
Tips and tricks in article formatting for coderlegion, markdown divs in coderlegion, code formatting tests in coderlegion.