/Magnus
mailto:pmw...@kiemw.ericsson.se
Not quite: the struct the variable is pointing to can not be assigned to at
all (through this pointer at least). However, the pointer can be made to
point to any pre-existing struct.
|> If I use (pointer->element = value) to assign values to a
|> struct *pointer, is there any good way of assigning the
|> values of this struct to the const struct* ? memcpy() ?
Memcpy should give you a warning for the same reason. It tries to change the
struct to which pointer currently points.
To give an example (Let us assume <string.h> and <stdlib.h> have been
included, and we are inside a function.)
struct example { int element; };
const struct example *pointer;
struct example existing;
struct example *allocated;
existing.element = 0; /* OK */
pointer->element = 0; /* Illegal: pointer does not currently point anywhere */
*pointer = existing; /* Illegal: pointer does not currently point anywhere */
memcpy(pointer,&existing,sizeof existing);
/* Illegal: *pointer cannot be modified */
pointer = &existing; /* OK */
*pointer = existing; /* Illegal: *pointer cannot be assigned to */
existing = *pointer /* OK */
pointer->element = 0; /* Illegal: pointer->element cannot be changed */
memcpy(pointer,&existing,sizeof existing);
/* Illegal: *pointer cannot be modified */
pointer = malloc(sizeof *pointer); /* OK */
pointer->element = 0; /* Illegal: pointer->element cannot be changed */
memcpy(pointer,&existing,sizeof existing);
/* Illegal: *pointer cannot be modified */
free(pointer); /* Illegal: free is like modifying *pointer */
free((struct example*)pointer);
/* OK: You know better than the compiler */
allocated = malloc(sizeof *pointer); /* OK */
allocated->element = 0; /* OK */
pointer = allocated; /* OK */
allocated = pointer /* Illegal: const cannot be assigned to nonconst */
*pointer = existing; /* Illegal: *pointer cannot be assigned to */
existing = *pointer /* OK */
pointer->element = 0; /* Illegal: pointer->element cannot be changed */
memcpy(pointer,&existing,sizeof existing);
/* Illegal: *pointer cannot be modified */
free(pointer); /* Illegal: free is like modifying *pointer */
free(allocated); /* OK */
existing = *pointer; /* Illegal pointer has been freed (when allocated was
freed.) */
Cheers
Tanmoy
--
tan...@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tan...@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
The purpose of the const keyword is to prevent you from assigning values
to the structure members. So my first reaction is that you should
reconsider why you want to bypass that protection in the first place.
But let's assume that you really do want to change a const struct
through a pointer. The simplest way is to use a cast to reassure
the compiler that you know what you're doing.
struct foobar
{
int foo;
int bar;
};
..
void changeFoobar( const struct foobar * pF )
{
struct foobar * pNonConst;
pNonConst = ( struct foobar * ) pF; /* cast away constness */
pNonConst->foo = 0;
}
I'm assuming that the pointer itself is not const; the structure to
which it points is const, as in the declaration:
const struct foobar * pF;
It is also possible for a const pointer to point to a non-const structure:
struct foobar * const pF;
In that case you can assign values to the structure members, but you
can't change the pointer to point to a different structure.
Or both can be const:
const struct foobar * const pF;
Scott McKellar
Southwestern Bell Telephone
St. Louis, MO
(314) 235-3595 jm4...@multi.sbc.com