|
|
|
|
![]() ![]() |
Nov 17 2004, 07:07 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
Hello,
Look at the code given below CODE void fun(void) { /* Put your code here so that main does not print 20 */ } int main() { int i = 20; fun(); printf("i = %d\n", i); return 0; } 1. You are allowed to put your code ONLY in fun. 2. You are not allowed to change even one character in main. 3. You should not use functions like exit(),abort() in the function fun. How do I manipulate and change the value of i? Have fun... |
|
|
|
Nov 17 2004, 12:55 PM
Post
#2
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 773 Joined: 4-November 04 Member No.: 2,118 |
This smells like a homework assignment... So.. I won't tell you how to do it
Here's a POINTER though... variables can be manipulated using their address in memory... |
|
|
|
Nov 18 2004, 04:51 AM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
actually, if u use pointers, u cannot be sure that it will work on all systems.
coz memory allocation is compiler and architecture specific.... btw, this is not an assignment and also I got the answer without using pointers.... just give it a shot.... |
|
|
|
Nov 18 2004, 04:15 PM
Post
#4
|
|
|
Newbie [Level 3] ![]() ![]() ![]() Group: Members Posts: 48 Joined: 26-October 04 From: Kalisz, Poland Member No.: 1,971 |
I haven't tested this code:
CODE void fun() { int aaaaaa; int *q= &aaaaaa; while ( *(q--)!=20 ); // we look for variable which value is 20 (it couldn't be 'i'!) *q = 10; // we change it } main() { ... } Hm.... It is only suggestion. |
|
|
|
Nov 19 2004, 07:35 AM
Post
#5
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
good try tmonty...
the solution wud work in a few cases, but just as i told earlier.. it cud fail reason: some other memory location might contain "20".. chek out my next reply for a hint.... |
|
|
|
Nov 19 2004, 10:02 PM
Post
#6
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 13 Joined: 16-November 04 Member No.: 2,309 |
CODE #include <stdio.h>
void fun(void) { #define fun() i=10 } int main() { int i = 20; fun(); printf("i = %d\n", i); return 0; } |
|
|
|
Nov 22 2004, 08:34 AM
Post
#7
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
yeah, that cud be a solution.... i have not tried out...
My solution is also on similar lines..... here it is: CODE fun()
{ #define printf( i , j ) printf( i , j+1 ) } main () { int i =10; fun(); printf("i = %d",i); } |
|
|
|
Nov 26 2004, 02:38 AM
Post
#8
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 178 Joined: 13-October 04 From: NSW, Australia Member No.: 1,713 |
very smooth.
|
|
|
|
Nov 26 2004, 04:47 PM
Post
#9
|
|
|
Newbie [Level 3] ![]() ![]() ![]() Group: Members Posts: 48 Joined: 26-October 04 From: Kalisz, Poland Member No.: 1,971 |
Yeah. ;p
With #define U can do everything. ;p |
|
|
|
Feb 1 2005, 02:02 AM
Post
#10
|
|
|
Newbie [Level 2] |