I just started learning how to use pointer array and i'm trying out examples to know more about them but i get confused with them easily. So, I tried to do a simple arithmetic calculation using a pointer array.

The user will input 2 string variables for addition. I then reverse the string variable.

int i = 0;
int j;

char temp;

j = s1.length();
while (i < j)
{
    temp = s1[i];
    s1[i] = s1[j];
    s1[j] = temp;

    i++;
    j--;
}

I tried to convert 2 string variables i have into a integer pointer array before adding them together

void stringtoInt(string& s1, string& s2)
{
   int size1 = s1.size();
   int size2 = s2.size();

   intPtr intArray;
   intPtr intArray2;

   intArray = new int[size1];
   intArray2 = new int[size2];

   for (int i = 0; i < size1; i++)
   {
       intArray[i] = s1[i] - '0';
   }

   for (int i = 0; i < size2; i++)
   {
       intArray2[i] = s1[i] - '0';
   }

   addInteger(intArray, intArray2);

}

I passed in 2 integer array in a function and tried to carry some addition calculation

void addInteger(intPtr intArray, intPtr intArray2)
{
   int *p = intArray;
   int *q = intArray2;

   intPtr total = new int[MAX];

   int i = 0;

   int sum = 0;
   int carried = 0;

   while (*p != '\0' || *q != '\0')
   {
       *(total + i) = *p + *q + carried;

       if(*(total + i) > 9)
       {
           *(total + i) %= 10;
           carried = 1;
       }

       else 
        carried = 0;    

       i++;
       p++;
       q++;         
   }     

   printArray(total, i);
}

And this is how i read my pointer array

void printArray(intPtr total, int size)
{    
    for(int i = 0; i < size; i++)
        cout << *(total + i);

}

After trying to debug this pointer integer array, i still can't figure out what is wrong with them and i just started learning how to use pointer integer array.

while (*p != '\0' || *q != '\0')

This is a red flag. Your arrays are not constructed as C-style strings, so '\0' isn't likely to exist within your owned memory. You can make that happen by adding the terminating value at the end of the arrays:

 intArray = new int[size1 + 1];
 intArray2 = new int[size2 + 1];
 int i;

 for (i = 0; i < size1; i++)
 {
       intArray[i] = s1[i] - '0';
 }

 intArray[i] = '\0';

 for (i = 0; i < size2; i++)
 {
       intArray2[i] = s1[i] - '0';
 }

 intArray2[i] = '\0';

However, that's confusing and smidge wasteful. I'd favor passing the current length of the array to your subsequent functions rather than trying to mix and match an integer array and C-style string rules. It makes your function definitions more complicated, but it's conceptually consistent, and that's usually a win.

I changed my code and i tried to pass in the size of an array in this method

void stringtoInt(string& s1, string& s2)
{
int size1 = s1.size();
int size2 = s2.size();

int i;
int k;

intPtr intArray;
intPtr intArray2;

intArray = new int[size1];
intArray2 = new int[size2];

for (i = 0; i < size1; i++)
{
    intArray[i] = s1[i];
}

intArray[i] = '\0';

for (k = 0; k < size2; k++)
{
    intArray[k] = s2[k];
}

addInteger(intArray, i, intArray2, k);

intArray2[i] = '\0';

}

void addInteger(intPtr intArray, int size1, intPtr intArray2, int size2)
{
int *p = intArray;
int *q = intArray2;

intPtr total = new int[MAX];

int i = 0;

int sum = 0;
int carried = 0;

while (*p != size1 || *q != size2)
{
    *(total + i) = *p + *q + carried;

    if(*(total + i) > 9)
    {
        *(total + i) %= 10;
        carried = 1;
    }

    else    
        carried = 0;    

    i++;
    p++;
    q++;            
}    

printArray(total, i);   
}

But it still can't to get it to work and i don't know why.

Let's start by fixing things so the code doesn't break massively from out of range errors:

#include <iostream>
#include <string>

using namespace std;

typedef int* intPtr;

const int MAX = 100;

void printArray(intPtr total, int size)
{
    for (int i = 0; i < size; i++)
        cout << *(total + i);
}

void addInteger(intPtr intArray, int size1, intPtr intArray2, int size2)
{
    int *p = intArray;
    int *q = intArray2;

    intPtr total = new int[MAX];

    int i = 0;

    int sum = 0;
    int carried = 0;

    while (p != intArray + size1 || q != intArray2 + size2)
    {
        *(total + i) = *p + *q + carried;

        if (*(total + i) > 9)
        {
            *(total + i) %= 10;
            carried = 1;
        }
        else
            carried = 0;

        i++;
        p++;
        q++;
    }

    printArray(total, i);
}

void stringtoInt(string& s1, string& s2)
{
    int size1 = s1.size();
    int size2 = s2.size();

    int i;
    int k;

    intPtr intArray;
    intPtr intArray2;

    intArray = new int[size1];
    intArray2 = new int[size2];

    for (i = 0; i < size1; i++)
    {
        intArray[i] = s1[i];
    }

    for (k = 0; k < size2; k++)
    {
        intArray2[k] = s2[k];
    }

    addInteger(intArray, i, intArray2, k);
}

int main()
{
    string a = "123", b = "456";

    stringtoInt(a, b);
}

The biggest problems were leftover C-style string stuff from my example when you seem to have gone with the consistent approach, and the loop in addInteger used a nonsensical comparison for finding the end of the array. You were comparing the dereferenced value of an element against the size rather than matching the current pointed to address against the end of the passed in "array".

On a side note, your addition algorithm has issues, but it's unrelated to the original question. I don't want to fix it for you because recognizing the problems and correcting them is a good exercise. :D

That said, I'll hint to one problem: what if the arrays are different sizes?

commented: Great answer. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.