0

Here is a segment of my (incomplete) code

int rows(int board[][9]){
    int badUnits = 0, i = 0, n = 9, j, z = 0;
    int (*temp)[9];

    //Sort each row of 2d array
    for (z; z < n; z++){
        for (i; i < n; i++){
            for (j = i; j < n; j++){
                if (board[z][i] > board[z][j]){
                    temp = board[z][i];
                    board[z][i] = board[z][j];
                    board[z][j] = temp;
                }
            }
        }
    }

    printf ("%d\n", temp[1][0]);
    printf ("%d\n", temp[1][1]);

    return badUnits;
}

The function takes a 9*9 array.

I get a segmentation fault when the print statements are executed. I believe my sort code is correct because it is similar to what I use for 1d arrays and I think everything else is correctly assigned.

So the culprit would be my temp variable. I have gone through and tried to assign values to it, tried to change the type, and have taken into account that the 2d array decays into a pointer but is not actually a pointer.

The conclusion I am left with is that this is a dynamic allocation issue. Can someone please lend a hand and assist me in fixing this? I have exhausted my knowledge base and am stuck.

To clarify: I decided to print the temp variable because I thought it would lend some information. The main problem was that the swap was not working, and I was still left with an unsorted array when I originally attempted to print out the board[][]. I know that board is what I am SUPPOSED to be printing.

Thank you for any help!

8
  • 1
    temp will only get assigned to if a swap occurs (which wouldn't be the case for a sorted input). Also why to you stop your loop at less than n=8 (ie. max z,i,j will be 7) if your board is 9x9? Commented Oct 19, 2014 at 23:16
  • Set n to 9, thank you for that. Commented Oct 19, 2014 at 23:23
  • 1
    Also why do you not declare temp as int temp since you are storing ints in that variable during the swap (which is inconsistent with the printf)? Commented Oct 19, 2014 at 23:26
  • 1
    I'm not referring to the format specifier. I'm referring to the fact that in the swap uses temp as if it were an int, but the argument temp[1][0] uses that value and interprets it as a pointer. Commented Oct 19, 2014 at 23:38
  • 1
    temp = board[z][i]; should give you a compiler error. Pay attention to those! Commented Oct 19, 2014 at 23:42

3 Answers 3

1

You assign an int value to temp

 temp = board[z][i]; // Temp now is a what ever value was at 
                     // That location in the array e.g. 42

You then treat temp as if it was the address in memory of an integer array

 temp[1][1] // treat temp as a pointer and find the integer 
            // 10 integers further along then temp.

Also sometime temp will not have been initialised (never assigned to) in this case your going to get unexpected behaviour depending on what the last value stored where temp is now (Lets call it a random number).

Did you mean to output the values in board?

printf ("%d\n", board[1][0]);
printf ("%d\n", board[1][1]);
Sign up to request clarification or add additional context in comments.

Comments

0

One thing to notice is that the variable temp will only get assigned to if a swap occurs, if the sorting algorithm was correct that is still a situation that could occur with a input corresponding to a sorted board.

But more importantly, the variable temp is used as an int during the swap. Later that integer value is interpreted as a pointer in the expressions temp[1][0] and temp[1][1], which in all likelihoods is not a valid address. You may want to change temp to be declared as:

int temp;

And figure out exactly what you would like to print. If it is whatever one of the two swapped values was (for the last swapped pair in the loop), then you would print it as:

printf("%d", temp);

Else, you would have to add logic according to what you really want to do.

Note that a single pass of this algorithm would not perform a complete sort, but I guess that's one of the reason why you said the provided code was not complete.

Comments

0

Something like this?

#include <stdio.h>
#include <stdlib.h>


void printArray(int** arr, int w, int h) {
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j) {
      printf("%d ", arr[i][j]);
    }

    printf("\n");
  }
}

void sortArray(int** arr, int w, int h) {
  for (int row = 0; row < h; ++row) {
    for (int col = 0; col < w; ++col) {

      for (int i = 0; i < w; ++i) {
        if (arr[row][i] > arr[row][col]) {
          int tmp = arr[row][col];

          arr[row][col] = arr[row][i];
          arr[row][i] = tmp;
        }
      }
    }
  }
}

int main() {
  int w = 9, h = 9;
  int** arr = (int**)malloc(sizeof(int*) * w);
  for (int i = 0; i < w; ++i) {
    arr[i] = (int*)malloc(sizeof(int) * h);

    for (int j = 0; j < h; ++j) {
      arr[i][j] = rand() % 10;
    }
  }

  printf("Unsorted:\n");
  printArray(arr, w, h);

  sortArray(arr, w, h);

  printf("Sorted:\n");
  printArray(arr, w, h);

  for (int j = 0; j < h; ++j) {
    free(arr[j]);
  }
  free(arr);

  return 0;
}

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.