comp.lang.c FAQ, question 4.8

Discussion in 'C Programming' started by Ian Collins, Jan 30, 2008.

  1. Ian Collins

    Ian Collins Guest

    Roman Mashak wrote:
    > Hello,
    >
    > as for Q4.8 from local FAQ, why can't the solution be like this:
    >
    > int f(int *p)
    > {
    > static int dummy = 10;


    Why not just write *p = 10; ?

    > *p = dummy;
    > }
    >

    I think you have misunderstood the question, which is "I have a function
    which accepts, and is supposed to *initialize, a pointer*". Note
    initialise the pointer, not the value pointed to by the pointer.

    --
    Ian Collins.
    Ian Collins, Jan 30, 2008
    #1
    1. Advertising

  2. Ian Collins

    Roman Mashak Guest

    Hello,

    as for Q4.8 from local FAQ, why can't the solution be like this:

    int f(int *p)
    {
    static int dummy = 10;
    *p = dummy;
    }

    int main()
    {
    int p = 5;
    printf("p before=%d\n", p);
    f(&p);
    printf("p after=%d\n", p);
    return 0;
    }

    It works fine with gcc on linux host. And it also looks fairly correct - we
    pass pointer and alter the value it points to.
    Am I missing something?

    Thanks in advance.

    With best regards, Roman Mashak. E-mail:
    Roman Mashak, Jan 31, 2008
    #2
    1. Advertising

  3. Ian Collins

    Roman Mashak Guest

    Hello, Ian!
    You wrote on Wed, 30 Jan 2008 20:43:15 +1300:

    ??>> *p = dummy;
    ??>> }
    ??>>
    IC> I think you have misunderstood the question, which is "I have a
    IC> function which accepts, and is supposed to *initialize, a pointer*".
    IC> Note initialise the pointer, not the value pointed to by the pointer.
    Indeed, I missed the point.

    With best regards, Roman Mashak. E-mail:
    Roman Mashak, Jan 31, 2008
    #3
    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.

Share This Page