Basic question about pointers - C

This is a discussion on Basic question about pointers - C ; On Mon, 16 Jul 2007 10:42:13 -0000 Mohan wrote: [Quoting Mohan as my server no longer carries the OP] > Aarti wrote: > > I have a very elementary question about pointers. Please pardon me > > for my ignorance ...

+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 14 of 14

Basic question about pointers

  1. Default Re: Basic question about pointers

    On Mon, 16 Jul 2007 10:42:13 -0000 Mohan wrote:

    [Quoting Mohan as my server no longer carries the OP]

    > Aarti wrote:
    > > I have a very elementary question about pointers. Please pardon me
    > > for my ignorance of C
    > >
    > > int main()
    > > {
    > > int* i;
    > > *i = 1 //at times this may give me a core dump.
    > > const char* str = "test"; //This seems to be a valid construct.
    > > }
    > >
    > > Now I understand that when we declare int* i, we just have a pointer
    > > that may point to any place. If we try to store something in it, we
    > > may get a segmentation fault. But why does const char* str = "test"
    > > work?


    I think you are confusing things due to different notations.

    You have:
    int* i;
    *i = 1;

    This is not correct, as i is not properly initialised so dereferencing it on
    the second line causes undefined behaviour.

    What you have above, is *NOT* the same as:
    int* i = 1;
    Here you create a pointer to an int and make it point to the address "1",
    not defining the content of *i.

    Had you written:
    char* str;
    *str = "test";
    you would have had the same problem (plus an additional one), you would
    have dereferenced the uninitialized pointer to char str. (And even had it
    been initialised, it would be a syntax error to assign as string literal to
    it).

    Writing:
    char* str;
    str = "test";
    would be correct and do the same thing as your example, but then
    int* i;
    i = 1;
    would also be valid (and do the same as int *i=1 but not do what you
    tried.

    The fact that the legal construct 'int *i=1;' contains the sequence '*i=1;'
    does not mean that i is dereferenced, the * belongs to the declaration of i.

    I imagine that this might have confused you.

    Cheers
    /urs

    --
    "Change is inevitable, except from a vending machine."
    -- Urs Beeli, <usenet@CONCAT_MY_FIRST_AND_LAST_NAME.ch>

  2. Default Re: Basic question about pointers

    Urs Beeli wrote:
    > I think you are confusing things due to different notations.
    >
    > You have:
    > int* i;
    > *i = 1;
    >
    > This is not correct, as i is not properly initialised so dereferencing it on
    > the second line causes undefined behaviour.
    >
    > What you have above, is *NOT* the same as:
    > int* i = 1;
    > Here you create a pointer to an int and make it point to the address "1",
    > not defining the content of *i.


    Here you have a constraint violation. `1` is not a legal value for a
    pointer. (If you cast the integer to a pointer, only the implementation
    knows what will happen. I think it has to tell you, though.)

    --
    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


  3. Default Re: Basic question about pointers

    On Tue, 17 Jul 2007 13:35:38 +0100 Chris Dollin wrote:
    > Urs Beeli wrote:
    > > I think you are confusing things due to different notations.
    > >
    > > You have:
    > > int* i;
    > > *i = 1;
    > >
    > > This is not correct, as i is not properly initialised so dereferencing it on
    > > the second line causes undefined behaviour.
    > >
    > > What you have above, is *NOT* the same as:
    > > int* i = 1;
    > > Here you create a pointer to an int and make it point to the address "1",
    > > not defining the content of *i.

    >
    > Here you have a constraint violation. `1` is not a legal value for a
    > pointer. (If you cast the integer to a pointer, only the implementation
    > knows what will happen. I think it has to tell you, though.)


    Fair enough, I missed that in my zeal to unconfuse the OP

    Cheers
    /urs

    --
    "Change is inevitable, except from a vending machine."
    -- Urs Beeli, <usenet@CONCAT_MY_FIRST_AND_LAST_NAME.ch>

  4. Default Re: Basic question about pointers

    On Jul 15, 10:14 pm, Aarti <aarti.sa...> wrote:
    > int main()
    > {
    > int* i;
    > *i = 1 //at times this may give me a core dump.
    > const char* str = "test"; //This seems to be a valid construct.
    >
    > }


    Shouldn't there be a semicolon on line 4?

    1: int main()
    2: {
    3: int* i;
    4: *i = 1; //at times this may give me a core dump.
    5: const char* str = "test"; //This seems to be a valid construct.
    6:
    7: }


+ Reply to Thread
Page 2 of 2 FirstFirst 1 2

Similar Threads

  1. Question about pointers
    By Application Development in forum c++
    Replies: 1
    Last Post: 11-07-2007, 05:39 AM
  2. pointers question
    By Application Development in forum C
    Replies: 2
    Last Post: 10-22-2007, 01:18 PM
  3. Basic question on pointers and scope
    By Application Development in forum c++
    Replies: 1
    Last Post: 10-22-2007, 10:17 AM
  4. pointers and assignment operators, basic question
    By Application Development in forum c++
    Replies: 6
    Last Post: 06-08-2007, 05:26 PM
  5. Visual Basic 6.0 function pointers
    By Application Development in forum basic.visual
    Replies: 2
    Last Post: 01-31-2006, 10:57 AM