473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a variable which is a pointer

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

Jul 6 '07 #1
6 1376
polas <ni**@helpforce .comwrote:
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Jul 6 '07 #2
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
Jul 6 '07 #3
On 6 Jul, 15:23, Richard Heathfield <r...@see.sig.i nvalidwrote:
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

Jul 6 '07 #4
polas wrote:
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
Jul 6 '07 #5
polas wrote:
>
.... 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.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 6 '07 #6
jacob navia said:
polas wrote:
>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.

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.

--
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
Jul 6 '07 #7

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

Similar topics

10
2253
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template...
17
4177
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset Set rs = Me.RecordsetClone rs.Find "=" & lngContractID If Not rs.EOF Then Me.Bookmark = rs.Bookmark I must site the Heisenberb Uncertainty...
14
2525
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable = "a". Considering the above I have a function, which is declared and defined to take any type of parameter with void* return-type foo (void *a); In...
9
10681
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
0
3916
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header,...
2
5030
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when...
17
2408
by: Johs32 | last post by:
When I make a pointer I have read that if I would like to use it in another function, I need to malloc it first else it will disapear after the function returns. In this code I do not use malloc, but it still works and the function print_me() prints the correct text. Why does it work eventhough I do not use malloc? johs
6
3667
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) { List<TimeSpantimes = new List<TimeSpan>(); DateTime start; DateTime end; for (int j = 0; j < 1000; j++)
65
3845
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.