0

I am new to c++ so I am asking kind of a simple question, but i cant seem to find the answer. I am trying to write a function that will print all of the elements of a two dimensional integer array and print its contents. i am trying to do it using array notation using pointer notation. i dont know what to send in to the method of the pointer notation. this is my code so far:

#include <iostream>
using namespace std;


void arrayNotation(int array[][4], int row){
    for (int i = 0; i < row; i++){
        for (int j = 0; j < 4; j++){
            cout << array[i][j];
        }
        cout << "\n";
    }
    cin.get();
}
void pointerNotation(){//i dont know what to send it
    for (int i = 0; i<4; i++){
        for (int j = 0; j<4; j++){
            cout << (*(*(array + i) + j));
        }
    }

}
int main(){     
    int array[2][4] = { { 467, 223, 189, 100 }, { 222, 561, 489, 650 } };
    arrayNotation(array, 2);
    pointerNotation();//how do you send it in to be pointer notation?

    return 0;
}

Thank you!

3 Answers 3

2

What you need is

void pointerNotation(int (*array)[4]) // pass pointer to array-of-4 ints

But the above is really the same as int array[][4], as the latter is just syntactic sugar.

Note that you cannot do

void pointerNotation(int**)

as int[][4] does not decay to int**. The latter is more flexible, the former is just a pointer to arrays-of-4-ints.

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

8 Comments

when you say "But the above is really the same as int array[][4], as the latter is just syntactic sugar." , what do you mean? it is the same just in pointer notation? also how is (*array)[4] a 2 dimensional array?
int* is the same as int[] when used as a type of a function parameter. Similarly, int (*array)[4] is a notation for a pointer to a variable of type int[4], i.e. a pointer to an array of 4 ints, and the syntactic sugar for it is just int array[][4]. If you increment the pointer, then you "jump" over 4 elements, so basically it represents a 2D array. That is, *(array+i) points to the i-th row, so further dereferencing it *(*(array+i)+j) is the element located at position [i][j] in the 2D array.
oh i get that. so by putting in int (*array)[4] - is that what is making it pointer notation ?
@programmingGirl yes, it explicitly uses the pointer notation. But again, internally there is no difference. It is exactly the same situation as when writing f(int*) and f(int[]). Both are completely equivalent declarations.
thank you so much. this works. so by me doing it the array notation and the pointer notation, i get the same answer. but in theory what exactly is the difference?
|
0

I prefer to explicitly pass the number of rows and the number of columns. That way, one approach might be the following.

void pointerNotation(const int * const pArray, const int ROWS, const int COLS) { 
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            const int offset = i * ROWS + j;
            cout << *(pArray + offset);   // or pArray[offset]
        }
    }
}

Call as

pointerNotation(array, 2, 4);

6 Comments

but i dont think we are supposed to use const. we need to do using pointer notation only
const guarantees that the values are readonly, it is unrelated to pointers. You can omit them if you want, for this question, it does not make a difference.
This is an alternative approach indeed, but is using a "flat" array. Perhaps this is what @programmingGirl wants though.
i get that. my question though is, because you sent in the row and col you only needed to do int * pArray and not int * pArray[4]?
Yes, int * pArray is sufficient. As @vsoftco mentioned, in this approach, we are flattening a 2D-array to a 1D one. As a matter of fact, computer memory is linear, this is how a 2D array is stored in memory.
|
0

You can setup 2D arrays and send them 2 different ways too.

#include <iostream>

using namespace std;

void pointerNotation(int (*array)[4]){//i dont know what to send it
    for (int i = 0; i<2; i++){
        for (int j = 0; j<4; j++){
            cout << (*(*(array + i) + j)) << " ";
        }
        cout << endl;
    }

}

void pointerNotation2(int **array){//i dont know what to send it
    for (int i = 0; i<2; i++){
        for (int j = 0; j<4; j++){
            cout << (*(*(array + i) + j)) << " ";
        }
        cout << endl;
    }
}

int main(){     
    int array[2][4] = { { 467, 223, 189, 100 }, { 222, 561, 489, 650 } };

    int **array2 = new int*[2];
    for (int i = 0; i < 2; i++) {
        *(array2 + i) = new int[4];
    }

    // copying for second array
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            *(*(array2 + i) + j) = *(*(array + i) + j);
        }
    }

    pointerNotation(array);

    pointerNotation2(array2);

    return 0;
}

Output

467 223 189 100 
222 561 489 650 
467 223 189 100 
222 561 489 650 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.