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 Mythbuntu
  • Get Edubuntu
  • Get Ubuntu GNOME
  • 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

Thread: [solved] c - assigment makes integer from pointer without a cast warning, thread tools.

  • Show Printable Version
  • Subscribe to this Thread…
  • 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

gcc assignment makes pointer from integer without a cast

Enabled by default warnings

The GNU Compiler Collection (GCC) 4.6.3 might generate enabled by default warnings that cannot be suppressed. To compile without the warnings, fix your code.

Enabled by default warnings for NULL

Problem: I received the following warning: return makes pointer from integer without a cast [enabled by default]

Solution: Investigate and fix code that generates this warning.

Problem: I received the following warning: assignment makes pointer from integer without a cast [enabled by default]

Solution: Identify and change coding errors such as casting the rvalue to an unsigned int when the lvalue is a pointer. This coding error might not be obvious in complex code; for example, when the code is in a segment that uses a C macro embedded in another C macro.

Problem: I received the following warning: comparison between pointer and integer [enabled by default]

Solution: Identify and change coding errors such as casting the rvalue to an unsigned int when the lvalue is a pointer.

Enabled by default warning for initialized extern variables

Problem: I received the following warning: 'REGISTER_NAMES' initialized and declared 'extern' [enabled by default]

Investigate and fix any code that has extern variables that are initialized, by removing the initialization or by removing the extern storage class specifier.

Enabled by default warning for undeclared extern variables

Problem: I received the following warning: type 'CMOA()::itcpc' with no linkage used to declare variable 'CMOA()::itcpc* itcpc_ptr' with linkage [enabled by default]

  • Create Account

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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.

C Board

  • 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 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…
  • View Profile
  • View Forum Posts

deciel is offline

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 .

JohnGraham is offline

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!

sparkomemphis is offline

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

Tclausex is offline

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

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

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

gcc assignment makes pointer from integer without a cast

Assignment makes pointer from integer without a cast

  • From : Lord Byron <lordbyronbr at yahoo dot com>
  • To : gcc at gcc dot gnu dot org
  • Date : Wed, 20 Mar 2002 05:16:25 -0800 (PST)
  • Subject : Assignment makes pointer from integer without a cast
  • Home New Posts Forum List Trending New Threads New Media Spy
  • WikiPost Latest summaries Watched WikiPosts
  • Support FAQ and Rules Contact Us

macOS   Assignment makes pointer from integer without a cast

  • Thread starter kubap007
  • Start date Jul 29, 2008
  • Sort by reaction score
  • Apple Programming

macrumors newbie

  • Jul 29, 2008

robbieduncan

robbieduncan

Moderator emeritus.

kubap007 said: Code: tmp = (@"%d\t", val); Click to expand...

gnasher729

kubap007 said: Hello! I know it is common problem but I read many posts and I still don't know what I'm doing wrong. That is my code: Code: ... short val; NSString *tmp; val = [[value valueForKey:@"stock"] shortValue]; // everything works alright I checked using NSLog(@"%d", val). I have e.g. 10. tmp = (@"%d\t", val); And I have warning "Assignment makes pointer from integer without a cast", but if I start program and using this function it turns off. I will be grateful if someone will help me Click to expand...

macrumors 6502a

gnasher729 said: The warning means exactly what it says: You are assigning a value. The value that you are assigning is an integer. The variable that you are assigning to is a pointer. That deserves a warning (actually, gcc is a bit stupid. This shouldn't be a warning, it should be a hard error). Click to expand...

MiniMacLean

  • Nov 24, 2008

sorry if i have bumped the thread a bit but, i got an integer declared in viewDidLoad Code: int *TabNumber; and then when a section of a UISegmentedControl is pressed i want that integer to change to 1 so i did this Code: TabNumber = 1; however it comes up with the assignment makes pointer from integer without a cast error, how do i fix it, i have read the post above but i dont see how it relates to this  

MiniMacLean said: sorry if i have bumped the thread a bit but, i got an integer declared in viewDidLoad Code: int *TabNumber; Click to expand...

d'ya mean if i delcare the int in the .h like Code: int *TabNumber; because i have also done that, if not what do you mean?  

MiniMacLean said: d'ya mean if i delcare the int in the .h like Code: int *TabNumber; because i have also done that, if not what do you mean? Click to expand...

lee1210

macrumors 68040

int * is a fundamentally different type than int. If you declared TabNumber: Code: int TabNumber; it would be an int, and you could do: Code: TabNumber = 1; You declared it as an int *: Code: int *TabNumber; So to store something in it, you need to get some memory for it: Code: TabNumber = (int *)malloc(sizeof(int)); *TabNumber = 1; If you don't yet know the difference between a pointer and a concrete variable (no offense intended, i'm just truly not sure if this is something you've learned yet), you should find out before proceeding. -Lee Edit: robbieduncan beat me to the punch, and expressed about the same thing.  

lee1210 said: Edit: robbieduncan beat me to the punch, and expressed about the same thing. Click to expand...

i know you will tell me to read up more about basic C language but i want to be able to use this integer in more than one method. But when doing this it says warning:local declaration of TabNumber hides instance variable sorry if i am being really stupid but could you please explain this warning  

Cromulent

macrumors 604

MiniMacLean said: i know you will tell me to read up more about basic C language but i want to be able to use this integer in more than one method. But when doing this it says warning:local declaration of TabNumber hides instance variable sorry if i am being really stupid but could you please explain this warning Click to expand...

We got a bit off track to try to teach you something rather than just giving you an answer. To declare TabNumber as an int, just take the * out of your declaration. -Lee  

lee1210 said: We got a bit off track to try to teach you something rather than just giving you an answer. To declare TabNumber as an int, just take the * out of your declaration. -Lee Click to expand...

notjustjay

macrumors 603

I strongly recommend Practical C Programming published by O'Reilly and Associates -- the so-called "cow book". If you don't want to buy a copy, find it at a library if you can. I was able to teach myself C programming using nothing but this book as a guide, and object-oriented programming (C++) came quickly afterward. It is THE best C book I have ever read.  

  • Nov 25, 2008
notjustjay said: I strongly recommend Practical C Programming published by O'Reilly and Associates -- the so-called "cow book". If you don't want to buy a copy, find it at a library if you can. I was able to teach myself C programming using nothing but this book as a guide, and object-oriented programming (C++) came quickly afterward. It is THE best C book I have ever read. Click to expand...

Position Is Everything

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

  • Recent Posts

Melvin Nolan

  • The HTML Style Element – Learn How to Improve Your Website’s Look - February 24, 2024
  • Creating an HTML New Line – Step by Step Tutorial On Simple Syntax - February 24, 2024
  • HTML Header Element – The Basis for Any HTML Document - February 24, 2024

This post may contain affiliate links. If you make a purchase through links on our site, we may earn a commission.

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.

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.

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.

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:

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:

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.

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.

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.

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

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

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.

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.

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.

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:

  • Getaddrinfo ENOTFOUND: Get a Premium Solution for the Error
  • Enoent: Best Techniques To Solve the “Enoent” Error
  • Exception in Application Start Method Java.lang.reflect.invocationtargetexception: Fixed
  • Ora-12514: Solve the Listener Error When Connecting to Oracle
  • The Superclass “javax.servlet.HTTP.httpservlet” Was Not Found on the Java Build Path
  • Possible Lossy Conversion From Double to Int: Fast Solutions
  • Only Integer Scalar Arrays Can Be Converted to a Scalar Index
  • Valueerror: All the Input Arrays Must Have Same Number of Dimensions
  • Error: Unknown Command: Cask: Step-by-Step Debugging Guide
  • Java Lang Noclassdeffounderror Could Not Initialize Class
  • No Match for Operator C++: Causes and the Best Solutios
  • Django.core.exceptions.ImproperlyConfigured: the SECRET_KEY Setting Must Not Be Empty.

Leave a Comment Cancel reply

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

What is Assignment Makes Pointer From Integer Without A Cast and How Can It Be Avoided?

Introduction.

Assignment Makes Pointer From Integer Without A Cast is a type of warning that is issued by the compiler when a pointer is assigned an integer value without a cast. This warning is issued to alert the programmer that the code may be vulnerable to errors and should be reviewed. This warning is usually issued when a pointer is assigned an integer value without a cast, which can lead to unexpected behavior and potential security issues. This article will discuss the warning, its implications, and how to avoid it.

Assignment Makes Pointer From Integer Without A Cast is a type of warning that is generated by the compiler when a pointer is assigned a value that is not a pointer. This warning is generated when a pointer is assigned an integer value, which is not a valid pointer. This warning is generated to alert the programmer that the code may be incorrect and could lead to unexpected results.

To avoid this warning , the programmer should ensure that the pointer is assigned a valid pointer value. This can be done by using the appropriate typecasting operator to convert the integer value to a pointer. Additionally, the programmer should ensure that the pointer is assigned a valid address before it is used. This can be done by using the appropriate memory allocation functions such as malloc() or calloc(). Finally, the programmer should ensure that the pointer is properly deallocated when it is no longer needed. This can be done by using the appropriate memory deallocation functions such as free() or delete().

Understanding the Dangers of Assignment Makes Pointer From Integer Without A Cast

When programming in C or C++, it is important to understand the dangers of assigning a pointer from an integer without a cast. This type of assignment can lead to unexpected and potentially dangerous results.

When assigning a pointer from an integer without a cast, the compiler will assume that the integer is a pointer and will attempt to interpret it as such. This can lead to a number of issues, including memory corruption, segmentation faults, and other errors. In some cases, the compiler may even attempt to dereference the integer, which can lead to a crash or other unexpected behavior.

In addition to the potential for errors, assigning a pointer from an integer without a cast can also lead to security vulnerabilities. If the integer is not properly validated, it is possible for an attacker to inject malicious code into the program. This can lead to a variety of security issues, including data leakage, privilege escalation, and other malicious activities.

To avoid these issues , it is important to always use a cast when assigning a pointer from an integer. This will ensure that the compiler interprets the integer as an integer and not as a pointer. Additionally, it is important to validate any integers that are used in pointer assignments to ensure that they are valid and not malicious.

By understanding the dangers of assigning a pointer from an integer without a cast, programmers can ensure that their programs are secure and free from unexpected errors.

Common Causes of Assignment Makes Pointer From Integer Without A Cast

Assignment makes pointer from integer without a cast is a common error that occurs when a programmer attempts to assign an integer value to a pointer. This error occurs when a programmer attempts to assign an integer value to a pointer without first casting the integer to a pointer type. This is an illegal operation in C and C++ programming languages, as the two types are incompatible.

When this error occurs, the compiler will usually generate a warning message that reads something like “ assignment makes pointer from integer without a cast ”. This warning is generated to alert the programmer that they are attempting to assign an integer value to a pointer without first casting the integer to a pointer type.

The most common cause of this error is when a programmer attempts to assign an integer value to a pointer without first casting the integer to a pointer type. This is an illegal operation in C and C++ programming languages, as the two types are incompatible.

Another common cause of this error is when a programmer attempts to assign a pointer to an integer without first casting the pointer to an integer type. This is also an illegal operation in C and C++ programming languages, as the two types are incompatible.

In order to avoid this error , it is important for programmers to ensure that they are assigning the correct type of value to a pointer. If a programmer is unsure of the type of value they are attempting to assign, they should consult the documentation for the language they are using. Additionally, it is important for programmers to ensure that they are casting the correct type of value when assigning a value to a pointer.

How to Debug Assignment Makes Pointer From Integer Without A Cast Errors

Debugging an assignment makes pointer from integer without a cast error can be a difficult task. This type of error occurs when a pointer is assigned an integer value, which is not allowed in C programming. To debug this error, it is important to understand the source of the error and the context in which it occurs.

First, it is important to identify the line of code that is causing the error. This can be done by examining the compiler output, which will indicate the line of code that is causing the error. Once the line of code has been identified, it is important to examine the code to determine why the error is occurring.

The most common cause of this error is when a pointer is assigned an integer value. This is not allowed in C programming, as pointers must be assigned a valid memory address. To fix this error, the code must be modified so that the pointer is assigned a valid memory address.

It is also important to check for any other errors in the code that may be causing the assignment makes pointer from integer without a cast error. This can be done by examining the code for any other errors that may be causing the error. If any other errors are found, they must be fixed before the assignment makes pointer from integer without a cast error can be fixed.

Finally, it is important to test the code after the changes have been made to ensure that the error has been fixed. This can be done by running the code and checking for any errors that may still be present. If any errors are still present, they must be fixed before the code can be used.

By following these steps, it is possible to debug an assignment makes pointer from integer without a cast error. It is important to understand the source of the error and the context in which it occurs, as well as to check for any other errors that may be causing the error. Once these steps have been taken, the code can be tested to ensure that the error has been fixed.

Best Practices for Avoiding Assignment Makes Pointer From Integer Without A Cast

1. Understand the Difference Between Pointers and Integers : It is important to understand the difference between pointers and integers. Pointers are variables that store the address of another variable, while integers are variables that store numerical values.

2. Use Explicit Typecasting : When assigning a pointer from an integer, it is important to use explicit typecasting. This will ensure that the compiler knows the type of data that is being assigned and will prevent any potential errors.

3. Use Appropriate Data Types : When declaring variables, it is important to use the appropriate data type. For example, if a variable is intended to store an address, it should be declared as a pointer.

4. Use Appropriate Operators: When performing operations on pointers and integers, it is important to use the appropriate operators. For example, when performing arithmetic operations on pointers, the addition and subtraction operators should be used instead of the multiplication and division operators.

5. Use Appropriate Functions : When performing operations on pointers and integers, it is important to use the appropriate functions. For example, when performing arithmetic operations on pointers, the appropriate pointer arithmetic functions should be used instead of the standard arithmetic functions.

6. Use Appropriate Libraries : When performing operations on pointers and integers, it is important to use the appropriate libraries. For example, when performing arithmetic operations on pointers, the appropriate pointer arithmetic libraries should be used instead of the standard arithmetic libraries.

7. Use Appropriate Compiler Flags: When compiling code, it is important to use the appropriate compiler flags. For example, when compiling code that uses pointers, the -Wpointer-arith flag should be used to enable warnings about pointer arithmetic.

8. Use Appropriate Debugging Tools: When debugging code, it is important to use the appropriate debugging tools. For example, when debugging code that uses pointers, the appropriate pointer debugging tools should be used instead of the standard debugging tools.

Exploring the Impact of Assignment Makes Pointer From Integer Without A Cast on Performance

The impact of assignment makes pointer from integer without a cast on performance is an important consideration for software developers. This type of assignment, also known as a “ pointer cast ”, is a common programming practice that can have a significant effect on the performance of a program.

When a pointer cast is used, a pointer is created from an integer value. This is done by assigning the integer value to a pointer variable. This type of assignment can be used to access memory locations that are not directly accessible by the program. It can also be used to access data stored in memory that is not directly accessible by the program.

However, this type of assignment can have a negative impact on performance. When a pointer cast is used, the program must perform additional calculations to determine the memory address of the data being accessed. This can lead to increased memory usage and slower execution times. Additionally, the program must also perform additional calculations to ensure that the data being accessed is valid. This can lead to additional overhead and slower execution times.

In addition to the performance impact, pointer casts can also lead to security vulnerabilities. If the pointer cast is not properly validated, it can be used to access memory locations that are not intended to be accessed. This can lead to data corruption or even malicious code execution.

For these reasons, it is important for software developers to consider the impact of assignment makes pointer from integer without a cast on performance. When used properly, pointer casts can be a useful tool for accessing data stored in memory. However, it is important to ensure that the pointer cast is properly validated and that the data being accessed is valid. Additionally, it is important to consider the performance impact of using pointer casts and to ensure that the program is optimized for the best possible performance.

1. What is an assignment makes pointer from integer without a cast?

An assignment makes pointer from integer without a cast is a type of programming error that occurs when a pointer is assigned a value that is not a valid memory address. This can lead to unexpected behavior and can cause a program to crash.

2. What are the consequences of an assignment makes pointer from integer without a cast?

The consequences of an assignment makes pointer from integer without a cast can be severe. It can lead to unexpected behavior, memory corruption, and program crashes.

3. How can an assignment makes pointer from integer without a cast be prevented?

An assignment makes pointer from integer without a cast can be prevented by ensuring that all pointers are assigned valid memory addresses. This can be done by using the appropriate typecasting functions or by using the correct data types when assigning values to pointers.

4. What are some common causes of an assignment makes pointer from integer without a cast?

Common causes of an assignment makes pointer from integer without a cast include incorrect typecasting, incorrect data types, and incorrect pointer assignments.

5. What are some best practices for avoiding an assignment makes pointer from integer without a cast?

Some best practices for avoiding an assignment makes pointer from integer without a cast include using the correct typecasting functions, using the correct data types, and ensuring that all pointers are assigned valid memory addresses.

admin

  • Previous Your Network Preference Prevent Content From Loading
  • Next The Supplied Icloud Was Unable To Unlock This Volume

Your Party Is Trying To Talk To You Fortnite Xbox

Your Party Is Trying To Talk To You Fortnite Xbox

Precision Tree Free Download

Precision Tree Free Download

How to Enable Bootup Logging to View What Did and Did Not Load

How to Enable Bootup Logging to View What Did and Did Not Load

Your email address will not be published. Required fields are marked *

  • About JOE TECH
  • Privacy Policy

gcc_warning:assignment makes integer from pointer without a cast

gcc assignment makes pointer from integer without a cast

    在使用gcc对源文件进行编译的时候,往往会出现各种各样的警告或者错误。如果知道这些警告或者错误的意思,就方便我们去对程序进行修改。然而,由于这些警告和错误都是英文了,而且有很多是晦涩难懂的,在此有必要做一下总结。

   warning: assignment makes integer from pointer without a cast [enabled by default]

    百度翻译:分配使得整数指针没有投。

    有道翻译:作业使整数指针不投。

    以上两个词典的翻译结果都是狗屁不通的,根本没有办法理解。那么,唯有从源代码中分析问题,然后根据问题去理解这句话的意思了。

代码段1:

char *c="hello world";

 *c = "hello";//赋值一个字符串

编译代码段1时,提示: warning: assignment makes integer from pointer without a cast [enabled by default]

执行代码段1时,提示:Segmentation fault (core dumped)

代码段2:

 *c = 123;//赋值一个整数

编译代码段2时,提示:没有任何提示

执行代码段2时,提示:Segmentation fault (core dumped)

代码段3:

char *c=""hello world";

 *c = 't';//赋值一个字符

编译代码段3时,提示:没有任何提示

执行代码段3时,提示:Segmentation fault (core dumped)

代码段4:

编译代码段4时,提示:没有任何提示

执行代码段4时,提示:Segmentation fault (core dumped)

代码段5:

编译代码段5时,提示:没有任何提示

执行代码段5时,提示:Segmentation fault (core dumped)

代码段6:

 *c = "t";//赋值一个字符串

编译代码段6时,提示: warning: assignment makes integer from pointer without a cast [enabled by default]

执行代码段6时,提示:Segmentation fault (core dumped)

    分析对比上述的6个代码段可知,不管char *c有无被初始化(指向确定的地址),*c接受int型,字符型的输入时,都不会出现warning(char 和int都可以理解为整型);但是,只要*c接受字符串型的输入,就会提示warning!

    由于字符窜在c中通常是以其首地址来表示,因此将“hello world”赋值给*c,其实是将其首地址赋值给*c。而*c只接受整型的输入,因此默认情况下,将这个地址(char *型)转换为整型(int 型)。

    根据上面的提示,可以再设计一段代码,如下面代码7所示。

代码段7:

 c = “hello”;//赋值一个字符串

编译代码段7时,提示: warning: assignment makes integer from pointer without a cast [enabled by default]

执行代码段7时,提示:没有任何提示。

    由代码段7可知,其实这个warning根本就是由于变量的类型(char)和赋值的类型(字符串)不匹配所导致的!类型不匹配时,自然就采用默认的类型转换方式[enabled by default]。

代码段8:

 c = (char)“hello”;//赋值一个字符串

编译代码段8时,提示: warning:cast from pointer to integer of different size [-Wpointer-to-int-cast]

执行代码段8时,提示:没有任何提示。

代码段9:

char *c=0;

编译代码段9时,提示:warning: initialization makes pointer from integer without a cast [enabled by default]

执行代码段9时,提示:没有任何提示。

代码段10:

char *c=(char *)0;

编译代码段10时,提示:没有警告!

执行代码段10时,提示:没有任何提示。

    结论:根据以上分析,可以知道warning的意思是:赋值由 指针 制造了一个整数,并且没有 强制类型转换 [使用 默认类型转换 ]。 cast在这里表示强制类型转换 !

    拓展:代码段1~6运行都会出现错误,说明对没有初始化的指针所指向的地址赋值是不允许的;如果一个指针初始化为为字符串,同样不可以再对它进行修改!

gcc assignment makes pointer from integer without a cast

“相关推荐”对你有帮助么?

gcc assignment makes pointer from integer without a cast

请填写红包祝福语或标题

gcc assignment makes pointer from integer without a cast

你的鼓励将是我创作的最大动力

gcc assignment makes pointer from integer without a cast

您的余额不足,请更换扫码支付或 充值

gcc assignment makes pointer from integer without a cast

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

gcc assignment makes pointer from integer without a cast

IMAGES

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

    gcc assignment makes pointer from integer without a cast

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

    gcc assignment makes pointer from integer without a cast

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

    gcc assignment makes pointer from integer without a cast

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

    gcc assignment makes pointer from integer without a cast

  5. Assignment makes pointer from integer without a cast by Contreras

    gcc assignment makes pointer from integer without a cast

  6. Assignment makes integer from pointer without a cast by Johnson Jessica

    gcc assignment makes pointer from integer without a cast

VIDEO

  1. Convex Set in Hindi

  2. Un si grand soleil : catastrophe pour Manu (Moïse Santamaria), France 2 sanctionnée

  3. Extra Problems 1 Pointers double pointers tracing inside function

  4. Gempa bumi 6,2 Magnitude Earthquake Strikes Sulawesi, Indonesia

  5. MODULE 5

  6. Excel VBA CLngPtr CONVERT TO LONG INTEGER POINTER

COMMENTS

  1. C pointers and arrays: [Warning] assignment makes pointer from integer

    C pointers and arrays: [Warning] assignment makes pointer from integer without a cast - Stack Overflow C pointers and arrays: [Warning] assignment makes pointer from integer without a cast [closed] Ask Question Asked 10 years ago Modified 4 years, 11 months ago Viewed 269k times 31 Closed. This question is not reproducible or was caused by typos.

  2. [SOLVED] C

    [SOLVED] C - assigment makes integer from pointer without a cast warning 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:

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

    Assignment makes integer from pointer without a cast in c Coder Legion · Follow 5 min read · Oct 4, 2023 Programming can be both rewarding and challenging. You work hard on your code,...

  4. Enabled by default

    Problem: I received the following warning: assignment makes pointer from integer without a cast [enabled by default] Solution: Identify and change coding errors such as casting the rvalue to an unsigned int when the lvalue is a pointer.

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

    The following code in c+ gives me the warning assignment makes integer from pointer without a cast. destination is set as char destination to limit the input string to 10 characters. name is an... C / C++. 1. Access Europe Meeting - Wed 6 Dec - Streamline Your Import/Export Spec Workflow with VBA Classes.

  6. C error

    jharvard@appliance (~/Dropbox/prg): gcc n.c -o n n.c: In function 'main': n.c:14:18: warning: assignment makes integer from pointer without a cast [enabled by default] firstInitial = "J"; ^ n.c:15:19: warning: assignment makes integer from pointer without a cast [enabled by default] secondInitial = "R"; ^

  7. Assignment makes pointer from integer without a cast

    Assignment makes pointer from integer without a cast 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?

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

    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: A character string literal has static storage duration and type "array of char", and is initialized with the given characters. A wide string literal has static storage duration and type "array of wchar_t", and is initialized with the wide characters corresponding to ...

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

    Instead, you need to use functions like strncpy and friends from <string.h> and use char arrays instead of char pointers. If you merely want the pointer to point to a different static string, then drop the *. The warning comes from the fact that you're dereferencing src in the assignment.

  10. assignment makes integer from pointer without a cast

    When compiling the code it gave warnings and errors: network.c:51:12: warning: assignment discards 'const' qualifier from pointer target type [enabled by default] network.c:60:12: warning: assignment makes integer from pointer without a cast [enabled by default] network.c:64:26: error: invalid type argument of unary '*' (have 'int ...

  11. Assignment makes pointer from integer without a cast

    This is the mail archive of the [email protected] mailing list ... Assignment makes pointer from integer without a cast. From: Lord Byron <lordbyronbr at yahoo dot com> To: gcc at gcc dot gnu dot org; Date: Wed, 20 Mar 2002 05:16:25 -0800 (PST) Subject: Assignment makes pointer from integer without a cast; I am using a function like this ...

  12. Assignment makes pointer from integer without a cast

    #1 Hello! I know it is common problem but I read many posts and I still don't know what I'm doing wrong. That is my code: Code: ... short val; NSString *tmp; val = [ [value valueForKey:@"stock"]...

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

    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:

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

  15. C

    Warning: assignment makes integer from pointer without a cast [enabled by default] This is my source code: int ft_replace (char const *s1) { int result; result = 0; for (; *s1 != '\0'; ++s1) { if (*s1 == '-') result = s1; // Warning here } return (result); }

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

    The mechanism to compute the value to put into the start pointer could be just using a pointer value or computing an x-bit linear address from segment or page address + offset. Some systems have a DMA memory window register, so a subtraction is needed to go from the linear address to the value to write into the start address register.

  17. What is Assignment Makes Pointer From Integer Without A Cast and How

    This type of assignment can lead to unexpected and potentially dangerous results. When assigning a pointer from an integer without a cast, the compiler will assume that the integer is a pointer and will attempt to interpret it as such. This can lead to a number of issues, including memory corruption, segmentation faults, and other errors.

  18. C语言assignment makes pointer from integer without a cast

    C语言assignment makes pointer from integer without a cast 这个警告的意思是将一个int整数值直接赋值给了一个指针变量。 (重点是类型不一致)消除警告的方法就是明确类型转换是否是正确的,如果确实要把整数变量赋予指针变量,那么请使用强制类型转换。

  19. c

    2 Answers Sorted by: 1 Since, "the value stored at ( (char *)b + 4) is itself another pointer", simply add an explicit cast to the result: void *a = NULL; void *b = //something; a = (void*)* (int *) ( (char *)b + 4); Since you're assuming that sizeof (void*)==sizeof (int), the code is not portable, but I am sure you already knew that.

  20. gcc_warning:assignment makes integer from pointer without a cast

    编译的时候报警告:warning: assignment makes pointer from integer without a cast 出现这个警告的原因是在使用函数之前没有对函数进行声明,未经声明的函数原型一律默认为返回int值。这样、就相当于你调用了返回值为int的函数,并将其赋给了char*变量,所有会出现警告。 ...