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!

cybertextron
  • 10,547
  • 28
  • 104
  • 208
  • `int *c`, when you output that it will output the pointer. Don't you mean to make `char *c`? – Bob Fincheimer Sep 05 '12 at 22:38
  • 1
    Oh, lurvely- the old "I like memory leaks, double deletes, and buffer overruns" style of code. – Puppy Sep 05 '12 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? – cybertextron Sep 05 '12 at 22:39
  • @DeadMG I don't see any of those in his code. – Barmar Sep 05 '12 at 22:40
  • @DeadMG: it doesn't seem too bad, maybe the buffer overrun because he forgot the null terminator in the allocation. But it doesn't seem to be memory leak or double delete style... – Bob Fincheimer Sep 05 '12 at 22:41
  • Why are you doing this instead of using a multi-precision arithmetic library? Is this homework? – Barmar Sep 05 '12 at 22:45
  • 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`. – eq- Sep 05 '12 at 22:46
  • Lots of criticism of a newbies code style, little attempt to answer his question. – john Sep 05 '12 at 22:55
  • If he is trying to read it as a char* , i.e. c-style string, there isn't any string terminating '\0' at the end. – Anirudh Ramanathan Sep 05 '12 at 22:56
  • @john, I for one value the criticism given here. Before starting to be active here, my coding style was completely terrible. – chris Sep 05 '12 at 23:06

5 Answers5

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.

smocking
  • 3,689
  • 18
  • 22
  • but he is using c like a `char *`, look again, I think he means to have c be a `char *`, not a `int *` – Bob Fincheimer Sep 05 '12 at 22:39
  • He's using int* because it's where he's accumulating his intermediate multiplication results. If he used char*, it would overflow. – Barmar Sep 05 '12 at 22:43
  • 1
    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. – smocking Sep 05 '12 at 22:47
1
cout << a << " * " << b << " = ";
    for( int i = 0; i < len_a + len_b; i++ ){
        cout << c[ i ];
    }

    cout << endl;

will yield the desired result ...

cybertextron
  • 10,547
  • 28
  • 104
  • 208
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.

Community
  • 1
  • 1
erol yeniaras
  • 3,701
  • 2
  • 22
  • 40
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.

eq-
  • 9,986
  • 36
  • 38
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';
  }