0

So I have written a function that should simply add the values of each element stored in two separate arrays, and save them to a third array.

I don't understand what the issue is, I am simply adding together the value of the int stored at the location referenced by each of my pointers, and saving it to my third, empty, array.

My code compiles just fine, but when I loop to print the contents of my third array (which should contain the sum of the two previous arrays elements at their respective indexes) it just prints a bunch of memory addresses. What gives?

EDIT: I fixed my while loop to perform the arithmetic, and everything is working well. My working code is below. Hope it helps someone else.

#include<iostream>
#include<stdlib.h>

using namespace std;

void arrayAdd(int firstArray[], int secondArray[], int targetArray[], int size){
    int *firstPtr = firstArray;
    int *secondPtr = secondArray;
    int *tragetPtr = targetArray;

   while (firstPtr <= &firstArray[size - 1] ){
       //add the first two array elements 
       *tragetPtr = (*firstPtr + *secondPtr);

      // point to the next location
      *firstPtr++;
      *secondPtr++;
      *tragetPtr++;
   }
}


int main() {

    int totalElements;
    const size_t ARRAY_SIZE = 50;
    int firstIntegerArray[ARRAY_SIZE];
    int secondIntegerArray[ARRAY_SIZE];
    int thirdIntegerArray[ARRAY_SIZE];

    cout << "Please enter the total number of elements for your array: ";
    cin >> totalElements;

    for(int i = 0; i < totalElements; i++){
        cout << "Please enter a value for the first array at index " << i << ": ";
        cin >> firstIntegerArray[i];
    }

    for(int i = 0; i < totalElements; i++){
        cout << "Please enter a value for the second array at index " << i << ": ";
        cin >> secondIntegerArray[i];
    }

    //run our arrayAdd function 
    arrayAdd(firstIntegerArray, secondIntegerArray, thirdIntegerArray, totalElements);

    cout << "The conents of your two arrays added together is; " << endl;
    for(int i = 0; i < totalElements; i++){
        cout << thirdIntegerArray[i] << ", ";
    }

    return 0;

}
1
  • sizeof(firstArray) in arrayAdd is not what you think it is. instead, pass in as a function parameter how many elements are in there. Commented Nov 4, 2014 at 0:45

1 Answer 1

1

A local array decays to a pointer when it is passed to a function, so you can't use sizeof on it anymore. Indeed this:

void arrayAdd(int firstArray[]) {
    int *firstPtr = firstArray;

    std::cout << "sizeof(firstArray) == " << sizeof(firstArray) << std::endl;
    std::cout << "sizeof(firstPtr) == " << sizeof(firstPtr) << std::endl;
}

int main() {
    int test[] = {1,2,3,4,5,6,7,8,9,0};
    arrayAdd(test);
    return 0;
}

Prints:

sizeof(firstArray) == 8
sizeof(firstPtr) == 8

on my 64 bit machine.

Casting int[] to int* doesn't change anything since it already became a pointer as an argument. You should pass the size of the array to the method or, since you are working with C++, use an std::array or std::vector which will just solve any problem.

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

4 Comments

Thanks @Jack, I would use vectors if I had a good professor, but I don't, and he insists on teaching the class deprecated structures. So I need to do this via pointer arithmetic. I have updated my code and edited my question, but it is still not working.
Actually, I was able to get it working. Pointer arithmetic is pretty cool, albeit unnecessary. Thanks @Jack, I will accept your answer.
@AdamJ: it looks like you are not incrementing tragetPtr in your loop
@Jack I was just gonna post an answer regarding that mistake ;-). Another (not very severe) cause of a compiler warning is that you never use the values when you increment your pointers. You can skip all the dereferencing operators (*), i.e. firstPtr++; instead of *firstPtr++;, and the same for secondPtr and tragetPtr (there is also a typo "traget" instead of "target").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.