HELP ! Array Query .

This is a discussion on HELP ! Array Query . within the C Programming forums, part of the General Programming Boards category; Hi met with an array question while studying , here it goes . we declare: int x[4][3]; then x[i][j] is ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    HELP ! Array Query .

    Hi met with an array question while studying , here it goes .

    we declare: int x[4][3]; then x[i][j] is also said to be :
    1)*(x[i] + j)
    2)*(&x[0][0] + 4*i + j)
    3)Both (a) and (b)
    4)None of the above
    actually i thing the answer shld be 1 or 4 .

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    Quote Originally Posted by joanna_88 View Post
    Hi met with an array question while studying , here it goes .

    we declare: int x[4][3]; then x[i][j] is also said to be :
    1)*(x[i] + j)
    2)*(&x[0][0] + 4*i + j)
    3)Both (a) and (b)
    4)None of the above
    actually i thing the answer shld be 1 or 4 .
    If you have no real idea how the compiler handles these expression, why don't you try a little testprogram and let the compiler do the work for you :-)

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int x[4][3];
    	printf("%p -- %p\n", &x[2][1], &(*(x[2] + 1)));
    
    	return 0;
    }
    Though, answer 1) is correct!

    - Andi -

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    1)*(x[i] + j)

    In this case x[i] is an adress. Incrementing that addres by 'j' and dereferencing it all with * infront gives you the value that x[i][j] has.

    1 2 5
    5 7 9
    2 8 3
    1 8 9

    Thats the declared array - 4 rows, 3 columns with random numbers.
    x[0] = 1
    x[1] = 5 //first element of the row

    *(x[1] + 1) = 7 //first element of the row + 1 adress forward

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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