0

I have a 15x15 array which I have to traverse with pointers (hw ). I'm writing a puzzle solver and I need to search some words vertically,I've done horizontal search but I can't traverse the array column by column.I am trying to assign ptr to tmp each time after tmp reached the end of column.

void VerticalSearch(char** puzzleArray, searchedWord* word) {

int len = word->wordLength;

char **tmp = puzzleArray;
char *ptr = &puzzleArray[0][0];

string s;   

for (int i = 0; i < 15; i++) {

    **tmp = *ptr;
    s = "";
    for (int k = 0; k < 15; k++)
    {
        s += **tmp;
        (tmp)++;    

    }
    cout << s << endl;
    ptr++;          

}   
} 
  • 3
    *"which I need to traverse with pointers"* why ? `for (int i = 0; i != 15; ++i) {for (int j = 0; j != 15; ++j) { foo(puzzleArray[i][j]); } }` and `for (int j = 0; j != 15; ++j) {for (int i = 0; i != 15; ++i) { foo(puzzleArray[i][j]); } }` would allow to traverse your matrix in both directions. – Jarod42 Oct 09 '17 at 08:02
  • "I am trying to assign ptr to tmp each time after tmp reached the end of column." You never assign to `tmp`. The first line of the for loop is assigning to the char pointed to by the first pointer pointed to by `tmp`. – Martin Bonner supports Monica Oct 09 '17 at 08:03
  • 3
    Also, avoid using pointers to pointers if you *possibly* can. Look into `std::array` or `std::vector`. They are both much easier to reason about. – Martin Bonner supports Monica Oct 09 '17 at 08:05
  • It's my homework, when I want to access any character in the puzzle, I am not allowed to use array indexes or vectors, I must use the pointer arithmetics. – Engin Baglayici Oct 09 '17 at 08:25

1 Answers1

2

To actually do what you need, you have to exploit the way in which the array is allocated in memory.

I'll assume you actually allocate the array on the stack (char puzzle[15][15] somewhere in your main). In this case, even though passing it to this function will give you a warning (see this answer ) it might work.

The array is allocated in a row-major form, which means that

a1, a2, a3, b1, b2, b3, c1, c2, c3

becomes in memory

a1,a2,a3,b1,b2,b3,c1,c2,c3

So you can actually do something like

void VerticalSearch(char** puzzleArray, searchedWord* word) {

    int len = word->wordLength;

    char** tmp; // initialize the pointer

    string s;   

    for (int i = 0; i < 15; i++) {
        tmp = puzzleArray + i; // update with the i-th char of the first row
        s = "";
        for (int k = 0; k < 15; k++){
            s += *tmp;
            tmp += 15; //each time you read a character from the column
                       // go to the next character of the same column
                       // one row far
        }
        cout << s << endl;    
    }   
}

This should work, I have not tried it.

By the way, AVOID using pointer arithmetics if you can, in general, and on arrays in particular, it can give severe headaches and lead to bugs. Use usual array indexing and let the compiler take care of this stuff.

For more information on how the matrix is saved in memory, see this

bracco23
  • 2,181
  • 10
  • 28
  • `tmp = puzzleArray + i; // update with the i-th char of the first row ` This line updates the i-th char of the column, not row. – Engin Baglayici Oct 09 '17 at 09:18
  • The line doesn't update the char itself, but updates the `tmp` pointer with the i-th char of the first row, which is in turn the first char of the i-th column. – bracco23 Oct 09 '17 at 09:19
  • I meant it doesn't update the tmp pointer with i-th char of the first row,it traverses row by row. – Engin Baglayici Oct 09 '17 at 09:26
  • The problem is; even when I update the `tmp` pointer with the i-th char of the first row, when I write `tmp++` the `tmp` turns back and traverses first column. – Engin Baglayici Oct 09 '17 at 13:04