Homework questions?

This is a discussion on Homework questions? within the C Programming forums, part of the General Programming Boards category; Can anyone explain to me why I got these ?'s wrong. given the following 2-dimensional array declaration and inialization: int ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Homework questions?

    Can anyone explain to me why I got these ?'s wrong.

    given the following 2-dimensional array declaration and inialization:

    int a[][4] = {{1,2,3,4}. {5,6,7,8}}

    whats the value of the expression *a(a[1]+2)?

    i said it was 3 because another way to write it is *(a[1]+2)
    which it would add 2 to the first element maiking the answer 3, right.


    Q2given the following declarations,

    char A[] = "abc", *p="def";

    whats the effect of the following statement?

    *A =P[2]

    i answered it copies the letter 'f' to the first position of A. I said this because *p points to the 2 element which 8A point to the A[] replacing the 2 element with f.

    Last ?

    given 2 dinensional array declaration:

    int A[2][5];

    what is the type A[1]?

    i said int**. because a fiend tryed to explain to me that each dimension has its own pointer. Which the way he expalined it made alot of sense.

    can anyone explain to me please why i got these wrong and whats the correct answers please?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I think the first answer is 5.

    Second, I think A becomes "abf".

    Third, A[1] is an int*. A is an int** so A[1] is an int*.

    Just what I would have said.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >int a[][4] = {{1,2,3,4}. {5,6,7,8}}
    This is a syntax error. It has a dot instead of a comma.

    >*a(a[1]+2)
    I don't believe is valid either.

    >char A[] = "abc", *p="def";
    >*A =P[2]
    .. contains an undefined variable, P. (note the upper case). If it were:
    >*A =p[2]
    then A would contain fbc.

    >int A[2][5];
    >what is the type A[1]?
    A pointer to an int, or int*. (as already stated).

    To answer some of these, why not just write a small prog to see what the answer is?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To answer some of these, why not just write a small prog to see what the answer is?
    I keep suggesting this, but obviously they think it's better to just ask what the answer is, rather than to actually try and figure it out themselves. And yet, I am not surprised...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > whats the value of the expression *a(a[1]+2)?
    meaningless, because it thinks 'a' is a function pointer, which in this instance it clearly isn't.

    Oooh, I can try code
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int a[][4] = {{1,2,3,4}, {5,6,7,8}};
        char b[] = "abc", *p="def"; 
    
        printf( "Value of a[1][2] is %d\n", a[1][2] );
        printf( "Value of *(a[1]+2) is %d\n", *(a[1]+2) );
    
        printf( "b initially %s\n", b );
        *b = p[2];
        printf( "A is now %s\n", b );
    
        return 0;
    }
    > given 2 dinensional array declaration:
    > int A[2][4];
    > what is the type A[1]?
    It seems everyone has gotten this wrong so far. Hammer is closest, but only in certain circumstances.
    First of all, read this

    The type of A is int[2][4]
    The type of A[1] is int[4]
    The type of A[1][0] is int
    See the pattern?

    If you draw the array on paper, there are no pointers (its just a contiguous block of memory), so there can be no answer which contains a *

    Since C doesn't support array assignments, we can't do this
    int b[4] = A[1];
    So we rarely see the type of A[1] in actual use, but it's there.
    But
    int b = A[1][0];
    is real enough.

    However, things change slightly when you pass arrays as parameters to functions.

    When you pass an array as a parameter, you get a pointer to the first element of the array, so
    void foo ( int *);
    foo ( A[1] );
    would be correct

    Here's some code which will probably confuse initially...
    Code:
        int (*p1)[4] = &a[1];   // point to the whole of a[1]
        int *p2 = a[1];         // point to the start of a[1]
        int i;
        for ( i = 0 ; i < 4 ; i++ ) {
            printf( "%d:  %d %d\n", i, p1[0][i], p2[i] );   // using array notation
            printf( "%d:  %d %d\n", i, (*p1)[i], *(p2+i) ); // using pointer notation
        }
    Ok, as I've said before, a[1] is an int[4], so &a[1] is a pointer to an int[4] (which is what p1 is declared as). This is about as close as you can get to actually seeing int[4] as a type in actual use.

    p2 mimics what happens if you were to pass a[1] as a parameter to a function (namely a pointer to the first element)

    The for loop illustrates the many ways of dereferencing those pointers to yeild the stored values.

    So what's the point of p1?
    Well if you want to dynamically allocate an array which is
    int arr[x][4];
    then p1 is the type of the pointer which you must allocate.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Thank you for the clarification Salem
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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