2-D array through pointer

This is a discussion on 2-D array through pointer within the C Programming forums, part of the General Programming Boards category; Hello, I am having some problem handling 2-D array through pointer. We know a pointer is equivalent to an array. ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    2-D array through pointer

    Hello,

    I am having some problem handling 2-D array through pointer.
    We know a pointer is equivalent to an array.
    for example::
    Code:
    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.
    Code:
    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
    veena

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    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
    Location
    The edge of the known universe
    Posts
    32,852
    Well the code you posted would have had warnings.

    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 06: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, 02:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21