is there a way to do this...

Discussion in 'C Programming' started by sumit1680@rediffmail.com, Sep 2, 2005.

  1. Guest

    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?
     
    , Sep 2, 2005
    #1
    1. Advertising

  2. wrote:
    > given below is a code snippet...
    >
    > void main()


    Dead already.
     
    Martin Ambuhl, Sep 2, 2005
    #2
    1. Advertising

  3. wrote:

    > 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.
    --
    Anton Petrusevich
     
    Anton Petrusevich, Sep 2, 2005
    #3
  4. Guest

    Re: is there a way to do this...

    but you can't say what will it print, sometimes 6 or some time 23
     
    , Sep 2, 2005
    #4
  5. 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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29 July 1999
    http://www.cpax.org.uk
    Email rjh at the above domain
     
    Richard Heathfield, Sep 2, 2005
    #5
  6. Re: is there a way to do this...

    wrote:
    > 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
     
    Krishanu Debnath, Sep 2, 2005
    #6
  7. writes:
    > 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?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 2, 2005
    #7
  8. Re: is there a way to do this...

    writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 2, 2005
    #8
  9. Guest

    Re: is there a way to do this...

    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.
     
    , Sep 2, 2005
    #9
  10. Duke Guest

    Re: is there a way to do this...

    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;
    }
     
    Duke, Sep 2, 2005
    #10
  11. Guest

    Re: is there a way to do this...

    Duke wrote:
    > 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
    }
     
    , Sep 2, 2005
    #11
  12. Novitas Guest

    Re: is there a way to do this...

    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.
     
    Novitas, Sep 2, 2005
    #12
  13. Guest

    Re: is there a way to do this...

    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...
     
    , Sep 2, 2005
    #13
  14. Re: is there a way to do this...

    writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 2, 2005
    #14
  15. Guest

    Re: is there a way to do this...

    Keith Thompson wrote:
    > writes:
    > > 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.

    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 ....
    > Follow the newsgroup for a while (or browse the archives) and
    > understand the conventions we use around here.
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.
     
    , Sep 2, 2005
    #15
  16. Flash Gordon Guest

    Re: is there a way to do this...

    wrote:

    <snip>

    > 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.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Sep 2, 2005
    #16
  17. Steffen Guest

    Hi

    wrote:
    > 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
     
    Steffen, Sep 2, 2005
    #17
  18. Marc Boyer Guest

    Le 02-09-2005, Steffen <> a écrit :
    > wrote:
    >> 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 ...


    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
     
    Marc Boyer, Sep 2, 2005
    #18
  19. Jirka Klaue Guest

    On Fri, 02 Sep 2005 10:01:58 +0200, <> wrote:

    > 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
     
    Jirka Klaue, Sep 2, 2005
    #19
  20. Steffen Guest

    Hi,

    Marc Boyer wrote:
    >>>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
    >>>}

    >>
    >>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 ?


    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
     
    Steffen, Sep 2, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    382
    Branimir Maksimovic
    Oct 20, 2005
  2. Paul Rubin
    Replies:
    5
    Views:
    421
    Hendrik van Rooyen
    Aug 6, 2009
  3. Bill Nicholson
    Replies:
    1
    Views:
    141
    Phil H
    Sep 26, 2007
  4. Andries

    is there a way ..... any way

    Andries, Apr 26, 2004, in forum: Perl Misc
    Replies:
    27
    Views:
    254
    Robin
    Apr 27, 2004
  5. francisco lopez
    Replies:
    2
    Views:
    168
    Dr John Stockton
    Dec 31, 2004
Loading...

Share This Page