Re: const char ** and char ** incompatibility

Discussion in 'C Programming' started by Paul N, Dec 30, 2013.

  1. Paul N

    Paul N Guest

    On Thursday, 19 December 2013 08:05:55 UTC, wrote:
    > Hi,
    >
    > const char *cp; char *p;
    >
    > const char **cpp; char **pp;
    >
    > why cp = p is ok, but cpp = pp have a warning: pointer incompatible?
    >


    cp = p is OK, because you are saying that you want cp to point at the same thing as p but you do not want to modify it using cp. No problem there.

    cpp = pp has a problem. Watch:

    cp = "Don't modify this!!"; // we don't want to modify this, using a const pointer will prevent us from doing so

    pp = &p; // OK, no consts here

    cpp = pp; // this line is the problem, as everything else is OK

    *cpp = cp; // should be OK, making cpp point at a const pointer
    // but it makes p equal to cp

    *p = 'x'; // should be OK, but actually modifies our string.

    Hope that helps.
    Paul.
    Paul N, Dec 30, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    24
    Views:
    800
    Netocrat
    Oct 30, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    996
    Ian Collins
    May 9, 2006
  3. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  4. Ben Bacarisse

    Re: const char ** and char ** incompatibility

    Ben Bacarisse, Dec 19, 2013, in forum: C Programming
    Replies:
    1
    Views:
    168
    James Kuyper
    Dec 30, 2013
  5. Edward A. Falk

    Re: const char ** and char ** incompatibility

    Edward A. Falk, Dec 19, 2013, in forum: C Programming
    Replies:
    0
    Views:
    91
    Edward A. Falk
    Dec 19, 2013
Loading...

Share This Page