Re: is there a way to do this...
- From: "Novitas" <ken@xxxxxxxxxxxx>
- Date: 9 Sep 2005 19:22:56 -0700
sumit1680@xxxxxxxxxxxxxx wrote:
> Novitas wrote:
> > You can achieve the behavior you seek simply by not passing the
> > variable by "reference" but by value -- that is remove the preceding &
> > from the argument. The subroutine will have to have its prototype
> > changed from int * to int.
> >
> > If there is more to your question, you will need to explain further.
>
> Agreed .... but i keep my original approach and yet want to do so....
> in a way what i want is to somehow increment pc (program counter ) to
> skip the execution of line i=6 .agreed this introduces architecture
> dependency ,yet , implementation wise how to achive the same...
In that case, I'd recommend something like the following:
void main()
{
int i=5;
if (change(&i))
i=6;
printf("%d",i); //it should print 23 instead of 6
}
int change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23
not 6
return 0; /// Note, -- in the general case there would be
conditional
/// logic deciding on a 1 or 0 as the return value.
}
You could also use longjmp to cause return to a different place in the
program, but it's a lot messier and tends to excessively couple the
internals of the subroutine to the main routine.
The bottom line is that one has to live within the limits of what the
language
provides with a few prescribed exceptions (none of which are in
evidence
given the information you provided so I won't go into that)
It is necessary for the change routine to communicate in some manner
whether or not the statement that follows will or will not be executed.
Giving it a
boolean return value is the most straightforward way I can think of.
.
- Follow-Ups:
- Re: is there a way to do this...
- From: Christopher Benson-Manica
- Re: is there a way to do this...
- References:
- is there a way to do this...
- From: sumit1680
- Re: is there a way to do this...
- From: Keith Thompson
- Re: is there a way to do this...
- From: sumit1680
- Re: is there a way to do this...
- From: Novitas
- Re: is there a way to do this...
- From: sumit1680
- is there a way to do this...
- Prev by Date: Re: Correct Identation/Contex can solve the too many compiler error messages problem when a closing bracket is missing.
- Next by Date: Re: C, lexical
- Previous by thread: Re: is there a way to do this...
- Next by thread: Re: is there a way to do this...
- Index(es):
Relevant Pages
|
|