Converting Pointers to Integers: Avoiding Cast Errors & Mastering the Process

David Henegar

In this guide, we will cover how to convert pointers to integers and vice versa without running into any cast errors. We will also walk you through the step-by-step process of mastering pointer and integer conversions in C/C++.

Table of Contents

  • Why Convert Pointers to Integers
  • Understanding uintptr_t
  • Step-by-Step Guide
  • Converting Pointers to Integers
  • Converting Integers to Pointers

Why Convert Pointers to Integers? {#why-convert-pointers-to-integers}

There are several use cases where you might need to convert pointers to integers and vice versa. Some common reasons include:

  • Manipulating memory addresses for low-level programming.
  • Serializing and deserializing data.
  • Storing pointers in a generic data structure.
  • Debugging and logging purposes.

However, when converting pointers to integers, it is crucial to avoid any errors that may arise from incorrect casting.

Understanding uintptr_t {#understanding-uintptr_t}

To safely convert pointers to integers, it is essential to use the uintptr_t data type. This is an unsigned integer type that is large enough to store the value of a pointer. It is available in the <stdint.h> header in C and the <cstdint> header in C++.

Using uintptr_t , you can safely cast a pointer to an integer and back to a pointer without losing any information. This ensures that the process is safe, fast, and efficient.

Step-by-Step Guide {#step-by-step-guide}

Converting pointers to integers {#converting-pointers-to-integers}.

To convert a pointer to an integer, follow these steps:

  • Include the <stdint.h> header (C) or the <cstdint> header (C++) in your program.
  • Cast your pointer to uintptr_t .

Converting Integers to Pointers {#converting-integers-to-pointers}

To convert an integer to a pointer, follow these steps:

  • Cast your integer to the required pointer type using a double cast.

FAQs {#faqs}

Why can't i just use a regular int or unsigned int to store pointers {#regular-int}.

While it may work on some platforms where the size of an int is equal to the size of a pointer, it is not guaranteed to be portable across different systems. Using uintptr_t ensures your code remains portable and safe.

Are there performance implications when using uintptr_t ? {#performance}

The performance impact of using uintptr_t is minimal. Most modern compilers can optimize the casting operations, resulting in little to no overhead.

When should I use intptr_t instead of uintptr_t ? {#intptr_t}

intptr_t is a signed integer type that can hold a pointer value. It is useful when you need to perform arithmetic operations on pointers that may result in negative values. However, in most cases, uintptr_t is recommended.

Is it safe to perform arithmetic operations on integers representing pointers? {#pointer-arithmetic}

Performing arithmetic operations on integers representing pointers can lead to undefined behavior if the resulting integer doesn't correspond to a valid memory address. It is generally safer to perform arithmetic operations on pointers directly.

How do I avoid losing information when casting pointers to integers? {#avoid-losing-information}

By using uintptr_t , you ensure that the integer is large enough to store the value of a pointer without losing any information. Make sure always to use uintptr_t when converting pointers to integers.

Related Links

  • C++ Reference: uintptr_t
  • C Reference: uintptr_t
  • Understanding Pointers in C and C++

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.

DEV Community

DEV Community

Coder Legion

Posted on Oct 4, 2023 • Originally published at coderlegion.com

Assignment makes integer from pointer without a cast in c

Programming can be both rewarding and challenging. You work hard on your code, and just when it seems to be functioning perfectly, an error message pops up on your screen, leaving you frustrated and clueless about what went wrong. One common error that programmers encounter is the "Assignment makes integer from pointer without a cast" error in C.

This error occurs when you try to assign a value from a pointer variable to an integer variable without properly casting it. To fix this error, you need to make sure that you cast the pointer value to the appropriate data type before assigning it to an integer variable. In this article, we will dive deeper into the causes of this error and provide you with solutions to overcome it.

Image description

What makes this error occur?

I will present some cases that triggers that error to occur, and they are all have the same concept, so if you understanded why the failure happens, then you will figure out how to solve all the cases easily.

Case 1: Assignment of a pointer to an integer variable

In this simple code we have three variables, an integer pointer "ptr", and two integers "n1" and "n2". We assign 2 to "n1", so far so good, then we assign the address of "n2" to "ptr" which is the suitable storing data type for a pointer, so no problems untill now, till we get to this line "n2 = ptr" when we try to assign "ptr" which is a memory address to "n2" that needs to store an integer data type because it's not a pointer.

Case 2: Returning a Pointer from a Function that Should Return an Integer

As you can see, it's another situation but it's the same idea which causes the compilation error. We are trying here to assign the return of getinteger function which is a pointer that conatains a memory address to the result variable that is an int type which needs an integer

Case 3: Misusing Array Names as Pointers

As we might already know, that the identifier (name) of the array is actually a pointer to the array first element memory address, so it's a pointer after all, and assigning a pointer type to int type causes the same compilation error.

The solutions

The key to avoiding the error is understanding that pointers and integers are different types of variables in C. Pointers hold memory addresses, while integers hold numeric values. We can use either casting, dereferencing the pointer or just redesign another solution for the problem we are working on that allows the two types to be the same. It all depending on the situation.

Let's try to solve the above cases:

Case 1: Solution: Deferencing the pointer

We need in this case to asssign an int type to "n2" not a pointer or memory address, so how do we get the value of the variable that the pointer "ptr" pointing to? We get it by deferencing the pointer, so the code after the fix will be like the following:

Case 2: Solution: Choosing the right data type

In this case we have two options, either we change the getinteger returning type to int or change the result variable type to a pointer. I will go with the latter option, because there are a lot of functions in the C standard library that returning a pointer, so what we can control is our variable that takes the function return. So the code after the fix will be like the following:

We here changed the result variable from normal int to an int pointer by adding "*".

Case 3: Solution: Using the array subscript operator

In this case we can get the value of any number in the array by using the subscript opeartor ([]) on the array with the index number like: myarray[1] for the second element which is 2. If we still remember that the array identifier is a pointer to the array first memory, then we can also get the value of the array first element by deferencing the array identifier like: *myarray which will get us 1.

But let's solve the case by using the subscript opeartor which is the more obvious way. So the code will be like the following:

Now the number 1 is assigned to myint without any compilation erros.

The conclusion

In conclusion, the error "assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast" arises in C programming when there is an attempt to assign a memory address (held by a pointer) directly to an integer variable. This is a type mismatch as pointers and integers are fundamentally different types of variables in C.

To avoid or correct this error, programmers need to ensure they are handling pointers and integers appropriately. If the intent is to assign the value pointed by a pointer to an integer, dereferencing should be used. If a function is meant to return an integer, it should not return a pointer. When dealing with arrays, remember that the array name behaves like a pointer to the first element, not an individual element of the array.

Top comments (0)

pic

Templates let you quickly answer FAQs or store snippets for re-use.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .

Hide child comments as well

For further actions, you may consider blocking this person and/or reporting abuse

shidhincr profile image

Macro tasks, Micro tasks and Long tasks - Web dev

Shidhin - May 15

falselight profile image

How to apply a skin tone to an emoji? 🧛🧛🏻🧛🏼🧛🏽🧛🏾🧛🏿

YURIIDE - May 15

kithenry profile image

Create Counter App With React

K(e)ith N. Henry - May 15

uxpin profile image

Join our free webinar: “Removing Friction from Design System Workflows”

UXPin - May 15

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

  • Windows Programming
  • UNIX/Linux Programming
  • General C++ Programming
  • warning:assignment makes integer from po

    warning:assignment makes integer from pointer without a cast.

assignment makes integer from pointer without a cast null

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

Understanding the pointer-integer conundrum in programming.

In the realm of programming, particularly in languages like C and C++, pointers play a crucial role in managing memory and optimizing the performance of applications. However, they can also be a source of confusion and errors, especially for those new to these languages. One common error that programmers encounter is the “assignment makes pointer from integer without a cast” warning or error. This message can be perplexing, but understanding its meaning is essential for writing clean, efficient, and error-free code.

Decoding the Warning: Assignment Makes Pointer from Integer Without a Cast

The warning “assignment makes pointer from integer without a cast” is a compiler message that indicates a potential issue in your code. It occurs when an integer value is assigned to a pointer variable without explicitly casting the integer to a pointer type. This can happen for various reasons, such as mistakenly using an integer as a memory address or overlooking the need for a type conversion.

Why Does This Warning Matter?

Ignoring this warning can lead to undefined behavior in your program. Since pointers and integers are fundamentally different types, treating an integer as a memory address can cause your program to access memory locations it shouldn’t, leading to crashes, data corruption, or security vulnerabilities.

Examples of Pointer-Integer Assignment Issues

To better understand the warning, let’s look at some examples where this issue might arise:

In the example above, the variable num is an integer, and ptr is a pointer to an integer. The assignment of num to ptr without a cast triggers the warning.

Root Causes of the Pointer-Integer Assignment Warning

Several scenarios can lead to this warning. Understanding these scenarios can help you avoid the issue in your code.

  • Accidental assignment of an integer to a pointer variable.
  • Using an integer as a function return value where a pointer is expected.
  • Incorrectly using an integer constant as a null pointer.
  • Forgetting to allocate memory before assigning it to a pointer.

Strategies to Avoid Pointer-Integer Assignment Issues

To prevent the “assignment makes pointer from integer without a cast” warning, you can employ several strategies:

  • Always use explicit casting when converting an integer to a pointer.
  • Ensure that functions returning pointers do not accidentally return integers.
  • Use the NULL macro or nullptr (in C++) for null pointers instead of integer constants.
  • Properly allocate memory using functions like malloc or new before assigning it to pointers.

Using Explicit Casting

Explicit casting involves converting one data type to another by specifying the target type. In the context of pointers and integers, you can use a cast to tell the compiler that you intentionally want to treat an integer as a pointer.

Proper Function Return Types

When writing functions that are supposed to return pointers, ensure that the return type is correctly declared as a pointer type, and that the return statements within the function adhere to this type.

Using NULL or nullptr

Instead of using integer constants like 0 for null pointers, use the NULL macro in C or nullptr in C++ to avoid confusion and potential warnings.

Memory Allocation

Before assigning a pointer, ensure that it points to a valid memory location by using memory allocation functions.

Advanced Considerations and Best Practices

Beyond the basic strategies, there are advanced considerations and best practices that can help you write robust pointer-related code:

  • Use static analysis tools to catch potential pointer-related issues before runtime.
  • Adopt coding standards that discourage unsafe practices with pointers and integers.
  • Understand the underlying architecture and how pointers are represented and used.
  • Regularly review and refactor code to improve clarity and safety regarding pointer usage.

FAQ Section

What is a pointer in programming.

A pointer is a variable that stores the memory address of another variable. Pointers are used for various purposes, such as dynamic memory allocation, implementing data structures like linked lists, and optimizing program performance.

Why is casting necessary when assigning an integer to a pointer?

Casting is necessary because pointers and integers are different data types with different interpretations. A cast explicitly informs the compiler that the programmer intends to treat the integer as a pointer, which can prevent unintended behavior.

Can ignoring the pointer-integer assignment warning lead to security issues?

Yes, ignoring this warning can lead to security issues such as buffer overflows, which can be exploited by attackers to execute arbitrary code or cause a program to crash.

Is it always safe to cast an integer to a pointer?

No, it is not always safe. The integer should represent a valid memory address that the program is allowed to access. Arbitrary casting without ensuring this can lead to undefined behavior and program crashes.

What is the difference between NULL and nullptr?

NULL is a macro that represents a null pointer in C and is typically defined as 0 or ((void *)0). nullptr is a keyword introduced in C++11 that represents a null pointer and is type-safe, meaning it cannot be confused with integer types.

The “assignment makes pointer from integer without a cast” warning is a signal from the compiler that there’s a potential issue in your code. By understanding what causes this warning and how to avoid it, you can write safer and more reliable programs. Always be mindful of the types you’re working with, use explicit casting when necessary, and follow best practices for pointer usage. With these strategies in place, you’ll be well-equipped to handle pointers and integers in your coding endeavors.

For further reading and a deeper understanding of pointers, integer casting, and related topics, consider exploring the following resources:

  • C Programming Language (2nd Edition) by Brian W. Kernighan and Dennis M. Ritchie
  • Effective Modern C++ by Scott Meyers
  • C++ Primer (5th Edition) by Stanley B. Lippman, Josée Lajoie, and Barbara E. Moo
  • ISO/IEC 9899:201x – International Standard for Programming Language C
  • ISO/IEC 14882:2017 – International Standard for Programming Language C++

These references provide a solid foundation for understanding the intricacies of pointers and type casting, as well as best practices for writing secure and efficient code in C and C++.

admin

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

Galaxy Z Fold 2 Vs Note 20 Ultra

Galaxy Z Fold 2 Vs Note 20 Ultra

Why Doesn’T My Phone Ring When I Get A Call

Why Doesn’T My Phone Ring When I Get A Call

How Many Xbox Players Are There In The World

How Many Xbox Players Are There In The World

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

  • About JOE TECH
  • Privacy Policy

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

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

Assignment makes integer from pointer without a cast [-Werror]

I keep getting this error message in response to hashtable[i]:

assignment makes integer from pointer without a cast [-Werror]

Best Answer

If hashtable is an array of integers then hashtable[i] expects an integer and NULL is a pointer.

So you're trying to assign a pointer value to an integer variable(without a cast), this is usually just a warning but since you have -Werror all warnings turn into errors.

Just use 0 instead of NULL .

Related Solutions

C++ – how to detect unsigned integer multiply overflow.

I see you're using unsigned integers. By definition, in C (I don't know about C++), unsigned arithmetic does not overflow ... so, at least for C, your point is moot :)

With signed integers, once there has been overflow, undefined behaviour (UB) has occurred and your program can do anything (for example: render tests inconclusive). 

To create a conforming program, you need to test for overflow before generating said overflow. The method can be used with unsigned integers too:

For division (except for the INT_MIN and -1 special case), there isn't any possibility of going over INT_MIN or INT_MAX .

Sqlite – Improve INSERT-per-second performance of SQLite

Several tips:

  • Put inserts/updates in a transaction.
  • For older versions of SQLite - Consider a less paranoid journal mode ( pragma journal_mode ). There is NORMAL , and then there is OFF , which can significantly increase insert speed if you're not too worried about the database possibly getting corrupted if the OS crashes. If your application crashes the data should be fine. Note that in newer versions, the OFF/MEMORY settings are not safe for application level crashes.
  • Playing with page sizes makes a difference as well ( PRAGMA page_size ). Having larger page sizes can make reads and writes go a bit faster as larger pages are held in memory. Note that more memory will be used for your database.
  • If you have indices, consider calling CREATE INDEX after doing all your inserts. This is significantly faster than creating the index and then doing your inserts.
  • You have to be quite careful if you have concurrent access to SQLite, as the whole database is locked when writes are done, and although multiple readers are possible, writes will be locked out. This has been improved somewhat with the addition of a WAL in newer SQLite versions.
  • Take advantage of saving space...smaller databases go faster. For instance, if you have key value pairs, try making the key an INTEGER PRIMARY KEY if possible, which will replace the implied unique row number column in the table.
  • If you are using multiple threads, you can try using the shared page cache , which will allow loaded pages to be shared between threads, which can avoid expensive I/O calls.
  • Don't use !feof(file) !

I've also asked similar questions here and here .

Related Topic

  • Assignment makes pointer from integer without cast

Join us to learn how we are empowering innovation from our expert embedded design engineers. Classes are open to all skill levels and cover an array of embedded control topics. Secure your spot now and stay ahead of the curve at MASTERs 2024 .

  • Menu About Community Forums

assignment makes integer from pointer without a cast null

The routine below draws an image on a tft-screen, and although the whole programm works fine and the image is displayed ok, I'm unable to resolve this 'assignment' warning at pS=pS0+n; .

I've tried several casts, but as yet not the right one (I'm not so well versed in casting).

Can anyone help with the solution?

Definition of the image:

Drawing the image:

The actual routine:

assignment makes integer from pointer without a cast null

pS0 should have the same type as pS (const char *), and definitely not unsigned int.

TNKernel-PIC32, an open-source real-time kernel for the PIC32

assignment makes integer from pointer without a cast null

I don't understand why you have ps0 defined as...

If you define it as the correct type, you can avoid all the ugly and unecessary casting:

Edit: andersm beat me to it.

It also seems a lot of your variables are incorrectly sized.  rgb8 could easily be unsigned char, for example.

assignment makes integer from pointer without a cast null

You are trying to convert an pointer to an integer without cast and it is simply telling you that the correct type is what pS type is that is the thing that line is trying to set after all

That all said it would be a hell of a lot easier to make PS0 a const char* and simply tell it

Thank you all for the replies. I think this will help me resolve it. And the the other tips too!

assignment makes integer from pointer without a cast null

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

ERROR: 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: ERROR: assignment makes integer from pointer without a cast

Thread tools.

  • Show Printable Version
  • Email this Page…
  • Subscribe to this Thread…
  • View Profile
  • View Forum Posts

ehitam is offline

Code: char *mid(char *tString, int x1, int x2){ char *rString; int i, l; rString= (char *) malloc(sizeof(char) * x2); l = x1-1; for (i= 0; i<x2; i++) { rString[i] = tString[l]; l++; } rString[x2]= NULL; return rString; } Mingw is giving me this error: C:\Users\Harshvardhan\Desktop\RPL\functions.h|30|w arning: assignment makes integer from pointer without a cast [enabled by default]| and for this line: Code: rString[x2]= NULL; And possible reason/cure for this?

anduril462 is offline

rString is a pointer (presumably to an array of chars or a string). Therefore rString[x2] is a single char. But NULL is a pointer type, which is not compatible with char. You are probably looking for the null byte/character (yes, slightly confusing) to terminate your string. Try Code: rString[x2] = '\0';
  • Visit Homepage

laserlight is offline

NULL is a null pointer constant. While it could just be the integer 0, it could also be (void*)0 or something else that ultimately is a null pointer constant. What you really want is a character constant, so just write: Code: rString[x2] = '\0'; By the way, as a single letter name, l is a horrible name as it can be confused with 1 on some fonts.
Originally Posted by Bjarne Stroustrup (2000-10-14) I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool. Look up a C++ Reference and learn How To Ask Questions The Smart Way
lol... And I always thought that NULL is exactly same as '\0'
Originally Posted by ehitam I always thought that NULL is exactly same as '\0' On some implementations, it is. Except for spelling, 0 is identical to '\0'. But you should not rely on that.
Originally Posted by laserlight By the way, as a single letter name, l is a horrible name as it can be confused with 1 on some fonts. I agree... I used it to signify length... And in code::blocks it is same as 1. I think I should change it with something better. And... Thanks laserlight and anduril for help...

stahta01 is offline

Originally Posted by laserlight NULL is a null pointer constant. While it could just be the integer 0, it could also be (void*)0 or something else that ultimately is a null pointer constant. What you really want is a character constant, so just write: Code: rString[x2] = '\0'; By the way, as a single letter name, l is a horrible name as it can be confused with 1 on some fonts. Is this a past end of array? In other words, is the code below correct? Code: rString[x2-1] = '\0'; Really tired, so my thinking might be wrong. Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
It looks more likely that the malloc should have allocated another char.
Originally Posted by stahta01 Is this a past end of array? No... x2-1 just make a character less... x2 is fine.
Originally Posted by laserlight It looks more likely that the malloc should have allocated another char. Do you mean rString?
Originally Posted by ehitam x2 is fine. The problem is that stahta01 is correct to say that it is a problem: you're writing to the array out of bounds. If you want to do this, your malloc should have a +1.
Originally Posted by laserlight The problem is that stahta01 is correct to say that it is a problem: you're writing to the array out of bounds. If you want to do this, your malloc should have a +1. Sorry... ... I'm not getting what you mean to say... When I use x2-1.... I get a character less...
Yes, you get a character less, but it's better than undefined behavior (what array out of bounds accesses result in). Arrays in C start at index 0 and go to index SIZE-1, or in your case x2-1. That is, your current code allocates an array of x2 characters, meaning you have rString[0]...rString[x2-1]. That's it, no more. rString[x2] is out of bounds. Tim's code corrects that in one possible way (by stopping at x2-1). Laserlight suggested another way, which allocates x2+1 characters, so rString[x2] is valid.
I mean that instead of this: Code: rString= (char *) malloc(sizeof(char) * x2); you should write: Code: rString = malloc(sizeof(*rString) * x2 + 1); Notice the +1 to account for the null character.
oh! Really didn't noticed that Thank you for help... I would replace x2 with x2+1

Next

  • Jump to page:
  • 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, 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.

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

Home Posts Topics Members FAQ

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

Similar topics

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.

IMAGES

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

    assignment makes integer from pointer without a cast null

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

    assignment makes integer from pointer without a cast null

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

    assignment makes integer from pointer without a cast null

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

    assignment makes integer from pointer without a cast null

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

    assignment makes integer from pointer without a cast null

  6. iphone

    assignment makes integer from pointer without a cast null

VIDEO

  1. How to change mouse pointer without use of control panel #viral #student #7thclass #zpschool

  2. Null Pointer Exception Song #2

  3. Java Programming # 44

  4. Integer Math for Non-Programmers

  5. 3 pointer without super 💀 #brawlstars #supercell #shorts

  6. Handle null pointer exception using java 8 Optional Feature Part 2

COMMENTS

  1. 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."

  2. Assignment makes integer from pointer without a cast [-Werror]

    assignment makes integer from pointer without a cast [-Werror] Why? c; int; ... If hashtable is an array of integers then hashtable[i] expects an integer and NULL is a pointer. ... assignment makes integer from pointer without a cast will be shown. Share. Improve this answer. Follow

  3. 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;

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

    In this simple code we have three variables, an integer pointer "ptr", and two integers "n1" and "n2". We assign 2 to "n1", so far so good, then we assign the address of "n2" to "ptr" which is the suitable storing data type for a pointer, so no problems untill now, till we get to this line "n2 = ptr" when we try to assign "ptr" which is a memory address to "n2" that needs to store an integer ...

  5. warning:assignment makes integer from po

    search_term[k] = '\0'; store[j] = search_term; return store; store and search_term are a pointers to char and store [j] is a char. you are assigning a pointer address to a char value. also, in your statement char search_term[k+1]; the value in [] should be a constant. thanks abhiskekm71, ah yes that makes sense.

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

    Decoding the Warning: Assignment Makes Pointer from Integer Without a Cast. The warning "assignment makes pointer from integer without a cast" is a compiler message that indicates a potential issue in your code. It occurs when an integer value is assigned to a pointer variable without explicitly casting the integer to a pointer type.

  7. ERROR: assignment makes integer from pointer without a cast

    NULL is a null pointer constant. While it could just be the integer 0, it could also be (void*)0 or something else that ultimately is a null pointer constant. What you really want is a character constant, so just write:

  8. [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.

  9. Assignment makes integer from pointer without a cast [-Werror]

    Assignment makes integer from pointer without a cast [-Werror] c int ... assignment makes integer from pointer without a cast [-Werror] Why? Best Answer. If hashtable is an array of integers then hashtable[i] expects an integer and NULL is a pointer. So you're trying to assign a pointer value to an integer variable ...

  10. C error

    When I try to compile the program I get the following messages in the terminal: 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 ...

  11. Assignment makes pointer from integer without a cast

    You are trying to convert an pointer to an integer without cast and it is simply telling you that the correct type is what pS type is that is the thing that line is trying to set after all. pS = (const char *) (pS0+n) That all said it would be a hell of a lot easier to make PS0 a const char* and simply tell it.

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

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

  14. ERROR: assignment makes integer from pointer without a cast

    Arrays in C start at index 0 and go to index SIZE-1, or in your case x2-1. That is, your current code allocates an array of x2 characters, meaning you have rString [0]...rString [x2-1]. That's it, no more. rString [x2] is out of bounds. Tim's code corrects that in one possible way (by stopping at x2-1). Laserlight suggested another way, which ...

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

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

  17. warning: comparison between pointer and integer

    warning: assignment makes integer from pointer without a cast by: Dawn Minnis | last post by: Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment...

  18. Error:Assignment makes pointer from integer without a cast

    You need take the address-of instead of and then pass to p, that's a pointer. In other words,you are assign char to a char* returned from value at i index in exp` array. Try this: p = &exp[i];

  19. 问题:warning: assignment makes integer from pointer without a cast

    warning: assignment makes integer from pointer without a cast [enabled by default] 这个警告其实不会导致系统运行出错,警告的意思是赋值类型和变量类型不一致导致的。. 在这个问题中一般出现的地方如下:. tempStruct *temp = tempStructGet();. 这种情况就会出现上述问题,一个函数 ...

  20. Error "assignment makes integer from pointer without a cast array = NULL"

    Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.