Pointers and constants

F

filia&sofia

Hello, I have constructed a simple array (table) that has pointers as
elements. Pointers point to an integer and they are typed as "pointer
to int". When I manipulate the array, I would like to put the pointers
to point at numeric constants e.g. 43. The problem is that I can not
put a numeric constant to array (because array is typed) nor get the
pointers point to constants (like &43). Any ideas how to solve this?
Thank you.

---
int integer = 4;
int *ptr = &integer;

For example an array of size 5:
[ptr]
[ptr]
[ptr]
[ptr]
[ptr]

After manipulation it should be something like this:
[ptr]
[6] or pointer to 6
[ptr]
[N] or pointer to N, N is an integer
[ptr]
 
D

Default User

filia&sofia said:
Hello, I have constructed a simple array (table) that has pointers as
elements. Pointers point to an integer and they are typed as "pointer
to int". When I manipulate the array, I would like to put the pointers
to point at numeric constants e.g. 43. The problem is that I can not
put a numeric constant to array (because array is typed) nor get the
pointers point to constants (like &43). Any ideas how to solve this?

Why do you want an array of pointers to single integers? Why not make
the table ints?

Otherwise, you'll need ints for which you have or can get the address
of. One way would be to use malloc() to dynamically create them.




Brian
 
R

Richard Tobin

filia&sofia said:
Hello, I have constructed a simple array (table) that has pointers as
elements. Pointers point to an integer and they are typed as "pointer
to int". When I manipulate the array, I would like to put the pointers
to point at numeric constants e.g. 43. The problem is that I can not
put a numeric constant to array (because array is typed) nor get the
pointers point to constants (like &43). Any ideas how to solve this?

In C90 you need to put the value in a variable, and point to that.

In C99, you can use a compound literal:

ptr[2] = (int []){23};

This produces an array of one int, and (in the usual way) becomes a
pointer to that single element.

Bear in mind that the compound literal has the "obvious" scope: if you
need it to have static scope you might will still have to declare
static variables (or a single static array) containing the values in
question.

-- Richard
 
F

filia&sofia

Why do you want an array of pointers to single integers? Why not make
the table ints?

Well, the idea behind this is that it is faster to change the value of
the integer for the whole array. For N-item array it would take N
assignments and in my program it takes only one assignment.
Otherwise, you'll need ints for which you have or can get the address
of. One way would be to use malloc() to dynamically create them.

OK. I realize that my question was a bit poorly formulated. Let me
clarify, what I try to accomplish. Take N-bit array and ints as
elements. The program replaces some ints (possibly all of them) in the
array. For example, N=5

____1st____2nd___
[3] [3] [3]
[1] [7] [7]
[2] => [2] => [0]
[2] [2] [0]
[2] [2] [0]

First replacement can not be made faster (one assignment and directly
accessed by index), but second replacement would require only one
assignment (pointed integer = 0) instead of three assignments (array[2]
=0, array[3]=0, array[4]=0, and we have to go through the whole table
to even find the 2's). In short, (1+0)=1 computing operations instead
of (5+3)=8 operations (searching+assigning).

So, in a way, using pointers the algorithm would be efficient as the
size of the array increases.
 
F

filia&sofia

Two more things. Array sizes can be anything from 1 to 2^32 or even
2^N. How about using unions to solve the problem?
 
D

Default User

filia&sofia said:
Why do you want an array of pointers to single integers? Why not
make the table ints?

Well, the idea behind this is that it is faster to change the value of
the integer for the whole array. For N-item array it would take N
assignments and in my program it takes only one assignment.
Otherwise, you'll need ints for which you have or can get the
address of. One way would be to use malloc() to dynamically create
them.

OK. I realize that my question was a bit poorly formulated. Let me
clarify, what I try to accomplish. Take N-bit array and ints as
elements. The program replaces some ints (possibly all of them) in the
array. For example, N=5

____1st____2nd___
[3] [3] [3]
[1] [7] [7]
[2] => [2] => [0]
[2] [2] [0]
[2] [2] [0]

First replacement can not be made faster (one assignment and directly
accessed by index), but second replacement would require only one
assignment (pointed integer = 0) instead of three assignments
(array[2] =0, array[3]=0, array[4]=0, and we have to go through the
whole table to even find the 2's). In short, (1+0)=1 computing
operations instead of (5+3)=8 operations (searching+assigning).


Well, your example tried to take the address of an integer literal,
which even if you had been able to do so would not have been mutable
later and so wouldn't do what you have there.

The solution is either use the address of an int (possibly from another
array) or dynamically allocate one and store in the table locations.

Something like:

int i, j;

int val[3] = {1,2,3};

int *table[2][3] = {{&val[0],&val[1],&val[2]},
{&val[0],&val[1],&val[2]}};

for(i = 0; i < 2; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d ", *table[j]);
}
putchar('\n');
}

val[1] = 6;

for(i = 0; i < 2; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d ", *table[j]);
}
putchar('\n');
}


Brian
 
A

Antoninus Twink

On 6 Jan 2009 at 23:14, Default User wrote:
[something useful]

Is this you again, Han? Or are we seriously meant to believe that for
the first time in his life Default Loser has posted a useful answer to a
technical question instead of sniping net-nannying or killfile
melodrama?
 
R

Richard

Antoninus Twink said:
On 6 Jan 2009 at 23:14, Default User wrote:
[something useful]

Is this you again, Han? Or are we seriously meant to believe that for
the first time in his life Default Loser has posted a useful answer to a
technical question instead of sniping net-nannying or killfile
melodrama?

It must be Han. I took a double check too. Bwian has either seen the
error of his ways or Han has a pulled another stormer.
 
A

Antoninus Twink

Since B.C. has a long history of smooching people like Richard Bos, my
guess is that Brian is trying to reassert his reason for being on an
unmoderated C programming i newsgroup

Why B.C. though? I seem to remember his name was something along the
lines of "Bwian Roderburn" before he gave himself the stupid nickname.

Though it's easy to imagine that the poor saps who have to work with him
might know him as "Brian the ****", so maybe that explains it...
 
R

Richard Bos

Han from China - Master Troll said:
Since B.C. has a long history of smooching people like Richard Bos,

I can state with my hand on my heart that Default has never even tried
to smooch me, thanked be the bearded Old Ones of Ling.

Richard
 
R

Richard Bos

Han from China - Master Troll said:
Hmm, you could be right. I'm aware of only his first name. I couldn't
think of anyone else to fit Richard Bos's hypocritical/idiotic "B.C."

That's because you are in both the first _and_ the two-b'th category.
But then, you knew that, right?

Richard
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top