Thread: 2-D array through pointer

  1. #1
    Registered User
    Join Date
    Dec 2007

    2-D array through pointer


    I am having some problem handling 2-D array through pointer.
    We know a pointer is equivalent to an array.
    for example::
    int arr[5]= {1,2,3,4,5};
    int *arrp;
    arrp = arr;
    Then *(arrp+i) will give arr[i].

    I couldn't access the elements of a 2-D array through a double pointer.
    int arr[2][2] = {{1,2},{3,4}};
    int **arrp;
    arrp = arr;
    Then *(arrp+i) will give arr[0][i] But I couldn't access elements of other rows.

    I appreciate any help.

    Thank you

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    I believe the syntax for a 2D array is something such as
    int (*arrp)[2].
    I might be wrong, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well the code you posted would have had warnings.

    int (*arrp)[2];
    arrp = arr;
    Then treat arrp as your 2D array.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Thank you. I got the idea.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM