1

I was trying to come up with the solution for that ... two large numbers, a and b are represented by char[] or char* and the goal is to multiply them into a third pointer, char* c:

void multiply( const char* a, const char* b ){ int len_a = strlen( a ); int len_b = strlen( b ); int* c = new int[ len_a + len_b]; memset( c, 0, sizeof(int) * ( len_a + len_b )); for( int i = len_a - 1; i >= 0; i-- ){ for( int j = len_b - 1; j >= 0; j-- ){ c[ i + j + 1 ] += ( b[ j ] - '0') * ( a[ i ] - '0' ); } } for( int i = len_a + len_b; i >= 0; i-- ){ if( c[ i ] >= 10 ){ c[ i - 1 ] += c[ i ] / 10; c[ i ] %= 10; } } cout << a << " * " << b << " = " << c << endl; delete[] c; } 

I wrote the above function to do this operation for me ... however, when I use the inputs:

int main( void ){ const char* a = "999"; const char* b = "99999"; multiply( a, b ); // I expect the answer to be 1 and 6 // profit = 0.92 return 0; } 

I got:

999 * 99999 = 0x100100080 

Why am I getting the memory address and not the actual number? Thanks!

10
  • int *c, when you output that it will output the pointer. Don't you mean to make char *c? Commented Sep 5, 2012 at 22:38
  • 1
    Oh, lurvely- the old "I like memory leaks, double deletes, and buffer overruns" style of code. Commented Sep 5, 2012 at 22:38
  • @BobFincheimer: Yes, I wanted to do it as a char* c. Could you give me an idea how could I write it as a char? Commented Sep 5, 2012 at 22:39
  • @DeadMG I don't see any of those in his code. Commented Sep 5, 2012 at 22:40
  • 1
    @BobFincheimer, what if someone flips the bits of the exception mask for cout? This type of code has no reason to prefer new[] to vector. Commented Sep 5, 2012 at 22:46

5 Answers 5

3

Because c is an int pointer and the stream operator for cout will print a memory address if passed such a pointer. To get the value you need to dereference the pointers with e.g. *c. You'll probably need to write a loop to print the whole "string" of integers.

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

3 Comments

but he is using c like a char *, look again, I think he means to have c be a char *, not a int *
He's using int* because it's where he's accumulating his intermediate multiplication results. If he used char*, it would overflow.
Was guessing he was using int to somehow have room for the result, but maybe you're right. He does say he wants a char* result, but his function actually just lets c go out of scope and returns void so it's hard to say.
1
cout << a << " * " << b << " = "; for( int i = 0; i < len_a + len_b; i++ ){ cout << c[ i ]; } cout << endl; 

will yield the desired result ...

Comments

1

Your logic is correct. Just a quick reminder: When you create an integer pointer and want to use it as an array, it points to "the first element of the array" therefore when you print it, you see the address of the first element of the array c, which is "0x100100080" in your case.

To print the number (characters) stored in c you need to de-reference the pointer, i.e., print the elements in the array one after another. Or alternatively you can convert your array into a number and print it at once. For the latter, please refer to: How to convert array of integers into an integer in C?. For printing the characters one by one you could replace

std::cout<<c; 

with the following code:

int n=strlen(c); for(int i=0; i<n; i++) { std::cout<<c[i]; } 

This will print the number.

Comments

0

std::ostream (of which type std::cout is) doesn't have any overloaded operators specifically for int*, thus it falls back to the void* overload which simply outputs the pointer value in an implementation-defined manner.

Furthermore, it wouldn't be possible for an int* overload to determine that the pointer points to an array, and further still, how many elements such an array would have.

1 Comment

how would I do this operation for a char* instead?
0

for(c++14) we can use boost libarary..

#include <iostream> #include <boost/multiprecision/cpp_int.hpp> namespace mp = boost::multiprecision; int main() { mp::cpp_int s1("12368123681263817263863821638126328136218362182"); mp::cpp_int s2("345897937325785470923092923709887329092470423707534025"); mp::cpp_int S=s1*s2; std::cout << S << '\n'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.