Thread: Question in arrays and pointers

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Question in arrays and pointers

    Code:
      1.#define ROWS 9
      2.#define COLUNMS 9
    
      3.char grid[ROWS][COLUNMS] = {{0,0,'B',     'A','D',0     ,'H','F',0},
                                {'D',0,'H', 'B','E',0     ,0,0,0},
                                {'A','G',0, 'C',0,0     ,0,0,'D'},
                   
                                {0,'A',0,     0,0,0 ,0    ,'G','F'},
                                {'F',0,'E', 'G',0,'B'     ,'C','D','A'},
                                {0,'I',0,     0,0,0 ,0    ,'B','H'},
                                           
                                {'E','B',0, 'I',0,0     ,0,0,'G'},
                                {'H',0,'F', 'E','G',0     ,0,0,0},
                                {0,0,'G',     'H','B',0     ,'F','C',0}};
    
      4.char * gridPtr = &grid[0][0];
    I am confused of what line 4 means. I think that gridPtr is the address of the array named grid and *gridptr is 0. Is this right or not?

  2. #2
    Registered User
    Join Date
    Oct 2013
    Posts
    7
    Excactly. You've no reason to be confused, you absolutely got it.
    The pointer to an array is always the address of the very first element of the array regardless of its dimensions. In your example that is put by using the »&« operator employed on the element grid[0][0] which is the very first element. Caused by the fact that »grid« already is a pointer to the array the expression »&grid[0][0]« is equal to just »grid« and line 4 could also be done as »char *gridPtr = grid«.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    I'm not sure if you did this on purpose but Columns is misspelled and it's bugging the heck out of me.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Quote Originally Posted by Cdd101 View Post
    I'm not sure if you did this on purpose but Columns is misspelled and it's bugging the heck out of me.
    No, it was by mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multidimensional arrays question
    By d2rover in forum C Programming
    Replies: 10
    Last Post: 11-07-2010, 10:43 AM
  2. Beginner pointers/arrays question
    By whiteandnerdy in forum C Programming
    Replies: 3
    Last Post: 07-14-2010, 02:19 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Quick question arrays/pointers
    By liljp617 in forum C Programming
    Replies: 6
    Last Post: 10-02-2008, 12:11 PM
  5. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM