Reputation: 105
I having trouble debugging the folowing code I want to get all items in the hisory from processHIstory() functioon but i am getting undesired result
here is what i am tring to do assume user enters the folowong ...
>> ls
>> pwd
>> clear
then history array will be
histrory= {"ls","pwd","clear"}
and when user wants history like this
>>!!
I need all histories to displaylike
ls
pwd
clear
but I am getiing the folowing now
!!
!!
!!
what is my problem?please help me.
Here is the git link to my code! GIST
thank you.
Upvotes: 0
Views: 106
Reputation: 5241
I suspect when you do history[commandsExcuted]=argv[0];
you then go and change the value of argv[0]
which is a pointer.
What you need to do is allocate memory for the string and copy it into history, perhaps something like:
history[commandsExecuted] = malloc(strlen(argv[0]));
strcpy(history[commandsExecuted], argv[0]);
Sorry if this doesn't compile, its been a while since I have worked with C/malloc/str* functions. So much nicer in C++.
Edit: Instead of manually calling malloc
and strcpy
there is also strdup
, from the man page:
The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).
Upvotes: 2