Basic question about pointers

Discussion in 'C Programming' started by Aarti, Jul 16, 2007.

  1. Aarti

    Aarti Guest

    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? Do we not need to allocate memory for str first? Can someone
    please shed some light on this?

    Thanks in advance
     
    Aarti, Jul 16, 2007
    #1
    1. Advertising

  2. On Jul 15, 10:14 pm, 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? Do we not need to allocate memory for str first? Can someone
    > please shed some light on this?
    >
    > Thanks in advance


    http://c-faq.com/decl/strlitinit.html
     
    Chris Johnson, Jul 16, 2007
    #2
    1. Advertising

  3. On Sun, 15 Jul 2007 20:14:33 -0700, 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"


    Whether you get a segmentation fault or not, the effort to evaluate
    any variable with an indeterminate value invokes undefined behavior.

    >work? Do we not need to allocate memory for str first? Can someone
    >please shed some light on this?


    "test" is a string literal. When your compiler constructs your
    program, that literal is part of the object file. The pointer str is
    initialized with the address of the first 't' in that literal.

    While not technically precise, you can think of it conceptually as the
    compiler allocating five bytes for the literal; initializing those
    five bytes with the values 't', 'e', 's', 't', and '\0'; and
    initializing str to point to the start of this five byte area.

    To answer your question - when initializing a pointer with a string
    literal, you do not need to allocate memory for the pointer because
    the compiler did it for you.


    Remove del for email
     
    Barry Schwarz, Jul 16, 2007
    #3
  4. Aarti

    santosh Guest

    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.


    Not necessarily. Segmentation faults do occur under modern hardware
    and operating systems, but not on all systems. Under DOS, for example,
    a segmentation fault will not occur.

    But in all cases, attempting to deference an indeterminate pointer
    causes Undefined behaviour, as per the C Standard. Also the outcome of
    storing an arbitrary numeric value into a pointer, (as you've done
    above), is implementation defined. It may be perfectly sensible in one
    situation, but fatal in another. It is however, not portable.

    > But why does const char* str = "test"
    > work? Do we not need to allocate memory for str first? Can someone
    > please shed some light on this?



    That's because when the implementation compiled your program, it
    automatically and anonymously allocated storage space for the string
    and initialised str to point to the first element of it. This is a
    part of the "high level" nature of languages like C. In assembler, for
    example, you'd have to manually set aside storage for "test" and
    explicitly initialise the pointer, (or register), with the start
    address of this piece of storage.
     
    santosh, Jul 16, 2007
    #4
  5. Aarti

    Mohan Guest

    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?


    "test" is a valid string literal, that means str will be pointing to a
    valid pointer.
    Where as 'i' is uninitialized and dereferencing 'i' causes undefined
    behavior.

    > Do we not need to allocate memory for str first? Can someone
    > please shed some light on this?

    Yes, 'str' in this case is pointing to a valid (allocated) memory.

    Mohan
     
    Mohan, Jul 16, 2007
    #5
  6. Mohan <> wrote:

    > Aarti 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.
    > > }


    > "test" is a valid string literal, that means str will be pointing to a
    > valid pointer.


    str points to the first character of an array of characters. It does
    not point to a pointer.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jul 16, 2007
    #6
  7. Aarti

    pete Guest

    Christopher Benson-Manica wrote:
    >
    > Mohan <> wrote:
    >
    > > Aarti 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.
    > > > }

    >
    > > "test" is a valid string literal,
    > > that means str will be pointing to a
    > > valid pointer.

    >
    > str points to the first character of an array of characters. It does
    > not point to a pointer.


    You can also say that str is a pointer to a string.

    --
    pete
     
    pete, Jul 16, 2007
    #7
  8. On Mon, 16 Jul 2007 00:56:11 -0700, santosh <>
    wrote:

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

    >
    >Not necessarily. Segmentation faults do occur under modern hardware
    >and operating systems, but not on all systems. Under DOS, for example,
    >a segmentation fault will not occur.
    >
    >But in all cases, attempting to deference an indeterminate pointer
    >causes Undefined behaviour, as per the C Standard. Also the outcome of
    >storing an arbitrary numeric value into a pointer, (as you've done
    >above), is implementation defined. It may be perfectly sensible in one
    >situation, but fatal in another. It is however, not portable.


    He was not attempting to store a numeric value into the pointer, but
    into the object the pointer should have been pointing to.

    >
    >> But why does const char* str = "test"
    >> work? Do we not need to allocate memory for str first? Can someone
    >> please shed some light on this?

    >
    >
    >That's because when the implementation compiled your program, it
    >automatically and anonymously allocated storage space for the string
    >and initialised str to point to the first element of it. This is a
    >part of the "high level" nature of languages like C. In assembler, for
    >example, you'd have to manually set aside storage for "test" and
    >explicitly initialise the pointer, (or register), with the start
    >address of this piece of storage.



    Remove del for email
     
    Barry Schwarz, Jul 17, 2007
    #8
  9. On Mon, 16 Jul 2007 10:42:13 -0000, Mohan <> wrote:

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

    >
    >"test" is a valid string literal, that means str will be pointing to a
    >valid pointer.


    str will be pointing to a valid char.

    >Where as 'i' is uninitialized and dereferencing 'i' causes undefined
    >behavior.
    >
    >> Do we not need to allocate memory for str first? Can someone
    >> please shed some light on this?

    >Yes, 'str' in this case is pointing to a valid (allocated) memory.
    >
    >Mohan



    Remove del for email
     
    Barry Schwarz, Jul 17, 2007
    #9
  10. Aarti

    Mohan Guest

    <snip>
    > > "test" is a valid string literal, that means str will be pointing to a
    > > valid pointer.

    >
    > str points to the first character of an array of characters. It does
    > not point to a pointer.
    >


    Yes, you are correct. Thanks for correcting me.

    Mohan
     
    Mohan, Jul 17, 2007
    #10
  11. Aarti

    Urs Beeli Guest

    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>
     
    Urs Beeli, Jul 17, 2007
    #11
  12. Aarti

    Chris Dollin Guest

    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
     
    Chris Dollin, Jul 17, 2007
    #12
  13. Aarti

    Urs Beeli Guest

    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>
     
    Urs Beeli, Jul 17, 2007
    #13
  14. Aarti

    Bryan Harris Guest

    On Jul 15, 10:14 pm, Aarti <> 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: }
     
    Bryan Harris, Jul 17, 2007
    #14
    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. Simon

    Basic pointers question

    Simon, Nov 17, 2006, in forum: C Programming
    Replies:
    6
    Views:
    337
  2. Jeff Bender
    Replies:
    6
    Views:
    355
    Jeff Bender
    Jun 8, 2007
  3. Replies:
    6
    Views:
    435
    James Kanze
    Mar 4, 2009
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    739
  5. bpascal123

    c pointers notation basic question

    bpascal123, Mar 16, 2013, in forum: C Programming
    Replies:
    37
    Views:
    593
    88888 Dihedral
    Mar 20, 2013
Loading...

Share This Page