By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

How do I convert ints to chars, and print out an array in reverse?

P: 5
Suppose I have an char array m, and I want to add an int value z into it, how do I convert z into a char so that I can put it into the array? Also, how could I print out the array in reverse? This is in C, NOT C++.
Sep 8 '10 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 100+
P: 931
Use sprintf function to insert number into array.

To print reverse array start from n(array length) instead of 0 in print loop.(In this case n should be n+1 after adding number in array)
Sep 8 '10 #2

Expert 100+
P: 2,398
Please provide a short example so I understand what you want to do: initial contents of m[], value of z, final contents of m[]. In particular, please make it clear whether m[] contains character values (like '3') or small integer values (like 10). If it contains character values, then is it simply an array or is it a string? In a string, there are typically fewer meaningful characters than there are slots in the array; the end of the meaningful characters is indicated by a null character ('\0').
Sep 8 '10 #3

P: 5
The initial contents of m[] are blank but there are 100 slots, all blank for chars to be inserted in. I am trying to convert a number n to a base b (2-16), so z is a temporary number used to insert a number, or character into the array m[]. m could contain digits 0-9, and letters a-f lowercase.
Sep 8 '10 #4

P: 5
I am new to C so I don't know what the sprintf function is, could you please explain it to me?
Sep 8 '10 #5

P: 542
In C, a string is an array of characters which ends with a NUL character '\0' so the string s[16]represents the array below.(15 characters + NUL)
So if char* s = "0123456789abcde" that is s has a type pointer to a character then:
Expand|Select|Wrap|Line Numbers
  1. cout<<s<<endl; //prints 0123456789abcde
There is an article on this forum which explains arrays in more detail.
You could use a for loop to print out the array in reverse by starting at the [end-1]and printing each element until you reach s[0]
Sep 9 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.