char *str - Pointer usage 
Author Message
 char *str - Pointer usage

I have seen the following function.

=====================================
F()
{

char *str;

str = GetStringFromSomeFunction();

Quote:
}

=====================================

Is this valid use of *str? My understanding was that memory has to be
allocated for pointers before using them. In the above example, only a
pointer is declared but no memory has been allocated. Wouldn't this
cause memory voilations?

Any explanation or pointers to text explaning this is greatly
appreciated.

TIA.

SubbaRao



Thu, 26 Aug 1999 03:00:00 GMT  
 char *str - Pointer usage


Quote:
> I have seen the following function.
> =====================================
> F()
> {
> char *str;
> str = GetStringFromSomeFunction();
> }
> =====================================
> Is this valid use of *str? My understanding was that memory has to be
> allocated for pointers before using them. In the above example, only a
> pointer is declared but no memory has been allocated. Wouldn't this
> cause memory voilations?

No, although it depends on what the function actually returns. *if*
the function returns a valid pointer (a pointer to an object wich
is valid and is of type char) then there's nothing wrong with it.
If on the other hand the function would return an invalid pointer
(say to auto storage) the pointer is invalid. I'll try to explain
it in simple terms ; First we say let's declare a pointer, and call
it 'str' this pointer is pointing nowhere so we need to point it at
something. We wrote a function wich returns a pointer so a string
this string is allocated *inside* the function, so that when we
return the pointer from this function it will remain valid. Let's
put this in practice ;

char *my_function(void)
{
  char *p = malloc(100);  /* point a pointer (p) to allocated memory */

  if(!p)
     ; /* Not enuff core - bail out */

  sprintf(p, "Hello there !"); /* print something into our buffer */

  return p;  /* return the *pointer* to our buffer wich holds the string */

Quote:
}

char *my_function_too(void)
{
  return "blah blah";

Quote:
}

int foo(void)
{
  char *some_pointer; /* a pointer, pointing nowhere */

  /* What we do now is point our pointer to a string (a valid object
     of type char *) and we will get a valid pointer from this, simply
     because the function will do all the work for us */

  some_pointer = my_function();

  puts(some_pointer);  

  free(some_pointer);  /* Since we (m)alloced it - free it */

  /* Now we point some_pointer at a string wich is located somewhere
     in (''static'') memory and we will again recieve a valid pointer */

  some_pointer = my_function_too();

  puts(some_pointer);

  /* no (m)alloc() - no free */

  return 0;

Quote:
}

int main(void)
{
  return foo();

Quote:
}
> Any explanation or pointers to text explaning this is greatly
> appreciated.

Hope that cleared it up a bit.

Cheers, flux.



Thu, 26 Aug 1999 03:00:00 GMT  
 char *str - Pointer usage

Quote:
> F()
> {

> char *str;

> str = GetStringFromSomeFunction();
> }
> =====================================

> Is this valid use of *str? My understanding was that memory has to be
> allocated for pointers before using them.

K&R2, p93, Ch5 is one text. When Get..Function returns a value, it will
not return a string as memory bytes (you can't do that in C), it will
return a pointer. This pointer will point to the beginning of a string.

Remember that a pointer is a placeholder or a bookmark to memory. When
you use a pointer, you are just using this bookmark. However, if you
want to use the information pointed by the bookmark, you must
dereference the pointer by using *. Using your example, if 'str' is a
pointer to a string, '*str' is the value of the first character.



Thu, 26 Aug 1999 03:00:00 GMT  
 char *str - Pointer usage

It depends on what you are doing in the GetStringFromSomeFunction=20
function. If you return a pointer to a correct string (static string or=20
a string allocated with calloc) no problems should occur.=20
=20
Robert B. Rossmann=20



Fri, 27 Aug 1999 03:00:00 GMT  
 char *str - Pointer usage

=====================================
F()
{

char *str;

str = GetStringFromSomeFunction();

Quote:
}

This is okay if, for example, GetStringFrom... looked like this:

char * GetStringFromSomeFunction(void)
{
        static string = {"Return string"};

        return(string);

Quote:
}

Thus the pointer returned has to point to an area of memory that
will always be available (thus the static).  You could also
return a pointer to a string constant.

--
Bill Symmes                           PROM Software Inc

                                      +1 802 862-8357 (Fax)    
                                      Burlington VT 05406-4027



Mon, 30 Aug 1999 03:00:00 GMT  
 char *str - Pointer usage


Quote:
> Is this valid use of *str? My understanding was that memory has to be
> allocated for pointers before using them. In the above example, only a
> pointer is declared but no memory has been allocated. Wouldn't this
> cause memory voilations?

    Yes it is a valid usage. Memory has been allocated (probably 4 or
    so bytes) for the pointer simply by defining it. The pointer does
    not point to anything until you call DamnFunctionNameIsTooLong(),
    so the first thing you do with it is make it point to something.
    You do this with an assignment of some sort, such as your example
    function call.

    A pointer value is just a value, and can be set or read pretty
    freely. The data a pointer points to, however, is a separate
    thing that, as you suggest, cannot be used until the pointer has
    been initialised to point to something.



Mon, 30 Aug 1999 03:00:00 GMT  
 char *str - Pointer usage

<snip>

Quote:
> This is okay if, for example, GetStringFrom... looked like this:
<snip>
>    static string = {"Return string"};

The above initialization is wrong: I guess, a `char*' got omitted.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Tue, 31 Aug 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. char *str vs. char s

4. char* and char str[2]

5. c standard regarding char str[ ], str[50] & *str

6. Comparison with pointer to pointer to char, and char

7. A char pointer (char *) vs. char array question

8. Basic Question - char *str V/S int *

9. str != (char *) NULL

10. register char str[LEN]

11. how to address char * str[] ?

12. q about array of char str

 

 
Powered by phpBB® Forum Software