I try to change string size from a different function from where the string definition is.
I send string's pointer to the function,then use malloc with a beginning value which works fine and then use realloc to change length,the realloc itself seems to work but I cannot insert input into the string afterwards.
Also I have to use this way of getting input because our lecturer asked us to write the program for
unknown size of input.
Here is the code: (I marked the problematic lines)
worker* Add_End(worker* head) /*I use this function to set up a linked list*/
{
char** ptr;
worker* tail=head;
worker* new_item;
new_item=(worker*)malloc(sizeof(worker));
if(new_item==NULL)
{
printf("Memory allocation failed - return previous value");
return head;
}
ptr=&new_item->f_name;
[B]Get_Input(ptr);[/B] //Input function call//
printf("%s",new_item->f_name);
new_item->next=NULL;
if(head==NULL)
return new_item;
while(tail->next!=NULL)
tail=tail->next;
tail->next=new_item;
return head;
}
void Get_Input(char** str)
{
fflush(stdin);
int n=1;
char tmpc;
*str=(char*)malloc(n*sizeof(char));
do
{
tmpc=getchar();
*str[n-1]=tmpc;
++n;
*str=(char*)realloc(*str,(n)*sizeof(char));
}
while(tmpc!='\n');
*str[n]='\n';
}
Thanks in advance!!!
Edited by ZekeDragon, 15 April 2010 - 07:06 PM.
Please use [code] tags (the # button) when posting code.


Sign In
Create Account

Back to top









