Basic Array Question

K

kelvSYC

What do you do in C so that you have two arrays that point to the same
values (ie. changing one array would also change the other)?

Can something like this work?

int a[];
int b[];

b = a

Or maybe this?

int a[];
int *b;

b = a;

In either case, would something like a[0] = 1 be the same as b[0] = 1
(and a[0] == b[0] would be true)?
 
J

Joona I Palaste

kelvSYC said:
What do you do in C so that you have two arrays that point to the same
values (ie. changing one array would also change the other)?

Impossible. You can fake it by making a *pointer* and pointing it at
the array.
Can something like this work?
int a[];
int b[];
b = a
No.

Or maybe this?
int a[];
int *b;

Yes. This is what I was talking about.
In either case, would something like a[0] = 1 be the same as b[0] = 1
(and a[0] == b[0] would be true)?

In the latter case. The former case won't make it past the compiler.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"My absolute aspect is probably..."
- Mato Valtonen
 
K

kelvSYC

What do you do in C so that you have two arrays that point to the same
values (ie. changing one array would also change the other)?

Impossible. You can fake it by making a *pointer* and pointing it at
the array.
In either case, would something like a[0] = 1 be the same as b[0] = 1
(and a[0] == b[0] would be true)?

In the latter case. The former case won't make it past the compiler.

In the case where a is an array and b is a pointer to the first element
thereof, would a routine that figures out the number of elements in a
also work for b, then?
 
J

Joona I Palaste

kelvSYC said:
What do you do in C so that you have two arrays that point to the same
values (ie. changing one array would also change the other)?

Impossible. You can fake it by making a *pointer* and pointing it at
the array.
In either case, would something like a[0] = 1 be the same as b[0] = 1
(and a[0] == b[0] would be true)?

In the latter case. The former case won't make it past the compiler.
In the case where a is an array and b is a pointer to the first element
thereof, would a routine that figures out the number of elements in a
also work for b, then?

Depends on how the routines work. sizeof a would work, but sizeof b
would not, as it would only give you the sizeof of a pointer. But if
the routines use the elements themselves, instead of the array
containing them, for example by looking for a 0 value, then they would
work.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am lying."
- Anon
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top