Understanding 'Assignment Discards Const Qualifier from Pointer Target Type': Solutions and Best Practices

David Henegar

This documentation aims to provide a comprehensive guide on understanding and solving the warning message "assignment discards 'const' qualifier from pointer target type" in the C programming language. We will cover the cause of this warning and provide step-by-step solutions and best practices to avoid this issue in the future.

Table of Contents

  • What is 'const' qualifier?
  • Why does the warning occur?
  • Solutions and Best Practices
  • Using const-correctness
  • Casting away const-ness

What is 'const' qualifier? {#what-is-const-qualifier}

In C, the const keyword is used to declare a variable that cannot be modified after initialization. It is a form of compile-time enforcement to guarantee that the value of a variable remains unchanged throughout the program. The const keyword is also used to indicate that a function does not modify the object pointed to by a pointer argument.

Consider the following example:

Why does the warning occur? {#why-does-the-warning-occur}

The "assignment discards 'const' qualifier from pointer target type" warning occurs when you try to assign a pointer to a non-const object to a pointer to a const object. This is because doing so would allow the modification of the const object through the non-const pointer, which is not allowed.

For example:

In the above example, the compiler will generate a warning because assigning the address of a const object (x) to a non-const pointer (p) could lead to the modification of the const object, which is not allowed.

Solutions and Best Practices {#solutions-and-best-practices}

Using const-correctness {#using-const-correctness}.

The best practice to avoid this warning is to ensure const-correctness in your code. When working with const objects, always use const pointers. This will prevent you from accidentally modifying const objects and will help you catch potential issues during compilation.

For example, the following code is const-correct and will not generate any warning:

Casting away const-ness {#casting-away-const-ness}

In some cases, you might need to cast away the const-ness of an object. This should be done with caution and only when you are sure that the object will not be modified. You can use a typecast to remove the const qualifier from a pointer:

Keep in mind that casting away const-ness can lead to undefined behavior if you attempt to modify the object through the non-const pointer. Therefore, it is recommended to only use this approach when absolutely necessary and when you are sure that the object will not be modified.

What is const-correctness? {#what-is-const-correctness}

Const-correctness is a programming practice that ensures the proper use of const qualifiers in your code. It involves using const pointers when working with const objects and preventing the modification of const objects through non-const pointers.

Why should I use const pointers? {#why-should-i-use-const-pointers}

Using const pointers helps to maintain the integrity of const objects and prevents accidental modifications. It also helps to catch potential issues during compilation by generating warnings when you try to assign a const object to a non-const pointer.

Can I modify a const object using a non-const pointer? {#can-i-modify-a-const-object-using-a-non-const-pointer}

Modifying a const object through a non-const pointer leads to undefined behavior. While it is possible to cast away the const-ness of an object, doing so should be done with caution and only when you are sure that the object will not be modified.

When should I cast away const-ness? {#when-should-i-cast-away-const-ness}

Casting away const-ness should be done cautiously and only when absolutely necessary. It is recommended to use this approach when you are sure that the object will not be modified and when there is no other way to accomplish your task without casting away const-ness.

How can I ensure const-correctness in my code? {#how-can-i-ensure-const-correctness-in-my-code}

To ensure const-correctness in your code, always use const pointers when working with const objects, and never attempt to modify const objects through non-const pointers. Make sure to follow the proper use of const qualifiers in your code and pay attention to compiler warnings related to const-ness.

Additional Resources

  • C Programming: const Keyword
  • Understanding const in C: A Complete Guide
  • A Guide to Undefined Behavior 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.

AOverflow.com

AOverflow.com Logo

warning: assignment discards 'const' qualifier from pointer target type

I am implementing the strchr function with the following prototype:

and whose code is:

with a small main to prove it.

When executing the program it gives me the following warning:

I understand that it is because of working with a constant variable and making an assignment, but I can't understand what is the root of the problem and how to solve it.

Isn't doing the assignment like making a "copy" of the pointer *s and storing it in *aux, later working with the "copy"?

warning assignment discards 'const' qualifier from pointer target type

First of all, you have to understand what this statement means:

The code above is interpreted like this: " s it's a pointer to a constant character", in other words, you're telling the compiler: "Hey man, s it points to a character that is a constant, its value shouldn't change".

Since pa it points to a constant integer, it is illegal to dereference the pointer , for that reason this expression leads to an error: *pa = 1 , because pa it cannot modify the variable a (it doesn't matter if a it wasn't declared as a constant variable, the expression is still illegal).

Once you know this, it's easy to see why the compiler throws a warning on this line:

I go back and repeat: s it is a pointer that points to a constant character, instead it aux points to a character (it can be modified, because there is no such restriction), for that reason the compiler complains, because aux now it will be able to modify the character to which the pointer points s .

Now you may be wondering, is this really a problem? If we execute your code it will surely work but the code will be confusing, because first you indicate that the value it points to s should not be modified and then aux if it can be modified, then it is not clear what the intention of the code is.

In fact, the purpose of this statement:

It is being able to indicate to other developers that they s should NEVER modify the value to which it refers. This helps to avoid possible errors, since in this way the developer will not intentionally modify (as you know, programmers are clueless) the value that the pointer points to.

Answering questions...

Of course yes.

This line is simple to interpret:

You're copying the memory address pointed s to by aux , at the end aux and s they point to the same thing. The problem with that assignment is that you aux can modify the value you point to s . This warning is simple to solve, put the const in the declaration:

In fact, the pointer aux is unnecessary, you can remove it and work directly with s .

A LOT OF EYE: Do not be confused with this statement:

In that case s it is a constant pointer that points to a character, this means that you cannot modify the content of the pointer.

Web Analytics Made Easy - Statcounter

Welcome to the new Microchip Forum! All previous communities hosted on  https://www.microchip.com/forums  are now being redirected to  https://forum.microchip.com . Please carefully review and follow the site  login instructions  and important information related to  users of AVR Freaks and Microchip Forum sites , including retired Atmel sites.

  • Menu About Community Forums

warning assignment discards 'const' qualifier from pointer target type

C Data Types

C operators.

  • C Input and Output
  • C Control Flow
  • C Functions
  • C Preprocessors

C File Handling

  • C Cheatsheet

C Interview Questions

  • C Programming Language Tutorial
  • C Language Introduction
  • Features of C Programming Language
  • C Programming Language Standard
  • C Hello World Program
  • Compiling a C Program: Behind the Scenes
  • Tokens in C
  • Keywords in C

C Variables and Constants

  • C Variables
  • Constants in C

Const Qualifier in C

  • Different ways to declare variable as constant in C
  • Scope rules in C
  • Internal Linkage and External Linkage in C
  • Global Variables in C
  • Data Types in C
  • Literals in C
  • Escape Sequence in C
  • Integer Promotions in C
  • Character Arithmetic in C
  • Type Conversion in C

C Input/Output

  • Basic Input and Output in C
  • Format Specifiers in C
  • printf in C
  • Scansets in C
  • Formatted and Unformatted Input/Output functions in C with Examples
  • Operators in C
  • Arithmetic Operators in C
  • Unary operators in C
  • Relational Operators in C
  • Bitwise Operators in C
  • C Logical Operators
  • Assignment Operators in C
  • Increment and Decrement Operators in C
  • Conditional or Ternary Operator (?:) in C
  • sizeof operator in C
  • Operator Precedence and Associativity in C

C Control Statements Decision-Making

  • Decision Making in C (if , if..else, Nested if, if-else-if )
  • C - if Statement
  • C if...else Statement
  • C if else if ladder
  • Switch Statement in C
  • Using Range in switch Case in C
  • while loop in C
  • do...while Loop in C
  • For Versus While
  • Continue Statement in C
  • Break Statement in C
  • goto Statement in C
  • User-Defined Function in C
  • Parameter Passing Techniques in C
  • Function Prototype in C
  • How can I return multiple values from a function?
  • main Function in C
  • Implicit return type int in C
  • Callbacks in C
  • Nested functions in C
  • Variadic functions in C
  • _Noreturn function specifier in C
  • Predefined Identifier __func__ in C
  • C Library math.h Functions

C Arrays & Strings

  • Properties of Array in C
  • Multidimensional Arrays in C
  • Initialization of Multidimensional Array in C
  • Pass Array to Functions in C
  • How to pass a 2D array as a parameter in C?
  • What are the data types for which it is not possible to create an array?
  • How to pass an array by value in C ?
  • Strings in C
  • Array of Strings in C
  • What is the difference between single quoted and double quoted declaration of char array?
  • C String Functions
  • Pointer Arithmetics in C with Examples
  • C - Pointer to Pointer (Double Pointer)
  • Function Pointer in C
  • How to declare a pointer to a function?
  • Pointer to an Array | Array Pointer
  • Difference between constant pointer, pointers to constant, and constant pointers to constants
  • Pointer vs Array in C
  • Dangling, Void , Null and Wild Pointers in C
  • Near, Far and Huge Pointers in C
  • restrict keyword in C

C User-Defined Data Types

  • C Structures
  • dot (.) Operator in C
  • Structure Member Alignment, Padding and Data Packing
  • Flexible Array Members in a structure in C
  • Bit Fields in C
  • Difference Between Structure and Union in C
  • Anonymous Union and Structure in C
  • Enumeration (or enum) in C

C Storage Classes

  • Storage Classes in C
  • extern Keyword in C
  • Static Variables in C
  • Initialization of static variables in C
  • Static functions in C
  • Understanding "volatile" qualifier in C | Set 2 (Examples)
  • Understanding "register" keyword in C

C Memory Management

  • Memory Layout of C Programs
  • Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
  • Difference Between malloc() and calloc() with Examples
  • What is Memory Leak? How can we avoid?
  • Dynamic Array in C
  • How to dynamically allocate a 2D array in C?
  • Dynamically Growing Array in C

C Preprocessor

  • C Preprocessor Directives
  • How a Preprocessor works in C?
  • Header Files in C
  • What’s difference between header files "stdio.h" and "stdlib.h" ?
  • How to write your own header file in C?
  • Macros and its types in C
  • Interesting Facts about Macros and Preprocessors in C
  • # and ## Operators in C
  • How to print a variable name in C?
  • Multiline macros in C
  • Variable length arguments for Macros
  • Branch prediction macros in GCC
  • typedef versus #define in C
  • Difference between #define and const in C?
  • Basics of File Handling in C
  • C fopen() function with Examples
  • EOF, getc() and feof() in C
  • fgets() and gets() in C language
  • fseek() vs rewind() in C
  • What is return type of getchar(), fgetc() and getc() ?
  • Read/Write Structure From/to a File in C
  • C Program to print contents of file
  • C program to delete a file
  • C Program to merge contents of two files into a third file
  • What is the difference between printf, sprintf and fprintf?
  • Difference between getc(), getchar(), getch() and getche()

Miscellaneous

  • time.h header file in C with Examples
  • Input-output system calls in C | Create, Open, Close, Read, Write
  • Signals in C language
  • Program error signals
  • Socket Programming in C
  • _Generics Keyword in C
  • Multithreading in C
  • C Programming Interview Questions (2024)
  • Commonly Asked C Programming Interview Questions | Set 1
  • Commonly Asked C Programming Interview Questions | Set 2
  • Commonly Asked C Programming Interview Questions | Set 3

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed (which depends upon where const variables are stored, we may change the value of the const variable by using a pointer). The result is implementation-defined if an attempt is made to change a const.

Using the const qualifier in C is a good practice when we want to ensure that some values should remain constant and should not be accidentally modified.

In C programming, the const qualifier can be used in different contexts to provide various behaviors. Here are some different use cases of the const qualifier in C:

1. Constant Variables

In this case, const is used to declare a variable var as a constant with an initial value of 100. The value of this variable cannot be modified once it is initialized. See the following example:

2. Pointer to Constant

We can change the pointer to point to any other integer variable, but cannot change the value of the object (entity) pointed using pointer ptr. The pointer is stored in the read-write area (stack in the present case). The object pointed may be in the read-only or read-write area. Let us see the following examples.

Example 2: Program where variable i itself is constant.

Down qualification is not allowed in C++ and may cause warnings in C. Down qualification refers to the situation where a qualified type is assigned to a non-qualified type.

Example 3: Program to show down qualification.

3. Constant Pointer to Variable

The above declaration is a constant pointer to an integer variable, which means we can change the value of the object pointed by the pointer, but cannot change the pointer to point to another variable.

4. Constant Pointer to Constant

The above declaration is a constant pointer to a constant variable which means we cannot change the value pointed by the pointer as well as we cannot point the pointer to other variables. Let us see with an example. 

Advantages of const Qualifiers in C

The const qualifier in C has the following advantages:

  • Improved Code Readability: By marking a variable as const, you indicate to other programmers that its value should not be changed, making your code easier to understand and maintain.
  • Enhanced Type Safety : By using const, you can ensure that values are not accidentally modified, reducing the chance of bugs and errors in your code.
  • Improved Optimization: Compilers can optimize const variables more effectively, as they know that their values will not change during program execution. This can result in faster and more efficient code.
  • Better Memory Usage: By declaring variables as const, you can often avoid having to make a copy of their values, which can reduce memory usage and improve performance.
  • Improved Compatibility : By declaring variables as const, you can make your code more compatible with other libraries and APIs that use const variables.
  • Improved Reliability : By using const, you can make your code more reliable, as you can ensure that values are not modified unexpectedly, reducing the risk of bugs and errors in your code.

This article is compiled by “ Narendra Kangralkar “.

Please Login to comment...

Similar reads.

  • C-Storage Classes and Type Qualifiers
  • 5 Reasons to Start Using Claude 3 Instead of ChatGPT
  • 6 Ways to Identify Who an Unknown Caller
  • 10 Best Lavender AI Alternatives and Competitors 2024
  • The 7 Best AI Tools for Programmers to Streamline Development in 2024
  • 30 OOPs Interview Questions and Answers (2024)

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Search code, repositories, users, issues, pull requests...

Provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

MicroPython

Warning: initialization discards 'const' qualifier from pointer target type #11142.

@sabsaback

{{editor}}'s edit

Sabsaback mar 28, 2023.

Beta Was this translation helpful? Give feedback.

Replies: 2 comments · 4 replies

Sabsaback mar 14, 2024 author, dlech mar 14, 2024 sponsor.

@sabsaback

sabsaback Mar 15, 2024 Author

@dlech

dlech Mar 15, 2024 Sponsor

@sabsaback

  • Numbered list
  • Unordered list
  • Attach files

Select a reply

Compiler warnings: assignment discards 'const' qualifier from pointer target type

Description.

Compiling LVGL (v8.0.2) code (RISC-V GCC, 8.3.0) I see these warnings:

lvgl/src/widgets/lv_checkbox.c:123:13: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

lvgl/src/core/lv_obj.c:132:18: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

lvgl/src/extra/widgets/tabview/lv_tabview.c:84:25: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

lvgl/src/extra/widgets/tabview/lv_tabview.c:93:24: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

lvgl/src/extra/widgets/tabview/lv_tabview.c:95:37: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

lvgl/src/extra/widgets/tabview/lv_tabview.c:97:37: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

lvgl/src/extra/widgets/tabview/lv_tabview.c:196:21: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

What MCU/Processor/Board and compiler are you using?

RISC-V (GCC 8.3.0), custom platform

What do you want to achieve?

Clean LVGL build with no discarded-qualifiers warnings. I’d like to treat them as errors (-Werror), but currently LVGL prevents me from doing this. It should be easy to fix these.

What have you tried so far?

Adding -Wno-discarded-qualifiers option: the build works, but I’d like to avoid having to add this option.

Code to reproduce

Screenshot and/or video.

(cc @kisvegabor )

Currently, our CI uses -Wno-discarded-qualifiers ; this would need to be changed if we are going to consider this a disallowed warning.

Unfortunately, I can’t see these warnings with GCC 7.5.0, but I agree that it’d be better to enable them.

If @embeddedt also agrees the enable discarded-qualifiers warnings, @marcinh0001 , can you send a PR with the fixes?

I could, but as I explained in another thread, I’m required to add copyright notices to all files I modify, even slightly. I’d like to avoid it, it’s too many files. Would it be possible for someone else to get rid of discarded-qualifiers warnings?

I can do it, however, I need to find a way of getting them to show up first, as when I enabled -Wdiscarded-qualifiers locally last week, they weren’t appearing.

Thanks. I think you’ll need a sufficiently fussy compiler (probably meaning modern?) I don’t have a full view of which GCC versions are / aren’t fussy about it, but these two I use definitely are:

  • 8.3.0 for RISC-V
  • 10.2.0 for x86 used under mingw

And, needless to say, you need to get rid of the -Wno-discarded-qualifiers option.

:slight_smile:

I am having the same issues. Environment is

Win 11 pro PlatformIO using Arduino framework lvgl 8.3.4 ESP32DEV module when compiling lvgl, i get several messages like

when using squarline studio 1.2.x, even more of such messages show up. Unfortunately, when I try to disable warning by specifying -Wno-discarded-qualifiers I get informed that this is not allowed in a C++ compile environment. Now, there shouldn’t be any warnings when compiling anyhow, so - what do I have to do to have this situation remedied?

With best regards

Volker Bandke

Same here, I’m working with the latest version of LVGL and PlatformIO using the Arduino Framework. I would like to suppress this warning using the build flag -Wno-discarded-qualifiers However, as previously mentioned, this is not allowed in C++.

Is there another way to suppress this warning globally?

corrects the warning:

src\core\lv_obj.c 143

const uint8_t * txt_u8; txt_u8 = (const uint8_t *)txt;

src\extra\widgets\tabview 81 const char ** new_map;

src\extra\widgets\tabview 107 tabview->map = (char **)new_map;

src\extra\widgets\tabview 228 tabview->map[0] = (char *)"";

\src\widgets\lv_checkbox.c 125 cb->txt = (char *)"Check box";

Great! Are you going to issue a pull request?

warning assignment discards 'const' qualifier from pointer target type

  • Latest Articles
  • Top Articles
  • Posting/Update Guidelines
  • Article Help Forum

warning assignment discards 'const' qualifier from pointer target type

  • View Unanswered Questions
  • View All Questions
  • View C# questions
  • View C++ questions
  • View Javascript questions
  • View Visual Basic questions
  • View Python questions
  • CodeProject.AI Server
  • All Message Boards...
  • Running a Business
  • Sales / Marketing
  • Collaboration / Beta Testing
  • Work Issues
  • Design and Architecture
  • Artificial Intelligence
  • Internet of Things
  • ATL / WTL / STL
  • Managed C++/CLI
  • Objective-C and Swift
  • System Admin
  • Hosting and Servers
  • Linux Programming
  • .NET (Core and Framework)
  • Visual Basic
  • Web Development
  • Site Bugs / Suggestions
  • Spam and Abuse Watch
  • Competitions
  • The Insider Newsletter
  • The Daily Build Newsletter
  • Newsletter archive
  • CodeProject Stuff
  • Most Valuable Professionals
  • The Lounge  
  • The CodeProject Blog
  • Where I Am: Member Photos
  • The Insider News
  • The Weird & The Wonderful
  • What is 'CodeProject'?
  • General FAQ
  • Ask a Question
  • Bugs and Suggestions

C Warning - assignment discards qualifiers from pointer target type

warning assignment discards 'const' qualifier from pointer target type

Add your solution here

  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Print

  • Create Account

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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.

Source Code

  • Issues  0
  • Roadmap 

#22 warning: assignment discards ‘const’ qualifier from pointer target type Closed: fixed 5 years ago Opened 5 years ago by emaldonado.

This is what we get:

Triggered by the '-Wdiscarded-qualifiers' uses in building.

warning assignment discards 'const' qualifier from pointer target type

This patch fixes the discarded 'const' qualifier warnings.

fixDiscardedConstQualifier.patch

(Edited description for formatting)

This is fixed by jss pr#25 . The upstream commit is cf19f3 .

Metadata Update from @cipherboy : - Custom field component adjusted to None - Custom field feature adjusted to None - Custom field origin adjusted to None - Custom field proposedmilestone adjusted to None - Custom field proposedpriority adjusted to None - Custom field reviewer adjusted to None - Custom field type adjusted to None - Custom field version adjusted to None

Metadata Update from @cipherboy : - Issue close_status updated to: fixed - Issue status updated to: Closed (was: Open)

Login to comment on this ticket.

Attachments 1

質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!.

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

const修飾子での警告発生理由

setsu_tenhou

投稿 2016/11/12 13:22

###前提・実現したいこと c言語でハッシュテーブルのプログラムを組んでいます、見たことのない警告が出たので、出た理由をしりたく、質問します ###発生している問題・エラーメッセージ ChainHash.c:14: warning: assignment discards qualifiers from pointer target type

###試したこと インターネットで調べてみたところ、なにやらconst修飾子が問題のようですが、しっくりくる、情報が見つけられませんでした。

###補足情報(言語/FW/ツール等のバージョンなど) 実行環境 : Centos6.5(final) コンパイラ : gcc-4.4.7-4.el6.x86_64

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちら の条件を満たす必要があります。

guest

Node構造体のnextという変数が、constがついていないNode*型であることが理由です。 const Nodeへのポインタを、Nodeへのポインタに代入しようとしているのです。

上記のコードを見てください。pの中身は、constがついているので変わらない「はず」ですよね? ですが、qを使うことで、中身を操作できてしまいます。 これが意図しないバグを生むことが多いため、constがついたポインタをそうでないポインタに代入しようとすると、警告が出るのです。

投稿 2016/11/12 13:38

majiponi

2016/11/12 14:43

ここの238ページにわかりやすく書いているとおりです。 https://www.math.nagoya-u.ac.jp/~naito/lecture/2003_SS/PDF/02/C-1.pdf

投稿 2016/11/12 13:48

hiim

質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。

15分調べてもわからないことは teratailで質問しよう!

ただいまの回答率 85 . 48 %

質問をまとめることで 思考を整理して素早く解決

テンプレート機能で 簡単に質問をまとめる

IMAGES

  1. resolve warning: assignment discards 'const' qualifier from pointer

    warning assignment discards 'const' qualifier from pointer target type

  2. warning: passing argument 3 of 'disp_32x32' discards 'const' qualifier

    warning assignment discards 'const' qualifier from pointer target type

  3. [Solved] passing argument 2 of 'memcpy' discards

    warning assignment discards 'const' qualifier from pointer target type

  4. initialization discards 'const' qualifier from pointer target type

    warning assignment discards 'const' qualifier from pointer target type

  5. c: discards ‘const’ qualifier from pointer target type

    warning assignment discards 'const' qualifier from pointer target type

  6. Day 22

    warning assignment discards 'const' qualifier from pointer target type

VIDEO

  1. You should read const pointers backwards

  2. Converting string to const char pointer C++

  3. Lecture 11: Constant| Types of constant| const qualifier| Constant initialization in c language

  4. When the Narcissist Discards New Supply they Claimed was Their Soulmate. #narcissist #npd #discard

  5. #samirsir ktsoftware #derived data type

  6. JavaScript variable assignment const variable

COMMENTS

  1. warning: discards 'const' qualifiers from pointer target type

    For first warning: return discards 'const' qualifiers from pointer target type. C does not have an implicit conversion from const-qualified pointer types to non-const-qualified ones, so to overcome the warning you need to add it explicitly. Replace return s; with return (char *)s; For second warning: assignment discards 'const' qualifiers from ...

  2. warning: assignment discards qualifiers from pointer target type

    @Asher: const T* is just a promise not to modify the pointee through that pointer, so you can implicitly convert a T* to const T* fine. (This does fall down for pointers-to-pointers, though. (This does fall down for pointers-to-pointers, though.

  3. warning: "assignment discards 'const' qualifier from pointer target type"

    You could change the signature of myStrTod so that your output is to a pointer to const char: float myStrTod(const char *nPtr, const char **endPtr); Then GCC will report a different error: 40937624.c: In function 'main': 40937624.c:11:25: warning: passing argument 2 of 'myStrTod' from incompatible pointer type [-Wincompatible-pointer ...

  4. Solved: Assignment discards 'const' qualifier from pointer ...

    I can't figure out how to fix the following compiler warning: assignment discards 'const' qualifier from pointer target type. I have set up an area in Flash to store firmware for another PSoC (I will be bootloading it): const uint8 FW831 [FW_SIZE] __ALIGNED (CY_FLASH_SIZEOF_ROW) = {0u}; My understanding is that I have to use "const" or the ...

  5. Assignment Discards Const Qualifier From Pointer Target Type (Resolved)

    The "assignment discards 'const' qualifier from pointer target type" warning occurs when you try to assign a pointer to a non-const object to a pointer to a const object. This is because doing so would allow the modification of the const object through the non-const pointer, which is not allowed. For example: In the above example, the compiler ...

  6. warning: assignment discards 'const' qualifier from pointer target type

    The problem with that assignment is that you aux can modify the value you point to s. This warning is simple to solve, put the const in the declaration: const char* aux = s; In fact, the pointer aux is unnecessary, you can remove it and work directly with s. A LOT OF EYE: Do not be confused with this statement:

  7. [Resolved] assignment discards qualifiers from pointer target type

    Joined: 23 Jun 2008. Posts: 469 View Posts. I have some code that is giving me a warning... assignment discards qualifiers from pointer target type Here it is... const XCHARItemList [] ="Line1\n ""Line2\n ""Line3\n "; XCHAR* pTemp; pTemp =ItemList ; // <--- warning on this line. Since the code is not working right, I assume that the warning may ...

  8. warning: initialization discards qualifiers from pointer target type

    The part that's wrong is trying to convert a "pointer to a const pointer " into a "pointer to a pointer to const "; you're removing the const from what the pointer points to ( "discarding the qualifier "). To assign to the same type, you want const void* const* fields = SS_Values1; jtemples, Thanks heaps for your response.

  9. Const Qualifier in C

    main.c: In function 'main': main.c:16:9: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] 16 | ptr = &j; | ^ *ptr: 10 *ptr: 20 3. Constant Pointer to Variable int* const ptr; The above declaration is a constant pointer to an integer variable, which means we can change the value of the ...

  10. resolve warning: assignment discards 'const' qualifier from pointer

    This warning became quite frequent after the data member of sc_apdu lost it's const qualifier. It was done to allow the background treatment of SM wrap/unwrap of the APDU data. Probably solution was not the best and there exists a better one.

  11. warning: assignment discards 'const' qualifier from pointer target type

    warning: assignment discards 'const' qualifier from pointer target type #107. Closed osalbahr opened this issue Nov 22, 2023 · 2 comments Closed ... warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] 1193 | hinfo = spec->gen.stream_analog_playback; | ^ At main.c:167: ...

  12. warning: initialization discards 'const' qualifier from pointer target type

    warning: initialization discards 'const' qualifier from pointer target type. I am trying to export a module with some functions and I compile with meson. This is throwing a warning and I am wondering if there is any way to get rid of it? ... warning: initialization discards 'const' qualifier from pointer target type #11142. sabsaback started ...

  13. Compiler warnings: assignment discards 'const' qualifier from pointer

    warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] new_map[1] = ""; when using squarline studio 1.2.x, even more of such messages show up. Unfortunately, when I try to disable warning by specifying

  14. C Warning

    Solution 1. You only have a const void* and assign that to a void*. Hence you can modify the target with pt_SAP->pt_param although you could not with pt_Param. This breaks the contract of the function parameter declaration, which "promises" not to modify the object to which pt_Param pointer. And hence the warning. Nice explanation, 5ed.

  15. warning: assignment discards qualifiers from pointer target type

    Here is basically what the code looks like: char *str; str = Inet_ntop (...); //returns const char * It's warning you that the "protection" of the pointer's target provided by the 'const' qualifier is lost when the 'const' is thrown away by assigning it to a "plain" type 'char*' pointer.

  16. Issue #22: warning: assignment discards 'const' qualifier from pointer

    PK11Store.c:841:19: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] JSSKeyStoreSpi.c:642:20: warning: passing argument 1 of 'freeObjectNick' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] ----- PK11Store.c: In function 'Java_org_mozilla_jss_pkcs11 ...

  17. const在指针赋值时候注意事项:assignment discards 'const' qualifier from pointer

    const在指针赋值时候注意事项:assignment discards 'const' qualifier from pointer target type的解决方法. lillian-lin: 这个能运行成功,那么非const数据可以赋值给const指针。 const在指针赋值时候注意事项:assignment discards 'const' qualifier from pointer target type的解决方法

  18. Assignment discards 'const' qualifier from pointer target type compiler

    Solved: I can't figure out how to fix the following compiler warning: assignment discards 'const' qualifier from pointer target type I have set up an We use cookies and similar technologies (also from third parties) to collect your device and browser information for a better understanding on how you use our online offerings.

  19. initialization discards 'const' qualifier from pointer target type

    argv is declared as const: const char **argv.This means that each element of argv is a const char * (you can view argv as an array of const pointers). You are assigning that const char * to a non-const char *, which discards the const qualifier. This is what prompts the warning. You can avoid this problem by chaning the signature of main to int main(int argc, char **argv).

  20. const修飾子での警告発生理由

    ###前提・実現したいこと c言語でハッシュテーブルのプログラムを組んでいます、見たことのない警告が出たので、出た理由をしりたく、質問します ###発生している問題・エラーメッセージ ChainHash.c:14: warning: assignment discards qualifiers from pointer target type