• 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.

C - Assignment makes integer from pointer without a cast

I'm getting this error: Warning: assignment makes integer from pointer without a cast [enabled by default] This is my source code:

So, I'm getting an error ( warning ) and my result of my function ft_replace is fxc... It return me a result like 4227111 and I don't know why I would like my function when it find the last char here this is ' - ' it return the position of the array.

So a string which contains " Hel-l-o " -> Return 6 A string which contains " He-llo " -> Return 3 A string which contains " Hell-o- " -> Return 7

  • 2 Well.. it's pretty clearly said - s1 is pointer, not a number(index). You can use additional counter, to count the index/position, or use pointer arithmetic to calculate the position. –  Kiril Kirov Commented May 21, 2015 at 13:34
  • @user3208656 And what to return if the character is not found in a string? –  Vlad from Moscow Commented May 21, 2015 at 13:35
  • What is the intent of this function? Are you trying to return the index of the last '-' within s1? If so then you probably want this instead: int ft_replace(char const *s1) { int result; result = 0; for (const char* s = s1; *s != '\0'; ++s) { if (*s == '-') result = (s1-s); } return (result); } –  Ron Kuper Commented May 21, 2015 at 13:39

4 Answers 4

result = s1

result is an int , integer s1 is a char const* , a const pointer to char

You can't assign the value of a const char* to an integer (because they might not have the same size for example).

You can however, as hinted by the warning, cast the value to the correct type to force it to stop warning you:

But this is not a good practice as it can induce undefined behavior. Reading your code, I don't think you are doing what you think you are doing anyway when you assign s1 to result . What you should be doing, is incrementing a counter and set result during the loop:

Eregrith's user avatar

In your code, result is int and s1 is char const * .

You can change it to

However, there is no way to know the position of the array currently. You need to have a separate counter to keep track of the valid element present in the array .

Natasha Dutta's user avatar

  • 3 @Eregrith: You certainly can assign a char to an int , it just won't do anything useful in this case. –  Bill Lynch Commented May 21, 2015 at 13:37
  • @Eregrith what about §6.5.16.1, paragraph 2? –  Natasha Dutta Commented May 21, 2015 at 13:42
  • @KirilKirov well, OP has logical issue in his code, that I have pointed out later. Otherwise, as per the question title, this is the way to remove the error. Now whether if fits the logic, that is another aspect (which also I addressed, I think). –  Natasha Dutta Commented May 21, 2015 at 13:51
  • @BillLynch And NatashaDutta you are both right. My bad –  Eregrith Commented May 21, 2015 at 13:51
  • @Natasha Dutta How do you do?:) –  Vlad from Moscow Commented May 21, 2015 at 13:52

I think you mean the following

As for your code then this statement

does not make sense. In the left side of the assignment there is an object of type int while in the right side of the assignment there is a pointer of type const char *

If you want to return pointer to the last occurence of character '-' then the function can look like

Vlad from Moscow's user avatar

A pointer holds the memory address, not the position in the array.

Use an index variable to keep track of the position.

Note that the first element in an array in C has index 0 , hence the +1 when assigning result :

Klas Lindbäck's user avatar

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 .

  • The Overflow Blog
  • Where does Postgres fit in a world of GenAI and vector databases?
  • Mobile Observability: monitoring performance through cracked screens, old...
  • Featured on Meta
  • Announcing a change to the data-dump process
  • Bringing clarity to status tag usage on meta sites
  • What does a new user need in a homepage experience on Stack Overflow?
  • Staging Ground Reviewer Motivation
  • Feedback requested: How do you use tag hover descriptions for curating and do...

Hot Network Questions

  • Journal keeps messing with my proof
  • Determining Error Rate of Phase Shift Keying via Monte Carlo Simulation
  • Overstayed Schengen but can I switch to US passport?
  • How is it possible to know a proposed perpetual motion machine won't work without even looking at it?
  • Whence “uniform distribution”?
  • Do passengers transiting in YVR (Vancouver) from international to US go through Canadian immigration?
  • What are the 270 mitzvot relevant today?
  • Is there a phrase for someone who's really bad at cooking?
  • How to place an arrayplot/image on a GeoGraphics?
  • If Starliner returns safely on autopilot, can this still prove that it's safe? Could it be launched back up to the ISS again to complete it's mission?
  • A short story about a boy who was the son of a "normal" woman and a vaguely human denizen of the deep
  • Is 3 ohm resistance value of a PCB fuse reasonable?
  • How to remove obligation to run as administrator in Windows?
  • Has a tire ever exploded inside the Wheel Well?
  • Held Action Sneak attack after action surge
  • How to count mismatches between two rows, column by column R?
  • How to disable Google Lens in search when using Google Chrome?
  • Cannot open and HTML file stored on RAM-disk with a browser
  • Why is the movie titled "Sweet Smell of Success"?
  • Why are complex coordinates outlawed in physics?
  • Can unlimited Duration spells be Dismissed?
  • Why does average of Monte Carlo simulations not hold for simple problem?
  • Math format bug for \mathbf command in TeXLive 2023/2024
  • ESTA is not letting me pay

assignment pointer from integer without a cast

  • Intro Physics Homework Help
  • Advanced Physics Homework Help
  • Precalculus Homework Help
  • Calculus Homework Help
  • Bio/Chem Homework Help
  • Engineering Homework Help

Follow along with the video below to see how to install our site as a web app on your home screen.

Note: This feature may not be available in some browsers.

  • 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
  • Tags Assignment Integer
  • Sep 13, 2013

A PF Electron

  • Higher-order topological simulation unlocks new potential in quantum computers
  • Using a gamma ray burst to search for violations of Einstein's relativity postulates
  • Researchers present new diagnostic tool for laser-plasma accelerator using metal foil as 3D scanner

"RED" is not a character variable - it is a string. The pointer conversion is from the string pointer.  

A PF SuperCluster

A pf planet.

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.

Related to C: warning assignment makes integer from pointer without a cast

This warning indicates that a pointer value is being assigned to an integer variable without being explicitly converted or casted. This can create unexpected behavior and should be avoided.

In C, pointers are used to store memory addresses of variables or data. Assigning a pointer value to an integer variable can result in loss of information or incorrect memory access, which is why the compiler issues this warning.

To fix this warning, you can use a typecast to explicitly convert the pointer value to an integer before assigning it to the variable. However, make sure that the conversion is appropriate and does not cause any loss of information.

While this warning may not always result in errors, it can lead to unexpected behavior and should be addressed. Ignoring this warning can potentially cause issues in your program, so it is best to fix it.

In some cases, using a different data type for the variable may be a better solution than using a typecast. For example, if the pointer is pointing to a string, you can use a string data type instead of converting it to an integer.

Similar threads

  • Nov 10, 2015
  • Apr 5, 2011
  • Oct 14, 2020
  • Jun 11, 2017
  • Feb 9, 2020
  • Jan 30, 2015
  • Jul 19, 2019
  • Sep 16, 2014
  • May 26, 2015
  • May 9, 2017

Hot Threads

  • Engineering   Help Calculating Length And Position Of Connections On Rotating Object
  • Comp Sci   Training YOLOv9 on custom dataset
  • Engineering   H-parameter model for non-inverting amplifier
  • Second order differential - Tanks in series cooling coil
  • Engineering   How High Was the Balloon When the Stone Was Dropped?

Recent Insights

  • Insights   Brownian Motions and Quantifying Randomness in Physical Systems
  • Insights   PBS Video Comment: “What If Physics IS NOT Describing Reality”
  • Insights   Aspects Behind the Concept of Dimension in Various Fields
  • Insights   Views On Complex Numbers
  • Insights   Addition of Velocities (Velocity Composition) in Special Relativity
  • Insights   Schrödinger’s Cat and the Qbit

Position Is Everything

Makes Pointer From Integer Without a Cast: Fix It Now!

  • Recent Posts

Melvin Nolan

  • MySQL No Database Selected: Actionable Tips to Fix the Error - July 12, 2024
  • How to Un-Snooze Email in Outlook: A Step-by-Step Guide - July 12, 2024
  • How to Search Outlook for Multiple Words: Boost Your Efficiency - July 12, 2024

Makes Pointer From Integer Without a Cast

Our research team has ensured that this will be your best resource on these error messages, and you won’t have to read another article about it again. With that said, launch your code that’s showing the error messages, and let’s fix it for you.

JUMP TO TOPIC

– You Assigned an Integer to a Pointer

– you want to convert an integer to a pointer, – you passed a variable name to the “printf()” function, – you used “struct _io_file” in a wrong way, – you copied a string to an invalid location, – you’re setting a pointer to a different type, – use equal data types during assignment, – ensure the pointer and integer have the same sizes, – pass a “format string” to the “printf()” function, – use a function that returns a pointer to “struct _io_file”, – copy the string to character array or character pointer, – assign pointers of compatible types, why your code made a pointer from an integer without a cast.

Your code made a pointer from an integer without a cast because you assigned an integer to a pointer or you want to convert an integer to a pointer. Other causes include the passing of a variable name to the “printf()” function and using “struct _io_file in the wrong way.

Finally, the following are also possible causes:

  • You copied a string to an invalid location
  • You’re setting a pointer to a different type

If you assign an integer to a pointer, it will lead to the “ assignment makes pointer from integer without a cast c programming ” error. For example, in the following, the “num_array” variable is an unsigned integer type while “tmp_array” is a pointer.

Later, an error will occur when the “for” loop tries to copy the values of the “num_array” to the “tmp_array” pointer using a variable assignment.

#include<stdio.h>

#include<stdint.h>

uint8_t num_array[8] = {0, 1, 2, 3, 4, 5, 6, 7};

void copy_arr_values() {

int i;

uint8_t *tmp_array[8];

for(i=0; i<8; i++){

tmp_array[i] = num_array[(i+3)%8];

}

}

int main() {

copy_arr_values();

}

 

Makes Pointer From Integer Without a Cast Causes

For example, in the following, the “theta” variable is an integer while “ptr_theta” is a pointer. Both have different sizes, and you can’t convert the integer to a pointer.

#include<stdio.h>

int main() {

int theta = 10;

int *ptr_theta = (int *)theta;

}

If you pass a variable name to the “printf()” function, that’s when the compiler will throw the “ passing argument 1 of ‘printf’ makes pointer from integer without a cast ” error.

For example, in the following, “num_val” is an integer variable, and the code is trying to print it using the “printf()” function.

#include <stdio.h>

int main() {

int num_val = 42;

printf(num_val); //Error!

return 0;

}

When you assign an integer to a pointer of “struct _io_file”, that’s when you’ll get the “ assignment to file aka struct _io_file from int makes pointer from integer without a cast ” error. For example, in the following code, “FILE” is a “typedef” for “struct _io_file”. This makes it a pointer to “struct _io_file”.

Later, the code assigned it to the integer “alpha,” and this will lead to an error stated below:

#include <stdio.h>

int main(void) {

int alpha = 10;

FILE *beta = alpha;

return 0;

}

Makes Pointer From Integer Without a Cast Reasons

Now, in the following, the code is trying to copy a string (“source”) into an integer (“destination”), and this leads to an error because it’s not a valid operation:

#include<stdio.h>

#include<string.h>

int main() {

int destination;

char source[] = “Hello, World!”;

strcpy(destination, source);

return 0;

}

When your code sets a pointer to a different type, your compiler will show the “ incompatible pointer type ” error. In the following code, “pacifier” is an integer pointer, while “qwerty” is a character pointer.

Both are incompatible, and your C compiler will not allow this or anything similar in your code.

#include<stdio.h>

int main() {

int charlie = 5;

int *pacifier = &charlie;

char *qwerty = pacifier;

}

How To Stop a Pointer Creation From an Integer Without a Cast

You can stop a pointer creation from an integer without a cast if you use equal data types during the assignment or ensure the integer and pointer have the same sizes. What’s more, you can pass a “format string” to “printf()” and use a function that returns a pointer to “struct _io_file”.

  • Copy the string to a character array or character pointer
  • Assign pointers of compatible types

During a variable assignment, use variables of equal data types. In our first example, we assigned a pointer (uint8_t *tmp_array) to an unsigned integer (uint8_t num_array) and it led to a compile-time error.

Now, the following is the revised code, and we’ve changed “tmp_array” to a real array. This will prevent the “ gcc warning: assignment makes pointer from integer without a cast ” error during compilation.

#include<stdio.h>

#include<stdint.h>

uint8_t num_array[8] = {0, 1, 2, 3, 4, 5, 6, 7};

int main() {

int i;

// Fix: change tmp_array to an array

uint8_t tmp_array[8] = {};

for (i = 0; i < 8; i++){

tmp_array[i] = num_array[(i+3)%8];

}

int length = sizeof(tmp_array)/sizeof(tmp_array[0]);

printf(“Elements of the ‘tmp_array’: \n”);

for (int i = 0; i < length; i++) {

printf(“%d “, tmp_array[i]);

}

}

Makes Pointer From Integer Without a Cast Fixes

Now, the fix is to use “intptr_t” from the “stdint.h” header file because it guarantees that the pointer and integer will have the same sizes. We’ve used it in the following code, and you can compile it without an error.

#include<stdio.h>

#include<stdint.h>

int main() {

int theta = 10;

int *ptr_theta = (int *)(intptr_t)theta;

printf(“%p”, ptr_theta);

}

To fix the “pointer to integer without a cast” in the “printf()” function, pass a “format string” as its first argument. How you write the “format string” depends on the variable that you’ll print. In the following updated code, “num_val” is an integer, so the “format string” is “%d”.

#include <stdio.h>

int main() {

int num_val = 42;

printf(“%d”, num_val);

return 0;

}

When you’re using “FILE” from the “stdlib.h” header file, you can prevent any “pointer from integer” error if you use a function that returns a pointer to “struct _io_file”.

An example of such a function is “fopen()” which allows you to open a file in C programming. Now, the following is a rewrite of the code that causes the pointer error in “struct _io_file”. This time, we use “fopen()” as a pointer to “FILE”.

#include <stdio.h>

int main(void) {

FILE *f = fopen(“myfile.txt”, “r”);

if (f == NULL) {

perror(“Error opening file”);

return 1;

}

char c;

while ((c = fgetc(f)) != EOF) {

printf(“%c”, c);

}

return 0;

}

Makes Pointer From Integer Without a Cast Solutions

Now, in the following code, we’ve changed “destination” from an integer to a “character array”. This means “strcpy()” can copy a string into this array without an error.

#include<stdio.h>

#include<string.h>

int main() {

char destination[100];

char source[] = “Hello, World!”;

strcpy(destination, source);

printf(“%s”, destination);

return 0;

}

Meanwhile, the following is another version that turns the “destination” into a “character pointer”. With this, you’ll need to allocate memory using the “malloc()” function from the “stdlib.h” header file.

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int main() {

char* destination;

char source[] = “Hello, World!”;

destination = malloc(100);

strcpy(destination, source);

printf(“%s”, destination);

free(destination);

return 0;

}

When you’re assigning pointers, ensure that they’re compatible by changing their data type . The following is the updated code for the “charlie” example , and “qwerty” is now an integer.

#include<stdio.h>

int main() {

int charlie = 5;

int *pacifier = &charlie;

int *qwerty = pacifier;

}

This article explained why your code made a pointer without a cast and how you can fix it. The following is a summary of what we talked about:

  • An attempt to convert an integer to a pointer will lead to the “makes pointer from integer without a cast wint conversion” error.
  • To prevent an “incompatible pointer type” error, don’t set a pointer to a different type.
  • If you’re using the “printf()” function, and you want to prevent any “pointer to integer” error, always pass a “format specifier”.

At this stage, you’ll be confident that you can work with integers and pointers in C programming without an error. Save our article, and share it with your developer communities to help them get rid of this error as well.

Related posts:

  • Input String Was Not in a Correct Format: Working Solutions
  • Best solutions to get rid of “operand should contain 1 column(s)” error
  • Another Git Process Seems To Be Running in This Repository: Solutions
  • Linker Command Failed With Exit Code 1: Causes and Fixes of This Error
  • Valueerror: No Json Object Could Be Decoded: Fix This Python Error
  • Conversion Failed When Converting Date And/or Time From Character String.
  • Importerror: Libcublas.So.9.0: Cannot Open Shared Object File: No Such File or Directory
  • Exception Code 0xe0434352: Read or Let It Disturb You
  • Noclassdeffounderror: An Article That Explains the Details
  • HTTP Error 500.19 – Internal Server Error: Fixing the Issue
  • Libressl SSL_connect: SSL_error_syscall in Connection to Github.com:443
  • Kudu Giving Forbidden Error 403: The Necessary Guide

Leave a Comment Cancel reply

Save my name, email, and website in this browser for the next time I comment.

Ubuntu Forums

  • Unanswered Posts
  • View Forum Leaders
  • Contact an Admin
  • Forum Council
  • Forum Governance
  • Forum Staff

Ubuntu Forums Code of Conduct

  • Forum IRC Channel
  • Get Kubuntu
  • Get Xubuntu
  • Get Lubuntu
  • Get Ubuntu Studio
  • Get Ubuntu Cinnamon
  • Get Edubuntu
  • Get Ubuntu Unity
  • Get Ubuntu Kylin
  • Get Ubuntu Budgie
  • Get Ubuntu Mate
  • Ubuntu Code of Conduct
  • Ubuntu Wiki
  • Community Wiki
  • Launchpad Answers
  • Ubuntu IRC Support
  • Official Documentation
  • User Documentation
  • Distrowatch
  • Bugs: Ubuntu
  • PPAs: Ubuntu
  • Web Upd8: Ubuntu
  • OMG! Ubuntu
  • Ubuntu Insights
  • Planet Ubuntu
  • Full Circle Magazine
  • Activity Page
  • Please read before SSO login
  • Advanced Search

Home

  • The Ubuntu Forum Community
  • Ubuntu Specialised Support
  • Development & Programming
  • Programming Talk

[SOLVED] C - assigment makes integer from pointer without a cast warning

  • Hello Unregistered, you are cordially invited to participate in a discussion about the future of the forum. Please see this thread .

Thread: [SOLVED] C - assigment makes integer from pointer without a cast warning

Thread tools.

  • Show Printable Version
  • Subscribe to this Thread…
  • Linear Mode
  • Switch to Hybrid Mode
  • Switch to Threaded Mode
  • View Profile
  • View Forum Posts
  • Private Message

usernamer is offline

I know it's a common error, and I've tried googling it, looked at a bunch of answers, but I still don't really get what to do in this situation.... Here's the relevant code: Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char *argv[] ) { char path[51]; const char* home = getenv( "HOME" ); strcpy( path, argv[1] ); path[1] = home; return 0; } -- there is more code in the blank lines, but the issue's not there (I'm fairly sure), so didn't see the point in writing out 100 odd lines of code. I've tried some stuff like trying to make a pointer to path[1], and make that = home, but haven't managed to make that work (although maybe that's just me doing it wrong as opposed to wrong idea?) Thanks in advance for any help

r-senior is offline

Re: C - assigment makes integer from pointer without a cast warning

path[1] is the second element of a char array, so it's a char. home is a char *, i.e. a pointer to a char. You get the warning because you try to assign a char* to a char. Note also the potential to overflow your buffer if the content of the argv[1] argument is very long. It's usually better to use strncpy.
Last edited by r-senior; March 10th, 2013 at 03:03 PM . Reason: argv[1] would overflow, not HOME. Corrected to avoid confusion.
Please create new threads for new questions. Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].
  • Visit Homepage

Christmas is offline

You can try something like this: Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char *argv[] ) { char *path; const char *home = getenv("HOME"); path = malloc(strlen(home) + 1); if (!path) { printf("Error\n"); return 0; } strcpy(path, home); printf("path = %s\n", path); // if you want to have argv[1] concatenated with path if (argc >= 2) { path = malloc(strlen(home) + strlen(argv[1]) + 1); strcpy(path, argv[1]); strcat(path, home); printf("%s\n", path); } // if you want an array of strings, each containing path, argv[1]... char **array; int i; array = malloc(argc * sizeof(char*)); array[0] = malloc(strlen(home) + 1); strcpy(array[0], home); printf("array[0] = %s\n", array[0]); for (i = 1; i < argc; i++) { array[i] = malloc(strlen(argv[i]) + 1); strcpy(array[i], argv[i]); printf("array[%d] = %s\n", i, array[i]); } // now array[i] will hold path and all the argv strings return 0; } Just as above, your path[51] is a string while path[1] is only a character, so you can't use strcpy for that.
Last edited by Christmas; March 10th, 2013 at 09:51 PM .
TuxArena - Ubuntu/Debian/Mint Tutorials | Linux Stuff Intro Tutorials | UbuTricks I play Wesnoth sometimes. And AssaultCube .
Originally Posted by Christmas You can try something like this: Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main( int argc, char *argv[] ) { char *path; const char *home = getenv("HOME"); path = malloc(strlen(home) + 1); if (!path) { printf("Error\n"); return 0; } strcpy(path, home); printf("path = %s\n", path); // if you want to have argv[1] concatenated with path if (argc >= 2) { path = malloc(strlen(home) + strlen(argv[1]) + 1); strcpy(path, argv[1]); strcat(path, home); printf("%s\n", path); } // if you want an array of strings, each containing path, argv[1]... char **array; int i; array = malloc(argc * sizeof(char*)); array[0] = malloc(strlen(home) + 1); strcpy(array[0], home); printf("array[0] = %s\n", array[0]); for (i = 1; i < argc; i++) { array[i] = malloc(strlen(argv[i]) + 1); strcpy(array[i], argv[i]); printf("array[%d] = %s\n", i, array[i]); } // now array[i] will hold path and all the argv strings return 0; } Just as above, your path[51] is a string while path[1] is only a character, so you can't use strcpy for that. Excellent point. I've basically fixed my problem by reading up on pointers again (haven't done any C for a little while, so forgot some stuff), and doing: Code: path[1] = *home; the code doesn't moan at me when I compile it, and it runs okay (for paths which aren't close to 51 at least), but after reading what you read, I just wrote a quick program and found out that getenv("HOME") is 10 characters long, not 1 like I seem to have assumed, so I'll modify my code to fix that.
Yes, getenv will return the path to your home dir, for example /home/user, but path[1] = *home will still assign the first character of home to path[1] (which would be '/').
  • Private Messages
  • Subscriptions
  • Who's Online
  • Search Forums
  • Forums Home
  • New to Ubuntu
  • General Help
  • Installation & Upgrades
  • Desktop Environments
  • Networking & Wireless
  • Multimedia Software
  • Ubuntu Development Version
  • Virtualisation
  • Server Platforms
  • Ubuntu Cloud and Juju
  • Packaging and Compiling Programs
  • Development CD/DVD Image Testing
  • Ubuntu Application Development
  • Ubuntu Dev Link Forum
  • Bug Reports / Support
  • System76 Support
  • Apple Hardware Users
  • Recurring Discussions
  • Mobile Technology Discussions (CLOSED)
  • Announcements & News
  • Weekly Newsletter
  • Membership Applications
  • The Fridge Discussions
  • Forum Council Agenda
  • Request a LoCo forum
  • Resolution Centre
  • Ubuntu/Debian BASED
  • Arch and derivatives
  • Fedora/RedHat and derivatives
  • Mandriva/Mageia
  • Slackware and derivatives
  • openSUSE and SUSE Linux Enterprise
  • Gentoo and derivatives
  • Any Other OS
  • Assistive Technology & Accessibility
  • Art & Design
  • Education & Science
  • Documentation and Community Wiki Discussions
  • Outdated Tutorials & Tips
  • Ubuntu Women
  • Arizona Team - US
  • Arkansas Team - US
  • Brazil Team
  • California Team - US
  • Canada Team
  • Centroamerica Team
  • Instalación y Actualización
  • Colombia Team - Colombia
  • Georgia Team - US
  • Illinois Team
  • Indiana - US
  • Kentucky Team - US
  • Maine Team - US
  • Minnesota Team - US
  • Mississippi Team - US
  • Nebraska Team - US
  • New Mexico Team - US
  • New York - US
  • North Carolina Team - US
  • Ohio Team - US
  • Oklahoma Team - US
  • Oregon Team - US
  • Pennsylvania Team - US
  • Texas Team - US
  • Uruguay Team
  • Utah Team - US
  • Virginia Team - US
  • West Virginia Team - US
  • Australia Team
  • Bangladesh Team
  • Hong Kong Team
  • Myanmar Team
  • Philippine Team
  • Singapore Team
  • Albania Team
  • Catalan Team
  • Portugal Team
  • Georgia Team
  • Ireland Team - Ireland
  • Kenyan Team - Kenya
  • Kurdish Team - Kurdistan
  • Lebanon Team
  • Morocco Team
  • Saudi Arabia Team
  • Tunisia Team
  • Other Forums & Teams
  • Afghanistan Team
  • Alabama Team - US
  • Alaska Team - US
  • Algerian Team
  • Andhra Pradesh Team - India
  • Austria Team
  • Bangalore Team
  • Bolivia Team
  • Cameroon Team
  • Colorado Team - US
  • Connecticut Team
  • Costa Rica Team
  • Ecuador Team
  • El Salvador Team
  • Florida Team - US
  • Galician LoCo Team
  • Hawaii Team - US
  • Honduras Team
  • Idaho Team - US
  • Iowa Team - US
  • Jordan Team
  • Kansas Team - US
  • Louisiana Team - US
  • Maryland Team - US
  • Massachusetts Team
  • Michigan Team - US
  • Missouri Team - US
  • Montana Team - US
  • Namibia Team
  • Nevada Team - US
  • New Hampshire Team - US
  • New Jersey Team - US
  • Northeastern Team - US
  • Panama Team
  • Paraguay Team
  • Quebec Team
  • Rhode Island Team - US
  • Senegal Team
  • South Carolina Team - US
  • South Dakota Team - US
  • Switzerland Team
  • Tamil Team - India
  • Tennessee Team - US
  • Trinidad & Tobago Team
  • Uganda Team
  • United Kingdom Team
  • US LoCo Teams
  • Venezuela Team
  • Washington DC Team - US
  • Washington State Team - US
  • Wisconsin Team
  • Za Team - South Africa
  • Zimbabwe Team

Tags for this Thread

View Tag Cloud

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is Off
  • HTML code is Off
  • Ubuntu Forums

Home Posts Topics Members FAQ

New Member |Select|Wrap|Line Numbers It comes from the line 32. Any help that helps me fix this problem and understand why it has arisen is appreciated.

Thanks 3599 Recognized Expert Top Contributor
Message
 

Sign in to post your reply or Sign up for a free account.

Similar topics

| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:
| last post by:

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use .

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.

Solving Assignment Makes Integer from Pointer Without a Cast in C: Tips and Solutions

David Henegar

As a developer, encountering errors while coding is inevitable. One common error that C programmers come across is the "assignment makes integer from pointer without a cast" error. This error message can be frustrating and time-consuming to resolve, but with the right tips and solutions, it can be easily fixed.

Understanding the Error Message

Before we dive into the tips and solutions for fixing this error, let's first understand what it means. The "assignment makes integer from pointer without a cast" error occurs when a pointer is assigned to an integer without a proper type cast. This error message is often accompanied by a warning message that looks like this:

This warning message is telling the programmer that the code is trying to assign a pointer value to an integer variable without casting the pointer to the correct type.

Tips for Fixing the Error

Here are some tips to help you fix the "assignment makes integer from pointer without a cast" error:

Tip #1: Check Your Pointer Types

Make sure that the pointer you are trying to assign to an integer variable is of the correct data type. If the pointer is pointing to a different data type, you will need to cast it to the correct type before assigning it to the integer variable.

Tip #2: Use the Correct Syntax

When casting a pointer to a different data type, make sure to use the correct syntax. The syntax for casting a pointer to an integer is (int) pointer .

Tip #3: Use the Correct Assignment Operator

Make sure that you are using the correct assignment operator. The assignment operator for pointers is = while the assignment operator for integers is == .

Tip #4: Check Your Code for Errors

Double-check your code for errors. Sometimes, the "assignment makes integer from pointer without a cast" error can be caused by a syntax error or a missing semicolon.

Solutions for Fixing the Error

Now that you have some tips for fixing the "assignment makes integer from pointer without a cast" error, let's look at some solutions.

Solution #1: Cast the Pointer to the Correct Type

To fix this error, you need to cast the pointer to the correct type before assigning it to the integer variable. Here's an example:

In this example, the pointer is cast to an integer using the (int) syntax before it is assigned to the num variable.

Solution #2: Declare the Integer Variable as a Pointer

Another solution is to declare the integer variable as a pointer. Here's an example:

In this example, the num variable is declared as a pointer, and the ptr variable is assigned to it without casting.

Q1: What causes the "assignment makes integer from pointer without a cast" error?

A: This error occurs when a pointer is assigned to an integer variable without being cast to the correct data type.

Q2: How do I cast a pointer to an integer in C?

A: To cast a pointer to an integer in C, use the (int) syntax.

Q3: Why is my code still giving me the same error message even after I cast the pointer to the correct type?

A: Double-check your code for syntax errors and missing semicolons. Sometimes, these errors can cause the same error message to appear even after you have cast the pointer to the correct type.

Q4: Can I declare the integer variable as a pointer to fix this error?

A: Yes, you can declare the integer variable as a pointer to fix this error.

Q5: What is the correct assignment operator for pointers and integers in C?

A: The assignment operator for pointers is = while the assignment operator for integers is == .

The "assignment makes integer from pointer without a cast" error can be frustrating, but with the right tips and solutions, it can be easily fixed. By understanding the error message and following the tips and solutions provided in this guide, you can resolve this error and improve the functionality of your code.

Related Links

assignment pointer from integer without a cast

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Your link has expired.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.

Get the Reddit app

A subreddit for all questions related to programming in any language.

assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast.

I’m new to C language:

include "main.h”

int func_d(va_list li) { char *p; int *num; int i, count = 0;

Then when I compiled this shows up:

assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion] 17 | p = itoa(num); | ^

By continuing, you agree to our User Agreement and acknowledge that you understand the Privacy Policy .

Enter the 6-digit code from your authenticator app

You’ve set up two-factor authentication for this account.

Enter a 6-digit backup code

Create your username and password.

Reddit is anonymous, so your username is what you’ll go by here. Choose wisely—because once you get a name, you can’t change it.

Reset your password

Enter your email address or username and we’ll send you a link to reset your password

Check your inbox

An email with a link to reset your password was sent to the email address associated with your account

Choose a Reddit account to continue

C Board

  • 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

Home

  • General Programming Boards
  • C Programming

"Assignment makes integer from pointer 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 integer from pointer 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

kirani is offline

I am working on a code that reads a CSV file and sorts it into separate arrays. I'm not sure what I'm doing wrong, but I'm getting "assignment makes integer from pointer without a cast" at each first line of the internal while loops (the ones that only have "{" in them). Anyone have a clue what I'm doing wrong? Code: /*read file*/ char arra [2000] [100] ; char line [2000]; for( row = 0; row < 2000; row++ ) for( column = 0; column < 100; column++ ) arra [row] [column] = '\0' ; for (row = 0; row < 5; row++) line [row] = '\0' ; if ( fp != NULL) { while (fgets (line, sizeof line, fp ) != NULL) /*read a line*/ { strcpy (arra[row], line) ; /*printf ("array --> %s", &arra[row]) ;*/ row++; } /*split up rows into separate arrays*/ char codes [2000][4] ; char names [2000][15] ; char alts [2000][10]; char lats [2000][10] ; char longis [2000][10] ; char states [2000][2]; int character = 0 ; char temp ; char *check ; char *comma ; row = 1 ; check = arra[row,character] ; comma = ',' ; for ( row = 1; row <2000 ; row ++) while ( strcmp ( check, comma ) != 0 ) { temp = arra [row, character] ; codes [row, character ] = temp ; character++ ; check = arra[row,character] ; } while ( strcmp ( check, comma ) != 0 ) { temp = arra [row, character] ; names[row][character] = temp ; character++ ; check = arra[row,character] ; } while ( strcmp ( check, comma ) != 0 ) { temp = arra [row, character] ; states[row][character] = temp ; character++ ; check = arra[row,character] ; } while ( strcmp ( check, comma ) != 0 ) { temp = arra [row, character] ; lats[row][character] = temp ; character++ ; check = arra[row,character] ; } while ( strcmp ( check, comma ) != 0 ) { temp = arra [row, character] ; longis[row][character] = temp ; character++ ; check = arra[row,character] ; } while ( strcmp ( check, '\n' ) != 0 ) { temp = arra [row, character] ; alts[row][character] = temp ; character++ ; check = arra[row,character] ; } fclose(fp) ; } else { perror( filename) ; }

Adak is offline

This is not good syntax in C: check = arra[row,character] ; //not OK check = arra[row][character]; //OK And I wouldn't use strcmp() to find your comma's. Just while(array[row][char++] != ','); should do.
Thanks for the heads up on the syntax, I'm still pretty new to C. I corrected as per your suggestions, but I'm getting the same error, now on the line in each while loop that reassigns the check variable.
I removed the pointer "*" from the check variable and it compiles, but returns a segmentation fault when i run the output file.

nonoob is offline

The comma pointer doesn't point to anywhere. Remove the '*' from that.
for every row, for every column, for every row, you're creating: Code: /*split up rows into separate arrays*/ char codes [2000][4] ; char names [2000][15] ; char alts [2000][10]; char lats [2000][10] ; char longis [2000][10] ; char states [2000][2]; int character = 0 ; char temp ; char *check ; char *comma ; statically. First, I believe this is far more arrays than you actually need, or intended, and second, it's more than you can create on the stack. From your input, how many names[] arrays, do you actually need? (in total). I want the number, not the description, and I want to know what arithmetic you used, to find that number, in a simple equation, like this: "Example: There are 2,000 rows, each row has 3 names in it: Number of names[] arrays = 2,000 * 3 Do that math, and adjust your program. Then repost your current code in this function AND post up a 10 line sample of data input, and data output after this function (or block of code within this function). Since I don't know exactly what you're doing with this data, I usually defer the number of data structures, to the student's good judgment. Here, your judgment on the number of data structures, has jumped the rails, I suspect.
  • 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

subscribe to a feed

  • 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

Help assignment due tomorrow, warning: passing argument 1 makes pointer from integer without a cast, "assignment makes integer from pointer without a cast", assignment makes pointer from integer, warning: assignment makes integer from pointer without a cast, tags for this thread.

View Tag Cloud

  • C and C++ Programming at Cprogramming.com
  • Web Hosting
  • Privacy Statement

IMAGES

  1. Array : Assignment makes integer from pointer without a cast [-Wint-conversion

    assignment pointer from integer without a cast

  2. [Solved] Assignment makes integer from pointer without a

    assignment pointer from integer without a cast

  3. Assignment makes integer from pointer without a cast in c

    assignment pointer from integer without a cast

  4. Makes Pointer From Integer Without a Cast: Fix It Now!

    assignment pointer from integer without a cast

  5. Solved error: assignment makes pointer from integer without

    assignment pointer from integer without a cast

  6. Making An Integer From A Pointer Without A Cast: A Comprehensive Guide

    assignment pointer from integer without a cast

VIDEO

  1. Casting Base Class Pointer to Derived Class Pointer

  2. Core Concept of Integer in "C" #cprogramming #cpp #codemantra #coding #programming #clanguage

  3. Pointers in C++ 01, Prerequisites, How to declare a pointer? Pointer and Reference Operators

  4. Arrays & Pointers in C Programming Explained

  5. Double pointer in C

  6. 0x06 C

COMMENTS

  1. ctypes

    A return type of ctypes.c_void_p is also "helpful" and converts the returned C address to a Python integer, but can be cast a more specific pointer type to access the data at the address. To find it's address, use ctypes.addressof on the contents of the pointer; otherwise you get the address of the storage of the pointer.

  2. warning: assignment makes integer from pointer without a cast

    The warning comes from the fact that you're dereferencing src in the assignment. The expression *src has type char, which is an integral type.The expression "anotherstring" has type char [14], which in this particular context is implicitly converted to type char *, and its value is the address of the first character in the array.So, you wind up trying to assign a pointer value to an integral ...

  3. c

    1. Earlier, I asked a similar question, but I've since changed my code. Now the compiler gives me a different warning. This is an example of what my code looks like now: void *a = NULL; void *b = //something; a = *(int *)((char *)b + 4); When I try to compile, I get "warning: assignment makes pointer from integer without a cast."

  4. C

    3. result = s1. result is an int, integer. s1 is a char const*, a const pointer to char. You can't assign the value of a const char* to an integer (because they might not have the same size for example). You can however, as hinted by the warning, cast the value to the correct type to force it to stop warning you: result = (int)s1;

  5. Assignment makes integer from pointer without a cast in c

    Case 1: Assignment of a pointer to an integer variable. n1 = 2; ptr = &n1; n2 = ptr; /* Failure in this line */. In this simple code we have three variables, an integer pointer "ptr", and two ...

  6. Makes Integer From Pointer Without A Cast (Resolved)

    Converting Integers to Pointers {#converting-integers-to-pointers} To convert an integer to a pointer, follow these steps: Include the <stdint.h> header (C) or the <cstdint> header (C++) in your program. Cast your integer to the required pointer type using a double cast. int a = 42; uintptr_t int_ptr = (uintptr_t)&a;

  7. Assignment makes integer from pointer without a cast and program

    OK - in that case, since you want to immediately change the value of sad, it can be a local variable to the function, and doesn't need to be an argument to the function: . void racun(int D, int M, int G, int *dUg) { int i, *sad; If you want sad to point to an array, such that accessing sad (sad[...]) will be equivalent to accessing that array (e.g. obicna[...]), you can simply assign the array ...

  8. assignment makes integer from pointer without a cast

    text should be declared as: char *text = NULL; You need a pointer to char. This pointer can be set to point to any literal string (as you do in your case statements). char text; // this is just a single character (letter), not a string. 2. Objective_Ad_4587 • 3 yr. ago. i got it thank you very much.

  9. C: warning assignment makes integer from pointer without a cast

    Related to C: warning assignment makes integer from pointer without a cast 1. What does the warning "assignment makes integer from pointer without a cast" mean? This warning indicates that a pointer value is being assigned to an integer variable without being explicitly converted or casted. This can create unexpected behavior and should be avoided.

  10. Initialization Makes Pointer From Integer Without A Cast (Resolved)

    Understand its causes and solution in this guide. (initialization makes pointer from integer without a cast) ... Check if the variable on the left-hand side of the assignment operator is a pointer variable. If it is not a pointer variable, you need to declare it as a pointer variable.

  11. Makes Pointer From Integer Without a Cast: Fix It Now!

    How To Stop a Pointer Creation From an Integer Without a Cast. - Use Equal Data Types During Assignment. - Ensure the Pointer and Integer Have the Same Sizes. - Pass a "Format String" to the "Printf ()" Function. - Use a Function That Returns a Pointer to "Struct _IO_file". - Copy the String to Character Array or Character ...

  12. [SOLVED] C

    Re: C - assigment makes integer from pointer without a cast warning. path [1] is the second element of a char array, so it's a char. home is a char *, i.e. a pointer to a char. You get the warning because you try to assign a char* to a char. Note also the potential to overflow your buffer if the content of the argv [1] argument is very long.

  13. Assignment makes pointer from integer without a cast

    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

  14. Need help with C, keep getting "assignment makes pointer from integer

    char *word is a pointer, meaning the value contained by the word parameter is a memory address (in this example, it will be the address of the first character in a string). When you use the notation *word on line 19, you are dereferencing the word pointer, meaning it will return the value of whatever is stored at word's memory address (which is a single character).

  15. [Warning]assignment makes integer from pointer without a cast

    warning: assignment makes pointer from integer without a cast I get that when I compile a C file where the function is defined as this: char **getvars () and the calling function has this declared: char **s; and I write: s=getvars (); C / C++. 1. 3858. warning assignment makes integer from pointer without a cast.

  16. assignment makes integer from pointer without a cast [enabled by

    This is my last program, and I have done quite well with the other 15. Errors: |43|warning: assignment makes integer from pointer without a cast [enabled by default]| |44|warning: assignment makes integer from pointer without a cast [enabled by default]| |46|error: incompatible types when assigning to type 'int [100]' from type 'int *'| |47 ...

  17. Assignment Makes Integer From Pointer Without A Cast In C (Resolved)

    Solving Assignment Makes Integer from Pointer Without a Cast in C: Tips and Solutions

  18. assignment to 'char *' from 'int' makes pointer from integer without a

    p = itoa (num); You probably want: p = itoa (*num); Since otherwise you're sending a pointer to an int, and not the int. But I don't think itoa () even works that way. You'd also have to send in the string buffer. You should go check out a reference. 1. I'm new to C language: include "main.h" int func_d (va_list li) { char *p; int *num; int ...

  19. assignment makes integer from pointer without a cast

    assignment makes integer from pointer without a castc/c++ warning explained#syntax #c/c++ #compiler #error #warning

  20. "Assignment makes integer from pointer without a cast"

    I'm not sure what I'm doing wrong, but I'm getting "assignment makes integer from pointer without a cast" at each first line of the internal while loops (the ones that only have " {" in them). Anyone have a clue what I'm doing wrong? And I wouldn't use strcmp () to find your comma's. Just.