is there a way to do this...

S

sumit1680

given below is a code snippet...

void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}



void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}

is this possible in C?
 
A

Anton Petrusevich

given below is a code snippet...

void main()
{
int i=5;
change(&i);
i=6; (1)
printf("%d",i); //it should print 23 instead of 6
}
void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}

is this possible in C?

No. The only possibility I see: simultaneously update "i" variable by
different threads. I.e. change() creates a thread and does something that
takes time until main thread reaches point (1) and then changing thread
changes value of i. But I can not imagine a way how to do that reliably.
 
R

Richard Heathfield

(e-mail address removed) said:
given below is a code snippet...

void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}

It might do just that, since the program exhibits undefined behaviour and
contains a syntax error.

main returns int, not void.
printf is a variadic function, so it requires a valid function prototype in
scope.
 
K

Krishanu Debnath

given below is a code snippet...

void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}

I have a 'very close' solution. ;-)
void change( int *i)
{
*i=23;
printf("%d\n", i);
exit(0); /* fair enough? */
//write some code here so that output of printf in main will be 23 not
6
}

is this possible in C?

Krishanu
 
K

Keith Thompson

given below is a code snippet...

void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}



void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}

is this possible in C?

Why do you want to do this?
 
K

Keith Thompson

but you can't say what will it print, sometimes 6 or some time 23

Sigh.

Please provide some context. Don't assume that your readers have easy
access to the article to which you're replying.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
S

sumit1680

well just for interest sake! i am told this is possible without threads
and the answer is predictable!
but the catch is that it is architecture predictable.

and for all who r concerned with " void " before main... consider " int
" if u like so.agreed to the fact that main returns int, not void.
pls dwell upon the question not its discrepancies.
 
D

Duke

I don't have a complete code of your error code, and I write a simple
code like yours, it works well and correct. And I think you should
provide some context about your program.

#include <stdio.h>
void change(int *);
int main(void)
{
int i = 5;
printf("1. The integer value is %d.\n", i); //It will print 5
change(&i);
printf("2. The integer value is %d.\n", i); //It will print 23
i = 6;
printf("3. The integer value is %d.\n", i); //It will print 6
system("PAUSE");
return 0;
}
void change(int * pst)
{
*pst = 23;
}
 
S

sumit1680

Duke said:
I don't have a complete code of your error code, and I write a simple
code like yours, it works well and correct. And I think you should
provide some context about your program.

#include <stdio.h>
void change(int *);
int main(void)
{
int i = 5;
printf("1. The integer value is %d.\n", i); //It will print 5
change(&i);
printf("2. The integer value is %d.\n", i); //It will print 23
i = 6;
printf("3. The integer value is %d.\n", i); //It will print 6
system("PAUSE");
return 0;
}
void change(int * pst)
{
*pst = 23;
}

the above appraoch is right .... but my question was different....
let me re-format it to fit ....
# include <stdio.h>
void change(int *);
int main()
{
int i=5;
change(&i);
i=6;
/* next line prints 6 as we all know, how to make it print 6 without
changing main() function is the question */
printf("%d",i); //it should print 23 instead of 6
}

void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}
 
N

Novitas

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.
 
S

sumit1680

Novitas said:
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...
 
K

Keith Thompson

well just for interest sake! i am told this is possible without threads
and the answer is predictable!
but the catch is that it is architecture predictable.

and for all who r concerned with " void " before main... consider " int
" if u like so.agreed to the fact that main returns int, not void.
pls dwell upon the question not its discrepancies.

Some friendly advice.

Don't assume that your readers can see the article to which you're
replying. Provide some context and proper attributions.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Don't use abbreviations like "u" for "you" or "r" for "are". They
just make it harder to read what you write.

Follow the newsgroup for a while (or browse the archives) and
understand the conventions we use around here.
 
S

sumit1680

Keith said:
Some friendly advice.
thanks for the same, its second time you are pointing me the same..
Don't assume that your readers can see the article to which you're
replying. Provide some context and proper attributions.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Don't use abbreviations like "u" for "you" or "r" for "are". They
just make it harder to read what you write.
ok ... i will try my best to avoid the same ....
 
F

Flash Gordon

(e-mail address removed) wrote:

the above appraoch is right .... but my question was different....
let me re-format it to fit ....
# include <stdio.h>
void change(int *);
int main()
{
int i=5;
change(&i);
i=6;
/* next line prints 6 as we all know, how to make it print 6 without
changing main() function is the question */
printf("%d",i); //it should print 23 instead of 6
}

void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}

1) I can think of no possible valid reason to want to do this
2) Standard C provides absolutely NO mechanism to allow this
3) Any mechanism that you find that "works" when you try it might do
something *completely* different if you change anything at all, compiler
switches, compiler version, change a compiletely unrelated line of code
etc. That is completely ignoring the fact that a different compiler or
different architecture may well be completely different.

You can't relyably do it by changing the return address because the
function might be inlined by the compiler and therefor there not *be* a
return address.

You can't do it by changing the executable code because the compiler
might change the code generated so that fails for any reason at all.
 
S

Steffen

Hi

given below is a code snippet...

void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}

void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}

is this possible in C?


void change(int* i)
{
*i = 23;

void* ptr = &i;
ptr -= 4;
*((int*)ptr) += 10;
}

works on x86, but it's a dirty, non-portable hack rather than C ...

Steffen
 
M

Marc Boyer

Le 02-09-2005 said:
void change(int* i)
{
*i = 23;

void* ptr = &i;
ptr -= 4;
*((int*)ptr) += 10;
}

works on x86, but it's a dirty, non-portable hack rather than C ...

What are you trying to do there ? Could you give me
some explanation ? Something like:
void change(int* i){
*i= 23;
char* ptr= (char*) &i;
ptr-= sizeof(int);
int* int_ptr= (int*) ptr;
*ptr+= 10;
}

By the way, on my linux/x86/gcc, the output is 134513642, not
23...

Marc Boyer
 
J

Jirka Klaue

void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}
void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}

Easy.

void change(int *i)
{
printf("23\n");
fclose(stdout);
}

or

void change(int *i)
{
printf("23\n");
exit(0);
}

Jirka
 
S

Steffen

Hi,

Marc said:
What are you trying to do there ? Could you give me
some explanation ?

On x86 parameters are passed on the stack, and the return address is
placed on the stack right below the last parameter (no guarantee, but at
least I have never seen anything else). So I access it
by taking the address of the parameter und go back 4 bytes (assuming
32bit). Then i increase the return address so that it skips the i=6;
a debugger showed me that that should be 10 bytes. That may be different
for a different compiler/version/settings/whatever ... no idea. Of
course the compiler must not optimize.
By the way, on my linux/x86/gcc, the output is 134513642, not
23...

I tried it on three linux/x86/gcc machines ("gcc -o hack hack.c"),
worked three times. But there are so many things that it depends upon ...

Steffen
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top