Thread: Wish to populate a 2D array using pointers

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    Wish to populate a 2D array using pointers

    Hi,

    Last night I had a go at populating a 2D 8x8 array using pointers - I want to use pointers as a memory exercise. From memory this is what I had:

    Code:
    int main()
    {
      char gameGrid[8][8];
    
      /* I just want to understand how to pass the grid as reference and fill in using pointers */
    
      initialGrid( gameGrid, 8, 8);  /* I will be remove hard coding later*/
    
    }
    
    void initialGrid(char *grid, int colsize, rowsize)
    {
      int i,j
    
    
      for (i=0; i<colsize; i++)
        for (j=0; j<rowsize; j++, grid++)
          *grid[i][j] = '.';  <---- I believe this is wrong.  
    
      printf("\n\nGrid initialised with dots");
    }
    I can compile successfully using gcc under Mandrake linux. Running gives me a segmentation fault. I am not sure what is the issue? I suspect it is the way I am trying to populate the gird. Apologies if any syntax is wrong but I'm at work(sql) right now.
    Any pointers (sic) to where I am falling over?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void initialGrid(char *grid, int colsize, rowsize)
    This should be one of these
    void initialGrid(char grid[8][8], int colsize, rowsize)
    void initialGrid(char grid[][8], int colsize, rowsize)
    void initialGrid(char (*grid)[8], int colsize, rowsize)

    Notice the similarity of the first one to the actual array you want to pass. In fact, copy/paste always works.
    Even though the first two lack a '*', they're still passed as a pointer.
    And make sure you prototype it before you call it.


    > *grid[i][j] = '.';
    This is just
    grid[i][j] = '.';
    Exactly like you would if the array was in scope.
    Oh, and drop the grid++ from the loop as well.

    Or when you say 'pointer', do you mean
    (*(grid+i))+j)
    kinda thing (that may be wrong BTW)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Thanks Salem - I'll give this a whirl tonight.

  4. #4
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Thanks. I tried it tonight and got my little square grid. For debugging I printed out the memory each character took using lx%.

    eg.
    grid[0][0] = 9fcba864
    grid[1][0] = 9fcba865
    grid[2][0] = 9fcba866 and so on...


    I know that this is hexadecimal, but from it can you determine where this memory exists in your pc system, say if you had 1Gb ram? ie, does the value show how high up the chain it is?

    Code:
    #include <stdio.h>
    #define ROWSIZE 8
    #define COLSIZE 8
    
    int main()
    {
      char grid[ROWSIZE][COLSIZE];
       
      initializeGameGrid(grid, ROWSIZE, COLSIZE);
      printGameGrid(grid, ROWSIZE, COLSIZE);
      return 0;
    }
    
    initializeGameGrid(char grid[ROWSIZE][COLSIZE], int rowsize, int colsize)
    {
      int i,j;
      
      /* initialise gameboard with dots down by row and column */
      for(i=0; i<rowsize; i++)
        for (j=0; j<colsize; j++){	    
          grid[i][j] = '0'; 
          printf("Address at grid %d,%d is: %lx\n",i,j,&grid[i][j]); /* testing */
        }
    }
    
    printGameGrid(char grid[ROWSIZE][COLSIZE], int rowsize, int colsize)
    {
      int i,j;
    
      /* position and center grid */
      printf("\n\n          ");
      
      for(i=0; i<rowsize; i++)
        for(j=0; j<colsize; j++){
          printf("%c",grid[i][j]);
          if(j == colsize-1) /* at end of a row (last column) put a CR */
    	printf("\n          "); 
        }
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The address that you are seeing in your application is a virtual address. There is no (trivial) way to find out what physical address that is, without involving kernel level code. If you have a second machine, you could use WinDBG to find the physical address. But I doubt you'd be wiser - just less hair in a more gray shade...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by matsp View Post
    The address that you are seeing in your application is a virtual address. There is no (trivial) way to find out what physical address that is, without involving kernel level code. If you have a second machine, you could use WinDBG to find the physical address. But I doubt you'd be wiser - just less hair in a more gray shade...

    --
    Mats
    Ok thanks Mats - I thought I might be stretching it there alright.

  7. #7
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    I played around with array sizes and used the rand & srand functions to give different sizes. Great fun thanks!

    Is it possible to move a character (l,r,u and down) on the screen simply pressing keys? I am thinking that 99&#37; of the time inputs to getch() required the user hitting return?

    ie, assuming I can define a screen height, refresh the screen and redraw the grid and the updated character - how could I avoid having to hit return for moving the character to another square.

    screen before

    00000
    *0000
    00000

    screen after I press "right" (assigned to a letter) without hitting return:

    00000
    0*000
    00000

    Question I am wondering is how to avoid hitting return for each movement. Thanks

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is it possible to move a character (l,r,u and down) on the screen simply pressing keys? I am thinking that 99&#37; of the time inputs to getch() required the user hitting return?
    There isn't a portable way to do this, but on Dev-C++ you can use getche() from <conio.h>. This echos the character, and if you don't want that, you can try printf("\b \b") afterwards.

    Also see this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    [edit] If you're thinking of getting serious with this, try something like ncurses. Such libraries handle updating the screen for you, can display colours, allow you to use non-buffered input (that's the technical term for not having to hit enter), and generally make text programming a much nicer experience. Curses libraries exist for almost any platform. I know you can use ncurses in Cygwin under Windows, and I've heard that you can use PDCurses under Windows natively. [/edit]
    Last edited by dwks; 10-17-2007 at 11:16 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Thanks dwks! I'll read up about ncurses.

  10. #10
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Wow! I was looking at a SDL tutorial and I might go graphical instead. It looks to be a lot more simpler and ultimately more flexible and portable

  11. #11
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Have you guys used SDL with plain C ? Most examples I have looked at use C++. I'm using Dev-C++ 4.99 myself and put SDL's lib and include files in their respective locations. I'd be curious to see what you guys think of SDL in general - just for light stuff like my array example.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I have and I know dwks has, check out his website he's got a few examples (the SDL manual is pretty good too ). Don't just limit yourself to SDL though, consider other things like using openGL (with glfw) you'd be amazed how easy it is.

    I find the SDL a bit bloated, it comes with a lot of stuff that isn't needed for small games (one of them is unicode support ) other than that it's pretty good, and really flexible -- not to mention highly extensible.

  13. #13
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by zacs7 View Post
    I have and I know dwks has, check out his website he's got a few examples (the SDL manual is pretty good too ). Don't just limit yourself to SDL though, consider other things like using openGL (with glfw) you'd be amazed how easy it is.

    I find the SDL a bit bloated, it comes with a lot of stuff that isn't needed for small games (one of them is unicode support ) other than that it's pretty good, and really flexible -- not to mention highly extensible.
    Thanks zacs. I guess I think of SDL first since it's pretty well documented to integrate with dev-c++ for example. If it works with plain C - then even better. Up to now I have never used a graphic library to help with output. SDL is cross platform as well which sounds great - window or linux.
    I might annoy dwk and check his website. It would be nice to get a simple window up with that 2D grid up and get a few lines of text in and refresh the screen with changes and so on. Once that happen's it can only get better.
    I would like to try OpenGL later - once I can walk.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Best thing about SDL then is you can use openGL if you want . Font rendering is also made really easy with SDL_TTF.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I confirm the rumour: I use the SDL all of the time, nearly always with plain C.

    The SDL website has all sorts of information. You can download the SDL, browse sub-libraries for the SDL (I recommend SDL_image, SDL_gfx, and SDL_ttf), look at some example programs, read the manual, browse some tutorials, check out an FAQ, join a mailing list . . .

    It's nice of you to check out my website. It's really old, though; my only SDL program there is Sail the Seas, and I haven't worked on that for many months. I often upload stuff there without actually creating a web page for it, however. One thing you could have a look at is xuni. I just uploaded the latest version of xuni before making this post, a modified version of SVN revision 55. http://dwks.theprogrammingsite.com/m...wn/xuni.tar.gz

    If you're using Windows, you'll have to compile it yourself, because the Win32 executable included is so ancient it probably doesn't run. And to do that you'll need to download SDL, SDL_image, SDL_gfx, and SDL_ttf. Bit of a hassle, but you'll probably want to use at least some of those libraries yourself anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointers
    By taurus in forum C Programming
    Replies: 27
    Last Post: 10-21-2008, 12:52 PM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM