Thread: please help array question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    please help array question

    Hey folks. I'm a little confused here. I'm trying to write some code and I was wondering... if I have a two dimensional array declaration as such:

    int A [2][5];

    I'm asked to use A[1] in the code. I'm confused because I'm not sure what the type of A[1] is. Is it some kind of pointer to int or is it incorrect syntax and it has no type. Wouldn't A[0][1] be more correct of A[1][0]? Any info would be great.

    Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    oops. please excuse my mistake in not code tagging that, and I meant

    wouldn't A [1][0] or A [0][1] be more correct in accessing an element?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I'm asked to use A[1] in the code
    Like
    int *pointerToSecondRow = A[1];

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It depends on what you're actually supposed to do:

    This is perfectly valid code:
    Code:
    /* onedim.c */
    
    #include <stdio.h>
    
    void show_one_dim(int *a, int nelems)
    {
      int i;
    
      for(i = 0;i < nelems;++i)
         printf("%d ", a[i]);
      putchar('\n');
    }
    
    int main(void)
    {
      int a[2][5] = { { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 } };
    
      show_one_dim(a[0], sizeof(a[0]) / sizeof(*a[0]));
      show_one_dim(a[1], sizeof(a[1]) / sizeof(*a[1]));
    
      return 0;
    }
    My output:
    Code:
    itsme@itsme:~/C$ ./onedim
    0 1 2 3 4
    5 6 7 8 9
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    To get me started they just give me:

    Code:
    int A [2][5];
    Then it says to use the type A [1] in the code (if the expression has a type, if it is not, state why it's not) . If it does, I have to state what type of expression it is. They ask if it is an int, int*, or a int**. And that's all they give me so I wasn't sure what to do. If you guys could steer me in the right direction, I would really appreciate it.

    Thanks

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you have int A[2][5] then A[1] is type int *.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    I'm pretty new to C and just so I understand the concept...... is
    A [1] a pointer to int all of the time or just in your example of
    code? Does A [1] mean that it points to only one dimension of the array? does A [0] point somewhere else? Does
    &A[1] mean something else? I just want to be sure that I understand. Thanks for the help man, I really appreciate this.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Given this:
    Code:
    int A [2][5];
    Then A[1] has type int [5]. When used in value context it becomes an int *, a pointer to the first element of the array.

    An interesting effect then occurs with sizeof, which uses object context.
    Code:
    printf("sizeof A[1] = %d\n", (int)sizeof A[1]); /* sizeof A[1] = 20 */
    Arrays are not pointers so if you had this.
    Code:
    int *a = A[1]; /* &A[1][0] */
    Then you could not reassign A[1].
    Code:
    A[1] = a; /* error */
    (I hope I am getting this right.)

    [edit]http://67.40.109.61/torek/c/pa.html
    Last edited by Dave_Sinkula; 02-06-2006 at 04:24 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM