pointer question (pretty basic)...

Discussion in 'C Programming' started by Chris Mantoulidis, Jan 20, 2004.

  1. I never liked pointers really much, so I decided to stay away from
    them for a while. I know they're useful, so now I decided to actually
    learn how the work, use them, etc.

    Here's my question...

    char *a = "some text";
    char *b = "some other text";

    Why will this work? Needn't I initialize the pointers first? (malloc
    in C, new in C++, whatever)

    And second question. Let's say I got a function that returns char *
    ("odd" if the parameter is odd, and "even" if the parameter is even)

    char *s1 = CHAR_PTR_FUNCTION(1);
    char *s2 = CHAR_PTR_FUNCTION(2);

    The return value (returned like 'return "odd"' or 'return "even"'),
    where is it stored? It has to be stored somewhere cuz otherwise *s1
    would have the same value as *s2...

    I'm getting confused here...

    Any help?
     
    Chris Mantoulidis, Jan 20, 2004
    #1
    1. Advertising

  2. (Chris Mantoulidis) wrote in
    news::

    > I never liked pointers really much, so I decided to stay away from
    > them for a while. I know they're useful, so now I decided to actually
    > learn how the work, use them, etc.


    They are extremely powerful and are integral to the C language, I suggest
    you become proficient in using them.

    > Here's my question...
    >
    > char *a = "some text";
    > char *b = "some other text";
    >
    > Why will this work? Needn't I initialize the pointers first?


    You just did. You are allocating two pointers and them initializing them
    to point to two strings stored somewhere else. Just don't try to write to
    these pointers. In this case, you'd have been better to use const char
    *pThing instead of char *pThing.
    >
    > And second question. Let's say I got a function that returns char *
    > ("odd" if the parameter is odd, and "even" if the parameter is even)
    >
    > char *s1 = CHAR_PTR_FUNCTION(1);
    > char *s2 = CHAR_PTR_FUNCTION(2);
    >
    > The return value (returned like 'return "odd"' or 'return "even"'),
    > where is it stored? It has to be stored somewhere cuz otherwise *s1
    > would have the same value as *s2...


    The strings "odd" and "even" are stored somewhere that you don't need to
    care about. Returning a point to a string of this type is save and you can
    safely set your s1 and s2 pointer to point to them via the function
    return. Pointer s1 and s2 may point to the same string in memory if both
    numbers are even or odd, so what? As long as you don't try to write to the
    strings pointed to by s1 or s2 you're fine.

    --
    - Mark ->
    --
     
    Mark A. Odell, Jan 20, 2004
    #2
    1. Advertising

  3. Chris Mantoulidis

    Dan Pop Guest

    In <> (Chris Mantoulidis) writes:

    >I never liked pointers really much, so I decided to stay away from
    >them for a while.


    Without pointers, C is practically useless. Even the canonical "hello
    world" program is using a pointer.

    >Here's my question...
    >
    >char *a = "some text";
    >char *b = "some other text";
    >
    >Why will this work? Needn't I initialize the pointers first? (malloc
    >in C, new in C++, whatever)
    >
    >And second question. Let's say I got a function that returns char *
    >("odd" if the parameter is odd, and "even" if the parameter is even)
    >
    >char *s1 = CHAR_PTR_FUNCTION(1);
    >char *s2 = CHAR_PTR_FUNCTION(2);
    >
    >The return value (returned like 'return "odd"' or 'return "even"'),
    >where is it stored? It has to be stored somewhere cuz otherwise *s1
    >would have the same value as *s2...
    >
    >I'm getting confused here...


    Open your C book and read about string literals. You'll find the answer
    to both your questions.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 20, 2004
    #3
  4. "Mark A. Odell" <> wrote in message news:<Xns947659CF31964CopyrightMarkOdell@130.133.1.4>...
    > (Chris Mantoulidis) wrote in
    > news::
    >
    > > I never liked pointers really much, so I decided to stay away from
    > > them for a while. I know they're useful, so now I decided to actually
    > > learn how the work, use them, etc.

    >
    > They are extremely powerful and are integral to the C language, I suggest
    > you become proficient in using them.
    >
    > > Here's my question...
    > >
    > > char *a = "some text";
    > > char *b = "some other text";
    > >
    > > Why will this work? Needn't I initialize the pointers first?

    >
    > You just did. You are allocating two pointers and them initializing them
    > to point to two strings stored somewhere else. Just don't try to write to
    > these pointers. In this case, you'd have been better to use const char
    > *pThing instead of char *pThing.
    > >
    > > And second question. Let's say I got a function that returns char *
    > > ("odd" if the parameter is odd, and "even" if the parameter is even)
    > >
    > > char *s1 = CHAR_PTR_FUNCTION(1);
    > > char *s2 = CHAR_PTR_FUNCTION(2);
    > >
    > > The return value (returned like 'return "odd"' or 'return "even"'),
    > > where is it stored? It has to be stored somewhere cuz otherwise *s1
    > > would have the same value as *s2...

    >
    > The strings "odd" and "even" are stored somewhere that you don't need to
    > care about. Returning a point to a string of this type is save and you can
    > safely set your s1 and s2 pointer to point to them via the function
    > return. Pointer s1 and s2 may point to the same string in memory if both
    > numbers are even or odd, so what? As long as you don't try to write to the
    > strings pointed to by s1 or s2 you're fine.


    Thanks, it's clear at last =P
     
    Chris Mantoulidis, Jan 20, 2004
    #4
    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. vamp4l
    Replies:
    8
    Views:
    421
    EdwardH
    Sep 19, 2005
  2. Replies:
    5
    Views:
    276
    Bruno Desthuilliers
    Oct 24, 2006
  3. Replies:
    10
    Views:
    745
    Chris Torek
    Feb 4, 2005
  4. Replies:
    0
    Views:
    219
  5. grocery_stocker

    Queue() question. This post is pretty long.

    grocery_stocker, Apr 16, 2009, in forum: Python
    Replies:
    2
    Views:
    267
    grocery_stocker
    Apr 16, 2009
Loading...

Share This Page