Quote:
Originally Posted by axes
Sajin,
I think using single pointer will not cater my requirement, we need to use double pointer,
because str parameter in prep_string function is different from
str pointer variable in main function
CORRECT me if i am wrong
I think the following serves ur purpose if any thing again is wrong with that pls correct me , actually i just typed that code in a hurry that's the reason..
char * prep_string();
int main()
{
char *str; // The dynamicallly allocated string u could use it like any other string
// Like str[30] ,str[i] is valid since it is charachter string
if( (str =prep_string() ) == NULL)
//code to handle not enough memory condition
else
// Now use the str as any other charachter string
return 0;
}
char * prep_string()
{
int size ;
printf("\nEnter the size of the string ");
scanf("%d",&size);
str =(char *) malloc(size * sizeof(char));
if(str == NULL)
// Not enough memory to satisfy the request
//Place the code to handle the situation
return NULL;
else
return str; // Now both pointers point to the same location
}