11 Replies - 333 Views - Last Post: 03 December 2017 - 12:03 PM Rate Topic: -----

#1 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

two dimensional array issues

Posted 03 December 2017 - 10:46 AM

i have a two dimensional array but when i pass it in a function i can only see the adress in the debugger and the content of the array is not there. for one dimensional arrays it all works fine and i can see the content even when passing the array to a function

void printarray(int size, char cube[size][size]){//size = 4
      for(int i = 0; i < size; i++)
            for(int j = 0; j < size; j++)
                  printf("%c", cube[i][j]);

}



this is one example and it prints the array like it should.

but when the array is returned i can look at the debugger and see that at position 0 its filled up to 10 when it only should be 4 and at position 2 its filled up to 5 and at position 3-4 its empty. I'm not sure why this is happening I'm guessing the array has a pointer and just fills it up accordingly. I've tried a few ways to pass the array as a pointer or as a array but the pointer way only seems to work for one dimensional arrays. it just doesn't compile for two dimensions.

Is This A Good Question/Topic? 0
  • +

Replies To: two dimensional array issues

#2 jimblumberg   User is online

  • member icon

Reputation: 5525
  • View blog
  • Posts: 17,115
  • Joined: 25-December 09

Re: two dimensional array issues

Posted 03 December 2017 - 10:58 AM

You need to show more content, a small complete program that illustrates the problem would be best.

Quote

but when the array is returned

Returned from where, and how are you "returning" the array?


By the way the code you posted seems to be fine and should compile without problems, as long as size is a #defined constant defined before the function.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: two dimensional array issues

Posted 03 December 2017 - 11:07 AM

hmm.. didn't know you had to define the size :s. let me check how it looks first

https://imgur.com/a/RK0v5

here is how it looks. its the cube down in the debugger window that is all wrong it should populate 1,2,3,4 with 4 characters
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 858
  • View blog
  • Posts: 3,431
  • Joined: 13-June 14

Re: two dimensional array issues

Posted 03 December 2017 - 11:11 AM

You need to post the code here, within code tags.
Was This Post Helpful? 0
  • +
  • -

#5 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: two dimensional array issues

Posted 03 December 2017 - 11:18 AM

kinda hard to just post the code when the program is 500 rows in 8 different files.

but this is the debugger at the execution where it looks weird but prints correct.

https://imgur.com/a/mnG3p
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 858
  • View blog
  • Posts: 3,431
  • Joined: 13-June 14

Re: two dimensional array issues

Posted 03 December 2017 - 11:20 AM

Have you written all that code and are only now running it? If so, you need to work more incrementally as it's easier to find and fix problems that way.

As jimblumberg asked, post a minimal (yet complete) program that shows the problem.

This post has been edited by ndc85430: 03 December 2017 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: two dimensional array issues

Posted 03 December 2017 - 11:22 AM

but just look at the debugger in the images and you will see the problem? its not a code issue its a issue about what is actually being passed to the functions
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is online

  • member icon

Reputation: 5525
  • View blog
  • Posts: 17,115
  • Joined: 25-December 09

Re: two dimensional array issues

Posted 03 December 2017 - 11:31 AM

Quote

its not a code issue its a issue about what is actually being passed to the functions

But that is a code issue and you haven't shown how you're trying to actually pass the array into the function. All you've shown is the function but the problem is not with the function but elsewhere. We need to know how that array was defined and how you're actually attempting to pass the array into the function.


By the way your "picture" shows a way that you may be able to "see" the contents of the array. Did you notice that "tab" icon before the array in your debug window? If you press that "tab" it should expand to show you the contents of the array.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: two dimensional array issues

Posted 03 December 2017 - 11:37 AM

yes i see the tab and when i press it nothing shows. its empty

static void loadLevel(){
    int running = 1;
    struct ThisPoint point;
    char whatChar;
    char * levelArray = malloc(30 * sizeof(char));
    srand(time(NULL));   //först ta en av de första 10 kuberna i storage filen
    int position = (rand() % 10)+1;
    loadSpecificPosition(levelArray, position);
    //ta bort 8 positioner i arrayen det kan bli samma position som tas bort
    for(int i = 0; i < 8; i++){
        position = (rand() % 15);
        levelArray[position] = 'x';
    }
    char cube[10][10];
    
    createCube(4 ,cube, levelArray);
    while(running){
        
        printf("\n************* GAME RUNNING **************\n");
        printSquare(4, cube);
        printf("\nVälj x positionen du vill ändra(max:3 min:0): ");
        scanf("%d", &point.x);
        getchar();
        printf("Välj y positionen du vill ändra(max:3 min:0): ");
        scanf("%d", &point.y);
        getchar();
        printf("Välj nu tecknet du vill lägga till (0-9, a-f): ");
        scanf("%c", &whatChar);
        getchar();
        
        
        printf("%c", cube[point.x][point.y]);
        if(point.x <= 3 && point.x >= 0 && point.y <= 3 && point.y >= 0 && ((whatChar <= 'f' && whatChar >= 'a') || (whatChar <= '9' && whatChar >= '0'))){
            if(cube[point.x][point.y] == 'x' && !doesExist(4, cube, whatChar)){
                cube[point.x][point.y] = whatChar;
            }else
                printf("\nden positionen du valde är upptagen eller så finns tecknet redan. Försök igen\n");
            
        }else
            printf("något var fel testa igen\n");
        
        if(checkComplete(4, cube)){
            createArray(4, cube, levelArray);
            if(isMagic(levelArray,4))
                printf("GRATTIS du vann");
            else
                printf("Den var inte magisk, försök igen!");
        }
        
    }
}





void createCube(int size, char cube[size][size], char *array){
    int counter = 0;
    for(int i = 0; i < size; i++)
        for(int j = 0; j < size; j++){
            cube[i][j] = array[counter];
            counter++;
        }
}



void printSquare(int size, char magicSquare[size][size]){

    for(int i = 0; i < size; i++){
        for(int j = 0; j < size; j++){
            printf("%c ", magicSquare[i][j]);
        }
        printf("\n");
    }

}



those are all places I'm passing a 2d array in.

there are more places but they are printing the correct values but when i look at the debugger it looks wrong. but if i can fix this i should be able to repair the rest
Was This Post Helpful? 0
  • +
  • -

#10 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: two dimensional array issues

Posted 03 December 2017 - 11:54 AM

Okay, i found the issue. it was like @jimblumberg said. i just changed the size on the array to a fixed number and ran it and it all looked good in the debugger. thanks for the help and sorry if i was being a ass :P
Was This Post Helpful? 0
  • +
  • -

#11 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 858
  • View blog
  • Posts: 3,431
  • Joined: 13-June 14

Re: two dimensional array issues

Posted 03 December 2017 - 12:00 PM

A couple of things about the loadLevel function you posted:

1. There are lots of magic numbers (basically, numeric literals whose origin isn't made clear) and that makes it hard to reason about the code. You should give these values meaningful names, since that will help you in the long run.

2. I'd wager that on line 3, the type and variable name are the wrong way around. Presumably, a point is the thing you're modelling (so the struct should be called Point and not ThisPoint) and the current point is the instance of interest (so the variable should be called thisPoint and not point).
Was This Post Helpful? 1
  • +
  • -

#12 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: two dimensional array issues

Posted 03 December 2017 - 12:03 PM

will have to fix that, thanks. this shit is being graded so it has to look good. that load level function is kinda crap coded.

yah i tried using Point at first but then it said i need to import some package to use Point and thats why i changed it to ThisPoint :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1