In 'comp.lang.c', Andrej Prsa <an*********@guest.arnes.si> wrote:
Sorry, I really didn't explain it well; I meant initialized, not declared.
I want to automate the strdup() function to something as:
int my_strdup (char *in)
{
/* Check if char *in was ever malloc-ed (e.g. by strdup): */
int switch = my_check_function_which_i_am_not_sure_how_to_write (in);
Note that the 'switch' keyword is part of the C-language.
if (switch == 0) in = strdup ("never initialized");
if (switch == 1)
{
free (in);
in = strdup ("newly initialized");
}
}
I see. The idiomatic way is to test the pointer against NULL. You are not
supposed to have uninitialized pointers in a program, because you would have
no way to discriminate between a valid or an invalid value. (and magically,
this is the problem you are facing to)
This is why the pointers have a special value called NULL in C (or NIL or
whatever in other languages) that clearly means
"This pointer is invalid. Don't attempt to dereference it at all or you will
burn in Hell."
There are complicated alternatives with a structure, a flag or whatever, but
the simple way is NULL.
--
-ed-
em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c :
http://www.isty-info.uvsq.fr/~rumeau/fclc/