Using a variable which is a pointer

P

polas

Hello all - I have a question which is perhaps a little unusual. I am
creating a language translator which uses C as the target language. In
C, when calling a function and passing variables as arguments in, I
wish to pass a pointer of the variable to the function. However, due
to internals of the language translator it will be difficult to mark
when variables are in fact pointers and need a * prefix. I was
therefore wondering if there is a way to pass a pointer but refer to
the contents of the memory location it points to as if the pointer
were that location. For example

void examplefn (int * me)
{
me=me + 1;
}

I would like the above function to add one to the integer value held
in the memory location pointed to be "me" rather than the pointer
address as the above does at the moment.

Cheers,
Nick
 
C

Christopher Benson-Manica

polas said:
void examplefn (int * me)
{
me=me + 1; *me=*me+1;
}
I would like the above function to add one to the integer value held
in the memory location pointed to be "me" rather than the pointer
address as the above does at the moment.

Simply indirect through the pointer as above.
 
P

polas

Simply indirect through the pointer as above.

The idea behind the question (I possibly did not explain it
particularly well) is that in order to make life simpler, not keeping
track of whether or not "me " is being accessed inside a function
(i.e. if it is actually a pointer) or the main body of the program is
ideal, and so in this case the translator would not know to add a "*"

Nick
 
R

Richard Heathfield

polas said:
Hello all - I have a question which is perhaps a little unusual. I am
creating a language translator which uses C as the target language. In
C, when calling a function and passing variables as arguments in, I
wish to pass a pointer of the variable to the function. However, due
to internals of the language translator it will be difficult to mark
when variables are in fact pointers and need a * prefix. I was
therefore wondering if there is a way to pass a pointer but refer to
the contents of the memory location it points to as if the pointer
were that location. For example

void examplefn (int * me)
{
me=me + 1;
}

I would like the above function to add one to the integer value held
in the memory location pointed to be "me" rather than the pointer
address as the above does at the moment.

Presumably you know you're in trouble when you're dealing with arrays,
but for the simple cases such as the one outlined, one possibility is
to use temps. This shouldn't be too hard to automate.

For example:

void examplefn(int * me)
{
/* automagically generated temp section start */
int me_ = *me;
/* temp section end */

/* code goes here */
++me_; /* uses the temp */

/* automagically generated fiddle-to-make-it-work */
*me = me_;
*/
}

With arrays, it's trickier because you don't actually get an array, just
a pointer to the first element in that array. If you passed a
pointer-to-array, however (e.g. int arr[10]; foo(&myarr); with foo
starting off with int (*p)[10] = *parm;, and used precisely the same
technique, I *think* you'd be fine, but frankly it's not an area of C
I've explored (since I'm not a huge fan of passing array pointers
around the place), so you'll be wanting a response from someone who
actually knows for sure or can be bothered to look it up in the
Standard.

But for normal stuff, the above will work just fine.
 
C

Christopher Benson-Manica

polas said:
The idea behind the question (I possibly did not explain it
particularly well) is that in order to make life simpler, not keeping
track of whether or not "me " is being accessed inside a function
(i.e. if it is actually a pointer) or the main body of the program is
ideal, and so in this case the translator would not know to add a "*"

I admit that I skimmed your entire first paragraph and answered what I
thought your question was... my apologies.
 
B

Ben Bacarisse

<snip>

Don't quote sigs.
The idea behind the question (I possibly did not explain it
particularly well) is that in order to make life simpler, not keeping
track of whether or not "me " is being accessed inside a function
(i.e. if it is actually a pointer) or the main body of the program is
ideal, and so in this case the translator would not know to add a
"*"

You may have to re-think that design. However... if you known that a
pointer is needed at some point in the translation but your translator
"forgets" only when it is generating the function's definition, you can
arrange for the C pre-processor to remember for you:

void examplefn(int EXAMPLEFN_P1 me)
{
(EXAMPLEFN_P1 me) = (EXAMPLEFN_P1 me) + 1;
}

and you output (maybe into another file that you #include)

#define EXAMPLEFN_P1 *

or

#define EXAMPLEFN_P1

when you know which is required. The pre-processor can help make this
neater if need the C to be more readable.
 
P

polas

polas said:


Hello all - I have a question which is perhaps a little unusual. I am
creating a language translator which uses C as the target language. In
C, when calling a function and passing variables as arguments in, I
wish to pass a pointer of the variable to the function. However, due
to internals of the language translator it will be difficult to mark
when variables are in fact pointers and need a * prefix. I was
therefore wondering if there is a way to pass a pointer but refer to
the contents of the memory location it points to as if the pointer
were that location. For example
void examplefn (int * me)
{
me=me + 1;
}
I would like the above function to add one to the integer value held
in the memory location pointed to be "me" rather than the pointer
address as the above does at the moment.

Presumably you know you're in trouble when you're dealing with arrays,
but for the simple cases such as the one outlined, one possibility is
to use temps. This shouldn't be too hard to automate.

For example:

void examplefn(int * me)
{
/* automagically generated temp section start */
int me_ = *me;
/* temp section end */

/* code goes here */
++me_; /* uses the temp */

/* automagically generated fiddle-to-make-it-work */
*me = me_;
*/

}

With arrays, it's trickier because you don't actually get an array, just
a pointer to the first element in that array. If you passed a
pointer-to-array, however (e.g. int arr[10]; foo(&myarr); with foo
starting off with int (*p)[10] = *parm;, and used precisely the same
technique, I *think* you'd be fine, but frankly it's not an area of C
I've explored (since I'm not a huge fan of passing array pointers
around the place), so you'll be wanting a response from someone who
actually knows for sure or can be bothered to look it up in the
Standard.

But for normal stuff, the above will work just fine.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Thanks for the reply - good idea (I think actually slightly easier for
me to have the temporary variable idea the other way round so the
argument is a temp variable) and probably my only option. As far as
arrays go I think you are also correct on that - I am implementing it
now and atm it seems to be the correct approach

Cheers,
Nick
 
C

CBFalconer

polas said:
.... snip ...

void examplefn (int * me)
{
me=me + 1;
}

I would like the above function to add one to the integer value
held in the memory location pointed to be "me" rather than the
pointer address as the above does at the moment.

Just write "*me = *me + 1;" or "*me += 1;". All done.
 
J

jacob navia

polas said:
Hello all - I have a question which is perhaps a little unusual. I am
creating a language translator which uses C as the target language. In
C, when calling a function and passing variables as arguments in, I
wish to pass a pointer of the variable to the function. However, due
to internals of the language translator it will be difficult to mark
when variables are in fact pointers and need a * prefix. I was
therefore wondering if there is a way to pass a pointer but refer to
the contents of the memory location it points to as if the pointer
were that location. For example

void examplefn (int * me)
{
me=me + 1;
}

I would like the above function to add one to the integer value held
in the memory location pointed to be "me" rather than the pointer
address as the above does at the moment.

Cheers,
Nick

If you use references you can do that. Use the lcc-win32 compiler
system. That is a C compiler that accepts references. Or any C++
compiler will do.

Within the function you address the object exactly as you want.

jacob
 
R

Richard Heathfield

jacob navia said:
If you use references you can do that.

C doesn't have references. If C is his target language, which he says it
is, there is no point in trying to do this with references.
Use the lcc-win32 compiler

Same old spam. He's writing in C, not lcc-win32. IIRC he hasn't
mentioned his platform, and there's no reason to assume it's Windows.
And even if it is, he would do better to use a compiler written by
someone who knows C rather better than the author of lcc-win32.
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top