473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Simple problem with pointers

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

Dec 14 '06 #1
6 1219
In article <11*********************@73g2000cwn.googlegroups.c om>,
mark <mk*********@gmail.comwrote:
>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()
--
Programming is what happens while you're busy making other plans.
Dec 14 '06 #2
"mark" <mk*********@gmail.comwrites:
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.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Dec 14 '06 #3
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11*********************@73g2000cwn.googlegroups.c om>,
mark <mk*********@gmail.comwrote:
>>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));

?
Dec 15 '06 #4
>
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???

Dec 15 '06 #5
sa*****@yahoo.co.in wrote:
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,
they are, though there may be padding between the items.

forall i: &a[i] < &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

Dec 15 '06 #6
"Nick Keighley" <ni******************@hotmail.comwrites:
sa*****@yahoo.co.in wrote:
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,

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

forall i: &a[i] < &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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: trustron | last post by:
Hi all, I have got a pointer question. I was told that a pointer is a variable holding a memory address. SO:
7
by: Erik Borgstr?m | last post by:
Hi, I simply want to use simple matrices of ints or doubles in C++ and I want all the good: 1) be able to use double-index, e.g. m 2) not have to use pointers at all 3) do it fast I know...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
24
by: laredotornado | last post by:
Hello, Are all pointer types the same length? My instinct tells me yes, but I just wanted to confirm with the experts. So if I have typedef struct { char* field1; int field2; }...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
41
by: SkyBlue | last post by:
Hi, can someone explain why the following simple C code segfaulted? I've stared it for 30mins but couldn't find the problem. Thx in advance #include <stdio.h> int main() { char *one; char...
10
by: Ivan Vecerina | last post by:
Here's a relatively simple code snippet: #include <memory> class Base { public: Base(); virtual ~Base(); virtual void f(int a, char const* name);
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.