Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with double pointers and const

Help with double pointers and const

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    Hi, i cannot figure this out:

    typedef struct
    {
    int a;
    int b;
    } CARPIC;

    const CARPIC const* CarPics[] =
    {
    &Audi,
    &BMW,
    &Bugatti,
    }

    // Get function
    void GetCarPicture(CARPIC** carPicture)
    {
    int carId= GetCarId(); // Lets say, returns 2
    *carPicture = CarPics[carId];
    }

    // Using the function
    CARPIC* car;
    GetCarPicture(&car);

    I get the warning:

    warning: assignment discards qualifiers from pointer target type

    I tried to add the const keyword to the GetCarPicture function parameter but could not figure out where to add it. I need to get the pointer to the const car picture. Regards.

    L C 2 Replies Last reply
    0
    • E elelont2

      Hi, i cannot figure this out:

      typedef struct
      {
      int a;
      int b;
      } CARPIC;

      const CARPIC const* CarPics[] =
      {
      &Audi,
      &BMW,
      &Bugatti,
      }

      // Get function
      void GetCarPicture(CARPIC** carPicture)
      {
      int carId= GetCarId(); // Lets say, returns 2
      *carPicture = CarPics[carId];
      }

      // Using the function
      CARPIC* car;
      GetCarPicture(&car);

      I get the warning:

      warning: assignment discards qualifiers from pointer target type

      I tried to add the const keyword to the GetCarPicture function parameter but could not figure out where to add it. I need to get the pointer to the const car picture. Regards.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This would make more sense: ```c++ typedef struct { int a; int b; } CARPIC; CARPIC Audi = { 1, 2 }; CARPIC BMW = { 1, 2 }; CARPIC Bugatti = { 1, 2 }; CARPIC const* CarPics[] = { &Audi, &BMW, &Bugatti, }; // Get function const CARPIC* GetCarPicture(int carId) { return CarPics[carId]; } // Using the function CARPIC const* car; car = GetCarPicture(2);/// ```

      1 Reply Last reply
      0
      • E elelont2

        Hi, i cannot figure this out:

        typedef struct
        {
        int a;
        int b;
        } CARPIC;

        const CARPIC const* CarPics[] =
        {
        &Audi,
        &BMW,
        &Bugatti,
        }

        // Get function
        void GetCarPicture(CARPIC** carPicture)
        {
        int carId= GetCarId(); // Lets say, returns 2
        *carPicture = CarPics[carId];
        }

        // Using the function
        CARPIC* car;
        GetCarPicture(&car);

        I get the warning:

        warning: assignment discards qualifiers from pointer target type

        I tried to add the const keyword to the GetCarPicture function parameter but could not figure out where to add it. I need to get the pointer to the const car picture. Regards.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        It should be

        void GetCarPicture(const CARPIC const * * carPicture)
        {
        //...
        }
        //...
        // Using the function
        const CARPIC const * car;
        GetCarPicture(&car);

        E S 2 Replies Last reply
        0
        • C CPallini

          It should be

          void GetCarPicture(const CARPIC const * * carPicture)
          {
          //...
          }
          //...
          // Using the function
          const CARPIC const * car;
          GetCarPicture(&car);

          E Offline
          E Offline
          elelont2
          wrote on last edited by
          #4

          Thanks, worked great, will try to figure out what gets consted...

          C S 2 Replies Last reply
          0
          • E elelont2

            Thanks, worked great, will try to figure out what gets consted...

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Your are welcome.

            1 Reply Last reply
            0
            • C CPallini

              It should be

              void GetCarPicture(const CARPIC const * * carPicture)
              {
              //...
              }
              //...
              // Using the function
              const CARPIC const * car;
              GetCarPicture(&car);

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              These second consts don't appear to make sense: both refer to the CARPIC value, neither refers to the pointer holding the address. That's also what my compiler states (in a warning). To give some examples:

              const int *a; // pointer to const int
              int const *a; // pointer to const int (same as above!)
              const int const *a; // warning C4114: same type qualifier used more than once
              int *const a; // const pointer to int
              const int * const a; // const pointer to const int

              See also http://cdecl.ridiculousfish.com/?q=const+int+*+const+*+a%3B[^]

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              C 1 Reply Last reply
              0
              • E elelont2

                Thanks, worked great, will try to figure out what gets consted...

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                The two consts are duplicate. Both refer to the CARPIC value. Neither refers to the pointer. You might also want to check out my comment to CPallinis response in this thread.

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                1 Reply Last reply
                0
                • S Stefan_Lang

                  These second consts don't appear to make sense: both refer to the CARPIC value, neither refers to the pointer holding the address. That's also what my compiler states (in a warning). To give some examples:

                  const int *a; // pointer to const int
                  int const *a; // pointer to const int (same as above!)
                  const int const *a; // warning C4114: same type qualifier used more than once
                  int *const a; // const pointer to int
                  const int * const a; // const pointer to const int

                  See also http://cdecl.ridiculousfish.com/?q=const+int+*+const+*+a%3B[^]

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Yes, you are right.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups