0

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.

4
  • Have you tried replacing *history++ with history[i]? Commented Feb 7, 2015 at 4:33
  • @zenith yes, but i get null! Commented Feb 7, 2015 at 4:38
  • Where do you get null? Commented Feb 7, 2015 at 4:42
  • @zenith from processhistory() function the result are (null)(null)(null) when displayed! Commented Feb 7, 2015 at 4:46

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I am getting null form out put ... when processhistory() is called
@zenith do you try it with my code, it's not working for me, which line do you modify.
I copypasted your code, replaced history[commandsExcuted]=argv[0]; in processCommand with the two lines ilent2 proposed, and the history is now showing correctly to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.