On Thursday, August 14, 2008 at 10:18:32 (-0700) Alan W. Irwin writes:
 > On 2008-08-14 09:58-0500 Maurice LeBrun wrote:
 > 
 > > For C & C++, a const pointer means the memory it points to cannot be 
 > > altered.
 > > The pointer can.  So for example
 > >
 > >    const char *foo = "bar";
 > >
 > > (or later reassignment) is perfectly legit.
 > 
 > After years of dabbling at C, this pointer stuff still makes my head spin so
 > could you expand a bit more, Maurice?  What actually happens with the above
 > statement?  After the declaration of foo as a pointer to a const char is foo
 > initialized as a pointer to the first character in the const "bar" string?

Correct.

 > Or would a better way to look at it be that all character strings like "bar"
 > can be interpreted as pointers so the above is simply a pointer assignment?

This too is correct.  All string literals in a program are stored in the
executable, as a `strings <program>` will demonstrate.  The proper way to
refer to these is via a const char* since they are considered fixed.

If you want a char* string that's modifiable, you need to define a character
buffer and copy a string into it.  Whenever unsure, just construct a simple
test and try it (I keep several handy for that purpose).  For example, what
if you assign a literal to a char* and then try to modify it?

    char *foo = "0"; 
    foo[0] = "1"; 

then gcc complains:
tst.cc: In function ‘int main(int, char**)’:
tst.cc:15: error: invalid conversion from ‘const char*’ to ‘char’

because apparently it's smart enough to know that even tho foo is defined as
char*, it's actually pointing to a const char*.  I'm no language lawyer, but
it would seem that this syntax is supported for legacy reasons.

 > Is this a good example of the later reassignment you referred to?
 > 
 > *foo = "whatever";
 > 
 > or would it be
 > 
 > foo = "whatever";
 > ?

The latter.  *foo gives you what foo is pointing at.  E.g.

    char *foo = "0123"; 
    char single = *foo; 
or
    char single = foo[0];

then single == '0'.  It helps to remember that *foo and foo[0] mean exactly
the same thing.

 > The first seems consistent with the mnemonic for the declaration, but I am
 > virtually positive the latter is correct instead since *foo would refer to
 > the first read-only character in "bar" which would make no sense for an
 > assignment.

Right, since you are assigning to a pointer.  If you assign to a char, you're
ok.

 > Thanks in advance for trying to deal with my confusion on these issues.  I
 > guess the start of my confusion all these years has been what I consider to
 > be a rather illogical mnemonic for pointer declarations using the
 > dereferencing operator.  I guess I just got off on the wrong foot with C
 > pointers and have never recovered.  :-)

:)  It's a little wierd coming from a Fortran environment, for sure.  But good
stuff to know.

-- 
Maurice LeBrun

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel

Reply via email to