+ 2

Can anyone show how to pass char array to function?

I prepared a program for convertation numbers from base (binary thru hex) to base (binary thru hex), and wanted to improve it. I decided move some repeating parts to the functions but unfortunatly can't put correctly char array to the function. I use char str[] for storing original input number, and later analyse this number. When i was trying rewrite rows 74-77 or 80-83 into function, or 86-107 into function any passing of str is not possible or cause some mistakes with program. Is it exist some possible way? https://code.sololearn.com/c9WUGBq2Y8i8/#cpp

25th Feb 2018, 8:09 AM
Dmytro Kovryzhenko
Dmytro  Kovryzhenko - avatar
9 Antworten
+ 3
@Timon Paßlick "What stops you from using the string class?" That's another good solution, I will try it too.
26th Feb 2018, 9:31 AM
Dmytro Kovryzhenko
Dmytro  Kovryzhenko - avatar
+ 3
Posted new code with using class for storing char array, wrote some functions and erase unnecessary parts. https://code.sololearn.com/cib16zKt3M26/#cpp Enjoy! P.S.: Report me about bugs.
3rd Mar 2018, 6:27 AM
Dmytro Kovryzhenko
Dmytro  Kovryzhenko - avatar
+ 2
@~ swim ~ Not so true, I pass int array to the function get_new_num and wanted to create some another functions with passing char array, but always stoke in some mistake, now I get what was wrong. There is necceseary to do following: int func(char* str, ...) { ..."Some code with str[x], where x is int"... } Also, thank for your report about mistakes with program! I fixed it!. Now it working properly. It appear that program takes str[length-1] character as base output when input string word is too long. I declare initial size of input to 30, so you can pass 26-digit number, dot, input base, dot, output base. Interesting why this happend, but it work now. Tell more about mistakes. Remember program convert number to the long long int in the middle of the process so your passing input word is limitid by size of this type. I want to improve it in the future.
26th Feb 2018, 8:22 AM
Dmytro Kovryzhenko
Dmytro  Kovryzhenko - avatar
+ 2
@~ swim ~ I will definitely use it to improve code.
26th Feb 2018, 9:17 AM
Dmytro Kovryzhenko
Dmytro  Kovryzhenko - avatar
+ 2
What stops you from using the string class?
26th Feb 2018, 9:28 AM
Timon Paßlick
+ 1
Cool.
26th Feb 2018, 9:38 AM
Timon Paßlick
+ 1
To take char[] when you need to, use this: template <size_t N> void example(char (&argument)[N]); Now argument is a reference to char[N]. This works not for char*, though. If you can't use the array class, you can do this for a c style array of type T: template <typename T, size_t N> void example(T (&argument)[N]); Only when you write code like in these 2 examples, you have a guarantee that N equals the array size. There is no way to take a c style array by value instead of by reference, you must use a constant reference and type the copy instruction by hand.
26th Feb 2018, 10:09 AM
Timon Paßlick