Mars said:
Why this error occurs??
typedef char *string;
int main(void)
{
string str;
Very important:
** You have declared a pointer
** but you have not allocated
** any room for the characters.
Misnomer:
** This line assigns pointers
** BUT NOT CONTENT. DO NOT
** GET IN THE HABIT OF USING
** ASSIGNMENT FOR CHAR ARRAYS.
** That is what the str*()
** methods are for.
printf("%s\n",str);
free(str);
printf("%s\n",str);
}
Don't use pointers after you have
freed the memory they point to.
That is bad karma. The memory
could be given to another program
running concurrently with yours.
Only free memory when you are finished
with it.
See also: Reference Counting.
BTW, do not label "char *" as a
string. A string is a data structure
{which may have content}. The
"char *" is a pointer; no memory
allocated, just a pointer.