By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,198 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,198 IT Pros & Developers. It's quick & easy.

change the address of one element of int table?

P: n/a
Is it possible to change the address of one element of int table?
My program would be much simpler, if the first element of the table
would be same as last one. I mean:
int *s;
s=(*int)malloc(sizeof(int)*10);
..
and no mater what I would do to this table I want s[0]==s[9] to be
true.
How to do this?

dopiotrko
Sep 20 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
do*******@o2.pl wrote:
Is it possible to change the address of one element of int table?
My program would be much simpler, if the first element of the table
would be same as last one. I mean:
int *s;
s=(*int)malloc(sizeof(int)*10);
You mean `(int*)', and it would be even better to leave
it out altogether.
..
and no mater what I would do to this table I want s[0]==s[9] to be
true.
How to do this?
There's no direct way: s[0] and s[9] are separate objects
and are able to hold different values. You'll need to exercise
some discipline of your own, such as

1) Whenever you store to either of them, store to the other
as well.

2) Instead of accessing s[i], access s[i % 9] (you can make
the array one place smaller in this case).

3) Don't access the elements of s[] directly, but always
through an array of pointers `int *sp[10]' which has
been initialized so sp[0] = &s[0], sp[1] = &sp[1], ...,
sp[8] = &s[8], sp[9] = &s[0 /* not 9 */].

--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 20 '08 #2

P: n/a
Thanks.
Very helpful
Especially last option.

dopiotrko

Sep 20 '08 #3

P: n/a
Thanks.
Very helpful
Especially last option.

dopiotrko

Sep 20 '08 #4

P: n/a
do*******@o2.pl writes:
Thanks.
Very helpful
Especially last option.
I may also be worth considering a circular linked list. Once set up,
you can repeat:

use(list_ptr->value);
list_ptr = list_ptr->next;

as often as you like, cycling round the values.

--
Ben.
Sep 20 '08 #5

P: n/a
<do*******@o2.plwrote in message news:
Very helpful
Especially last option.
Essentially you don't want an array, you want a wrapped buffer.

So maybe a structure and some macros

typedef struct
{
int *buff;
int capacity;
} CIRCULAR;

int getval(c, i) (c)->buff[(i)%(c)->capacity]

void putval(c, i, value) (c)->buff[(i)%(c)->capacity] = (value)

is the way to go.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.