Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:29:24+00:00 2026-05-11T11:29:24+00:00

I have a dictionary that goes like this: typedef struct dictNode { int key;

  • 0

I have a dictionary that goes like this:

typedef struct dictNode {     int key;     char *value;     struct dictNode *next; } Dict; 

And a get() function that goes like this:

char *get(const Dict *dict, int key) {     if(!dict) return NULL;      Dict *currPtr = dict;      while(currPtr) {         if(currPtr->key == key) {             return currPtr->value;         }          currPtr = currPtr->next;     } } 

Compiling this code produces the following error:
dict.c:85: warning: initialization discards qualifiers from pointer target type

This warning refers to the line:

Dict *currPtr = dict; 

If I add a ‘const’ before that line like this:

const Dict *currPtr = dict; 

The warning goes away…

1) First thing I don’t understand is this: I added ‘const’ to the dict argument in get() so that the compiler warns me if I try to change the address dict is pointing to, otherwise I won’t be able to access the dictionary in the main program cause I lost the address I was pointing too. Now, I’m creating a new pointer, currPtr, that points to the same place as dict. This way, I use this pointer instead, to traverse the dictionary and keep dict pointer intact. Why do I also need to have const for currPtr?

2) Second thing I don’t understand is this: The line currPtr = currPtr->next; is changing the currPtr pointer, so, why doesn’t the compile warn me about that if I added a const to currPtr?

Then I have a del() function that goes like this:

Dict *del(const Dict *dict, int key) {     if(!dict) return NULL;      Dict *currPtr = dict;     Dict *prevPtr = dict;      while(currPtr) {         if(currPtr->key == key) {             prevPtr->next = currPtr->next;             free(currPtr);         }          prevPtr = currPtr;         currPtr = currPtr->next;     }      return dict; } 

Please note that I’m aware that this delete function code is not complete, it does not correctly work if I want to delete the first element for instance. It doesn’t matter, I’ll finish later, it suffices to demonstrate my problem…

3) So, in the get() function I needed to add const to currPtr but in this del() function, I don’t need to add const to currPtr nor prevPtr? In both functions I am changing the currPtr pointer and in the case of the del() function I’m changing the prevPtr pointer too. Why does the get() function require me to add a const before currPtr and the del() function does not require me to add a const before currPtr and prevPtr?

I can basically resume this whole post to: When and where exactly in my get() and del() functions should I use const and why, and, when and where I shouldn’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:29:24+00:00Added an answer on May 11, 2026 at 11:29 am

    Without a pointer, you would have

    const Dict currPtr 

    which is a constant Dict. Now, if you make it a pointer you have

    const Dict *currPtr 

    which is a pointer to a constant Dict. That does not mean that the pointer is constant. But it does mean that the Dict pointed to is treated as constant

    currPtr->key = 10; // error, dict is treated as constant.  

    But the pointer is not

    currPtr = otherPtr; // possible: the pointer is not constant 

    You would get an error for the second case if you make the pointer constant. Keeping the pointed dict constant too, this would look like this

    const Dict * const currPtr = init; 

    Now you can’t set currPtr to point to something different, because the pointer is now constant, not just what the pointer points to is treated so. Some people like the look if the const is always right to the stuff that it makes const. This would look like this

    Dict const * const currPtr = init; 

    which is the same as the previous snippet. If you then read it from right to left, it tells you what it is ‘const pointer to a const Dict’. If you have a type, it doesn’t matter how you order the specifiers

    int const a = 10; const int b = 10; 

    Both are constant integers. That is why we could put const right of the Dict type specifier.

    Now, if you have a pointer, you can always pretend you point to a constant object, even though the object wasn’t declared const. But you can’t pretend to work with a non-const object if what you point to is a const object:

    int const *p = NULL; // doesn't work without a cast. Will at least provoke a warning int *pn = p;  int *p = NULL; // always works: pretending to point to something const doesn't harm. int const *pc = p; 

    Note that if you make the pointer itself const, the rules are different to that. They are analogous to const applied to other types:

    int const i = 0; int j = i; // works. we only read the value of i. its const doesn't matter.   int * const p = NULL; int * q = p; // works: we only read the value of p (a null pointer). 

    After copying the value into a new variable (whether pointer or not), the new variable is not connected in any way to the other variable, because the value read has no associativity to how the value was created in the first place. The const of the other variable doesn’t matter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 80k
  • Answers 80k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I use SDB on a couple of large-ish applications. The… May 11, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer As far as I know setting the element.style.backgroundImage is essentially… May 11, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer I remember that Martin Fowler advices to keep the control… May 11, 2026 at 4:20 pm

Related Questions

I want to create variables inside a function from a dictionary. Let's say I
I doubt this is possible, but I was curious if you could have more
I'm trying to read input from the terminal. For this, I'm using a BufferedReader.
This one is a bit tedious in as far as explaining, so here goes.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.