alias for a variable

P

paresh

Hi Friends,

Can we have a alias of a variable in C.

Like I define an int---
int example;

Now I want another variable that is the alias for the "example". I
meant both example and another variable let say "example2" have the
same memory but we are refrencing with it by two variables.
I need both to be global variables.

I am not talking about call by reference and I can use #define. Am I?

Regards,
Paresh
 
R

Richard Heathfield

paresh said:
Hi Friends,

Can we have a alias of a variable in C.

Like I define an int---
int example;

Now I want another variable that is the alias for the "example". I
meant both example and another variable let say "example2" have the
same memory but we are refrencing with it by two variables.

Yes, you could use #define, or you could simply point two pointers at
the same object.
I need both to be global variables.

Why on earth would you need to do that?
I am not talking about call by reference

No such thing in C.
and I can use #define.

Yes, you can.

Am you what?
 
R

robertwessel2

Hi Friends,

Can we have a alias of a variable in C.

Like I define an int---
int example;

Now I want another variable that is the alias for the "example". I
meant both example and another variable let say "example2" have the
same memory but we are refrencing with it by two variables.
I need both to be global variables.

I am not talking about call by reference and I can use #define. Am I?


Are you perhaps looking for a union?
 
C

Chris Dollin

paresh said:
Hi Friends,

Can we have a alias of a variable in C.

Like I define an int---
int example;

Now I want another variable that is the alias for the "example". I
meant both example and another variable let say "example2" have the
same memory but we are refrencing with it by two variables.
I need both to be global variables.

Never mind the answers: the real question is, /why/ do you want to
do this? It looks like a real Asking For Trouble to me. There's
quite enough necessary aliasing in programming already.
 
N

Nick Keighley

No I am not looking for union. Yes the easiast way to do this is
#define.
Is there any other way

why do you want to do this? What was wrong with Richard's
suggestion to use pointers?

#define and global variables sound like an accident
waiting to happen.
 
P

paresh

Never mind the answers: the real question is, /why/ do you want to
do this? It looks like a real Asking For Trouble to me. There's
quite enough necessary aliasing in programming already.


There is already a large chuck of code, I want to use that.
There is a sturcture(quite large) that is used at two different
location.
I dont want to waste memory(embedded device I dont have much) so i am
trying to use the same sturcture by using extern. If I use pointer I
have to
do a lot of mechanical changes(stuct to pointer to stuct) in one
module.

Regards
 
C

Chris Dollin

paresh said:
There is already a large chuck of code, I want to use that.
There is a sturcture(quite large) that is used at two different
location.
I dont want to waste memory(embedded device I dont have much) so i am
trying to use the same sturcture by using extern. If I use pointer I
have to
do a lot of mechanical changes(stuct to pointer to stuct) in one
module.

Either the two structures /should/ be different objects, in which
case leave well alone, or they should be the same object (so how
did /that/ happen?), in which case delete one of them from the
code -- you will have to do that anyway, if you want to save space --
and change all the references to it to refer to the other one.

I don't see why you want to mess around with aliasing when what
you want to is a cut-and-rename.
If I use pointer I have to
do a lot of mechanical changes(stuct to pointer to stuct) in one
module.

Well, yes, but that looks like a one-liner

ENTER gs/name./ptrName->/ RETURN

or however your local editor handles a global substitute.
 
N

Nick Keighley

paresh wrote:
Never mind the answers: the real question is, /why/ do you want to
do this? It looks like a real Asking For Trouble to me. There's
quite enough necessary aliasing in programming already.

you should normally trim sigs

There is already a large chuck of code, I want to use that.
There is a sturcture(quite large) that is used at two different
location.
I dont want to waste memory(embedded device I dont have much) so i am
trying to use the same sturcture by using extern. If I use pointer I
have to
do a lot of mechanical changes(stuct to pointer to stuct) in one
module.

I'd (probably) bite the bullet and do the global edit(s).

my_struct. => my_struct->
&my_struct => my_struct
 
J

John Turner

There is already a large chuck of code, I want to use that.
There is a sturcture(quite large) that is used at two different
location.
I dont want to waste memory(embedded device I dont have much) so i am
trying to use the same sturcture by using extern. If I use pointer I
have to
do a lot of mechanical changes(stuct to pointer to stuct) in one
module.

Regards

May I suggest a good old search and replace?
 
R

Richard

Richard Heathfield said:
paresh said:


Yes, you could use #define, or you could simply point two pointers at
the same object.


Why on earth would you need to do that?


No such thing in C.

This is misleading for a new person to C.

Arrays are, by default, "passed by reference" in any semi reasonable
interpretation of it phrase.
 
C

Chris Dollin

Richard said:
This is misleading for a new person to C.

Arrays are, by default, "passed by reference" in any semi reasonable
interpretation of it phrase.

Only in the sense that arrays aren't passed /at all/ in C.
The decay-to-pointer effect is not specific to argument
passing and I believe it does a disservice to C newbies to
connect it to the full-blow pass-by-reference found in
some other languages.

Of course, I'm not semi-reasonable, just detached-reasonable.
 
S

santosh

Richard said:
This is misleading for a new person to C.

Arrays are, by default, "passed by reference" in any semi reasonable
interpretation of it phrase.

True pass by reference doesn't exist in C. Pointers are used to
simulate it, with arrays being just a special case of this feature.
 
R

Richard Heathfield

Richard said:
This is misleading for a new person to C.

No, it isn't. What is misleading is the pretence that there is such a
thing as pass-by-reference in C.
Arrays are, by default, "passed by reference"

No, arrays aren't passed *at all* in C.
 
C

CBFalconer

Richard said:
Richard said:

No, it isn't. What is misleading is the pretence that there is
such a thing as pass-by-reference in C.


No, arrays aren't passed *at all* in C.

Since Richard <noname> wants to intermix languages, maybe he will
describe to us how to implement 'pass by name' in C? Hint: read-up
on Algol.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top