Re: pointer question (pretty basic)...

From: Mark A. Odell (nospam_at_embeddedfw.com)
Date: 01/20/04


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


Relevant Pages

  • Re: Function Problem
    ... > but fnReplace takes pointers to strings of length 16. ... > void fnReplace (char *RplStr) ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)
  • Re: new IL: C (sort of...).
    ... only for "recent" Pascals, ... far pointers weren't really limited, ... in my compiler, I made wchar_t a builtin type (in most cases, aliased to ... I could very well include builtin "managed strings" in the new IL. ...
    (comp.lang.misc)
  • Re: HeapFree() Failing to deallocate string
    ... I've been able to recreate and isolate the problem with HeapFree(), ... in this simplified example is just a pointer to 16 bytes to store pointers ... the szCaption strings need to be copied to the pointed to ... strings for which storage needs to be allocated, ...
    (microsoft.public.windowsce.embedded)
  • Re: Increasing efficiency in C
    ... >> The representation of a string in C is the sequence of characters, ... strings, they are passed the addresses of strings. ... supports pointers the way it does. ... Competent programmers make mistakes, too. ...
    (comp.lang.c)