Re: pointer question (pretty basic)...
From: Mark A. Odell (nospam_at_embeddedfw.com)
Date: 01/20/04
- Next message: James Kuyper: "Re: Extent of the "as-if" rule"
- Previous message: Mark A. Odell: "Re: Static Variables"
- In reply to: Chris Mantoulidis: "pointer question (pretty basic)..."
- Next in thread: Chris Mantoulidis: "Re: pointer question (pretty basic)..."
- Reply: Chris Mantoulidis: "Re: pointer question (pretty basic)..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 Jan 2004 13:49:43 GMT
cmad_x@yahoo.com (Chris Mantoulidis) wrote in
news:a8587dd9.0401200518.57b84bd4@posting.google.com:
> 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 -> --
- Next message: James Kuyper: "Re: Extent of the "as-if" rule"
- Previous message: Mark A. Odell: "Re: Static Variables"
- In reply to: Chris Mantoulidis: "pointer question (pretty basic)..."
- Next in thread: Chris Mantoulidis: "Re: pointer question (pretty basic)..."
- Reply: Chris Mantoulidis: "Re: pointer question (pretty basic)..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|