Simple problem with pointers

M

mark

Hello,

I have two variables:
bool oldValues[2] = {true, false};
bool newValues[2] = {false, false};

Now I would like to copy to oldValues the values from newValues. I
don't want to copy it one by one (using for) but just copy the address
in memory which points to newValues into oldValues. How can I do that?

btw. Is it possible to inicialize such array in the presented way (i.e.
using = {true, false} ) after initialisation, i..e bool values[2];
values = {true, false} doesn't work, but maybe there is another way to
do it without for)??

Thank you very much, marko
 
W

Walter Roberson

mark said:
I have two variables:
bool oldValues[2] = {true, false};
bool newValues[2] = {false, false};
Now I would like to copy to oldValues the values from newValues. I
don't want to copy it one by one (using for) but just copy the address
in memory which points to newValues into oldValues. How can I do that?

You cannot. Once an array is declared, you should consider its
address to be constant (i.e., unchangable) within its execution lifetime.

btw. Is it possible to inicialize such array in the presented way (i.e.
using = {true, false} ) after initialisation, i..e bool values[2];
values = {true, false}
No.

doesn't work, but maybe there is another way to
do it without for)??

If you were using a structure, there is structure assignment.
For arrays, there is no mass assignment as such, but there is
memcpy()
 
B

Ben Pfaff

mark said:
I have two variables:
bool oldValues[2] = {true, false};
bool newValues[2] = {false, false};

Now I would like to copy to oldValues the values from newValues. I
don't want to copy it one by one (using for) but just copy the address
in memory which points to newValues into oldValues. How can I do that?

It sounds like you should read section 6, "Arrays and Pointers",
in the C FAQ.
 
R

Richard

mark said:
I have two variables:
bool oldValues[2] = {true, false};
bool newValues[2] = {false, false};
Now I would like to copy to oldValues the values from newValues. I
don't want to copy it one by one (using for) but just copy the address
in memory which points to newValues into oldValues. How can I do that?

You cannot. Once an array is declared, you should consider its
address to be constant (i.e., unchangable) within its execution
lifetime.

Reading between the lines I would guess he doesn't want to change the
address of the arrays : he wants to "quickly" copy the contents of one
into the other in some sort of fast memory copy.
btw. Is it possible to inicialize such array in the presented way (i.e.
using = {true, false} ) after initialisation, i..e bool values[2];
values = {true, false}
No.

doesn't work, but maybe there is another way to
do it without for)??

If you were using a structure, there is structure assignment.
For arrays, there is no mass assignment as such, but there is
memcpy()

Agreed, possibly he is looking for a memcpy solution?

Something along the lines of

memcpy(oldValues,newValues,sizeof(oldValues));

?
 
S

sam_cit

Agreed, possibly he is looking for a memcpy solution?

Something along the lines of

memcpy(oldValues,newValues,sizeof(oldValues));

?

This holds true that the values are stored in consequtive memory
locations, however i remember that data structure for an array doesn't
expect it to be consequtive, rather they indicate a mapping between the
memory location of the individual elements of the array and a key and
it is that the key is stored in a consequtive memory locations, i know
that this sounds like an array of pointers to any type, but i really
wonder what should be an ideal implementation for an array, any
comments???
 
N

Nick Keighley

This holds true that the values are stored in consequtive memory
locations,

they are, though there may be padding between the items.

forall i: &a < &a[i+1]
however i remember that data structure for an array doesn't
expect it to be consequtive, rather they indicate a mapping between the
memory location of the individual elements of the array and a key and
it is that the key is stored in a consequtive memory locations,
no.

i know
that this sounds like an array of pointers to any type, but i really
wonder what should be an ideal implementation for an array, any
comments???

a simple set of strictly increasing memory addresses. The memcpy()
above is completly correct.


--
Nick Keighley

"Half-assed programming was a time-filler that, like knitting,
must date to the beginning of human experience."
"A Fire Upon The Deep" by Verne Vinge
 
K

Keith Thompson

Nick Keighley said:
This holds true that the values are stored in consequtive memory
locations,

they are, though there may be padding between the items.

forall i: &a < &a[i+1]

[...]

No, there is no padding between elements of an array.

This implies, among other things, that any type's size must be a
multiple of its alignment.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top