473,386 Members | 1,841 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,386 software developers and data experts.

double array pointer modification

Hi all,
another newbie question from me, but here goes.

Ok, I have a double array defined as follows:

int cluster[5][5];

now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides. now for the program i am
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].

my question is how do i make that reference change?
i tried the following but they gave compile errors or didnt work:

&cluster[4]=cluster[0];
cluster[4]=cluster[0];
&cluster[4][0]=&cluster[0][0];

thanks in advance for your help!
-- Kiran

Jan 21 '07 #1
5 1658
xmx
cluster[4] is not a left value

Kiran wrote:
Hi all,
another newbie question from me, but here goes.

Ok, I have a double array defined as follows:

int cluster[5][5];

now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides. now for the program i am
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].

my question is how do i make that reference change?
i tried the following but they gave compile errors or didnt work:

&cluster[4]=cluster[0];
cluster[4]=cluster[0];
&cluster[4][0]=&cluster[0][0];

thanks in advance for your help!
-- Kiran
Jan 21 '07 #2
Kiran wrote:
Hi all,
another newbie question from me, but here goes.

Ok, I have a double array defined as follows:

int cluster[5][5];

now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides. now for the program i am
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].
Why on earth would you want to do that other than to confuse the reader?

--
Ian Collins.
Jan 21 '07 #3

Ian Collins wrote:
Kiran wrote:
Hi all,
another newbie question from me, but here goes.

Ok, I have a double array defined as follows:

int cluster[5][5];

now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides. now for the program i am
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].
Why on earth would you want to do that other than to confuse the reader?

--
Ian Collins.
The reason I want to do this is because I am combining clusters for
Kruskal's algorithm, and this is one way I thought of doing it.

Jan 21 '07 #4
"Kiran" <Ki*********@gmail.comwrites:
another newbie question from me, but here goes.

Ok, I have a double array defined as follows:

int cluster[5][5];

now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides.
Um, sort of. cluster[0] is the first (0th) row of your array; in most
contexts, it gives you the *address* of the first row.

Section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has some
excellent information on this kind of thing.
now for the program i am
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].

my question is how do i make that reference change?
i tried the following but they gave compile errors or didnt work:

&cluster[4]=cluster[0];
cluster[4]=cluster[0];
&cluster[4][0]=&cluster[0][0];
Given the way you've declared cluster, what you're trying to do can't
be done.

You might consider using an array of pointers rather than an array of
integers. If x and y are pointers to int (of type int*), then setting
x = y means that modifying *x will modify *y. It's the same thing if
the pointers are array elements rather than declared variables.

--
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.
Jan 21 '07 #5


On Jan 20, 8:01 pm, "Kiran" <Kiran.Ka...@gmail.comwrote:
Hi all,
another newbie question from me, but here goes.

Ok, I have a double array defined as follows:

int cluster[5][5];

now, i know that if you say for example cluster[0], this will give me
the address of where cluster[0][0] resides. now for the program i am
cluster is an array of 5 array of 5 int. clluster[0] is the first of
the 5 arrays. cluster[4] is the last.

Except when the operand of sizeof or &, an expression of type array of
T evaluates to the address of the first element of the array with type
pointer to T. Since cluster is an expression of type array, it will
evaluate to the address of cluster[0] with type pointer to array of 5
int, syntactically int(*)[5]. cluster[4] is also an expression of type
array. It will evaluate to the address of cluster[4][0] with type
pointer to int.
writing, at some point in the program i may want cluster[4] to
reference to cluster[0], meaning that after I do the referencing, if i
say cluster[4][1] = 2, then i should also have cluster[0][1] to be
equal to 2, because now cluster[4] points to the same address as
cluster[0].
Since neither cluster[4] nor cluster[0] are pointers, they area of
memory they refer to cannot be altered.
>
my question is how do i make that reference change?
i tried the following but they gave compile errors or didnt work:

&cluster[4]=cluster[0];
The result of the & operator is not a modifiable l-value and therefore
cannot appear on the left of the assignemtn operator.
cluster[4]=cluster[0];
An array is not a modifiable l-value so see above.
&cluster[4][0]=&cluster[0][0];

thanks in advance for your help!
-- Kiran
You cannot do this with arrays. Ignoring the debate of whether it is a
god idea or not ( vote no), you can do it with pointers.

int icluster[5][5]; /* this is your normal 5x5 array */
int (*pcluster[5])[5] /* an array of five pointers each pointing
to an array of five int */
= {icluster[0], icluster[1], icluster[2], icluster[3],
icluster[4]};

All references to icluster should be done through pcluster, as in
pcluster[4][1] = 2;

At some point, you can assign pcluster[4] the value in pcluster[0] and
every subsequent reference to icluster through pcluster[4] will affect
the elements of icluster[0].

Jan 21 '07 #6

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

Similar topics

4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: ur8x | last post by:
I have a double pointer and a 2D array: int mat, **ptr; ptr = mat; mat = 3; Although mat and ptr are pointing at the same address, &ptr and &mat are not, why?
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
0
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib...
0
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.