[comp.os.msdos.programmer] Returning a char* in Turbo C:

frotz@drivax.UUCP (Frotz) (07/31/90)

mrice@caen.engin.umich.edu (Michael Rice) writes:

] I wrote the following code to return a char* (a character pointer)
] to a string let's say "Michigan".  I went through the debugger and
] the return value tempv->vname is correct but it returns something
] else something like "\eu\r4" which is gibberish to me.

] I am mainly calling it like this:

]   printf("%s",findvolname(temp));

Try looking at the prototype definition before the printf().  I assume
that this is either: a) in a separate module; or b) declared below the
point where you are using it. 

Most likely the compiler is returning an int and you are trying to use
it as a pointer.  As a note, an external definition may be sufficient
for return-types but is often not sufficient for parameter types. 

Try putting a full prototype for findvolname() in a header file and
including that header file for all modules that use the function. 
--
Frotz

mrice@caen.engin.umich.edu (Michael Rice) (07/31/90)

Thanks to all those who responded by email.  Many possible problems
were pointed out to me.  

1.  It was suggested I was returning (NULL)->vname which would give me
    the gibberish I encountered.  This wasn't actually the problem
    since the data structures I am using are set up in a way that this
    NULL pointer will never occur.  I will probably write in error
    checking for this at a later time.

2.  It was suggested that I was returning a variable that is out of
    its scope in the calling procedure.  This is probably true, but
    this wasn't the problem either.  I guess it is true that it returns
    a pointer to memory that is not allocated anymore, but the data is
    still available there unless I write over it and since the data is
    used immediately it wouldn't cause the problem.  Is it possible
    somehow in the future this memory will be corrupted before I get
    what I want from it?

3.  It was also suggested that I didn't declare the function as (char*)
    in the calling code and it defaulted to (int).  This was the problem.
   

Thanks to all who replied.  Any other comments are appreciated.
I am new to Turbo C and somewhat new to C so I'll probably have more
questions.  Thanks again.

Mike