Few questions: Bytes, Arrays, Declarations, and Buff

This is a discussion on Few questions: Bytes, Arrays, Declarations, and Buff within the C Programming forums, part of the General Programming Boards category; I had a few questions from my study guide...for my upcoming final The first one is on arrays: Code: How ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    Few questions: Bytes, Arrays, Declarations, and Buff

    I had a few questions from my study guide...for my upcoming final

    The first one is on arrays:

    Code:
    How many bytes are in the following array?
    char list [20] = "Exercise 1";
    My answer for this is 20 because i was under the impression that each part of the list is a byte...however, i really doubt that this is true.

    How does "Exercise1" play a role in this?


    Next problem... Given the following declarations:

    Code:
    float list [20], *plist, value = 2.2;
    int i = 3;
    Code:
    Identify each of the following statements as valid or invalid:
    a.  list[2] = value;
    b.  scanf ("%f", &list[I]);
    c.  list = value;
    d.  value = list;
    e.  plist = value;
    f.  plist = list;
    my answers are
    a. valid
    b. valid
    c. invalid
    d. invalid
    e. valid
    f. valid


    Last Question:

    Code:
    given the following declaration:
    int buff[30];
    
    express the address of buff[0] in two different ways
    I tried looking up the buff command but i can't find a clear definition...I don't know what this means

    Please help as my final is coming up soon...thank you

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by viciousv322
    Code:
    How many bytes are in the following array?
    char list [20] = "Exercise 1";
    My answer for this is 20 because i was under the impression that each part of the list is a byte...however, i really doubt that this is true.
    There are 20 bytes in the array.
    Quote Originally Posted by viciousv322
    How does "Exercise1" play a role in this?
    It does nothing for the size, but it initializes the data in the array.

    Added colored comments to the following:
    Quote Originally Posted by viciousv322
    Next problem... Given the following declarations:
    Code:
    float list [20], *plist, value = 2.2;
    int i = 3;
    Code:
    Identify each of the following statements as valid or invalid:
    a.  list[2] = value;
    b.  scanf ("%f", &list[I]);
    c.  list = value;
    d.  value = list;
    e.  plist = value;
    f.  plist = list;
    my answers are
    a. valid
    b. valid (if I was supposed to be lowercase)
    c. invalid
    d. invalid
    e. valid (did you think it was *plist = value?)
    f. valid
    Quote Originally Posted by viciousv322
    Code:
    given the following declaration:
    int buff[30];
    
    express the address of buff[0] in two different ways
    I tried looking up the buff command but i can't find a clear definition...I don't know what this means
    There is buff, which is the same as &buff[0].
    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.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    I didn't even notice the I was not lowercase...thanks for the colored comments. Are my other answers correct?

    What do you mean by... "There is buff"? Is that what the question means by expressing the address of buff? ( as in &buff[0] )

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    int buff[30];
    The above declares an int array of 30 elements called buff. There is obviously no "buff command", it's just the name of the identifier.

    When you want the address of an arbitrary element of an array, you can use the & operator, as with any other addressable data.

    Example, for the address of buff[5], you'd use &buff[5].

    However, since buff[i] is shorthand for *(buff + i), then buff[0] is shorthand for *(buff + 0) and since + 0 does nothing, it's short for *(buff).

    So, to get the address of *(buff) you apply &, which cancels out the *, and you are left with buff.

    In general, the address of buff[i] is buff + i and &buff[i].

    Hope that is clearer than mud.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    so would it be correct to say the address of buff[0] is buff+0 and &buff[0]

    Also, for the declarations are these answers correct?

    a. valid
    b. invalid
    c. invalid
    d. invalid
    e. invalid
    f. valid

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Dave already answered about the declarations. See his signature above.. It's a bit early in the USA for him to have had enough cheap american beer to start making any mistakes

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Should we tell them about:
    Code:
    &0[buf]
    Or would it be too much too fast?
    Code:
    #include<stdio.h>
    int main( void )
    {
        char buf[] = "Hello World!\n";
        size_t x;
        
        for( x = 0; x[buf]; x++ )
        {
            putchar( *&x[buf] );
        }
    
        /* edit - common fun variant */
        for( x = 0; x["Hello World!\n"]; x++ )
        {
            putchar( x["Hello World!\n"] );
        }
    
        return 0;
    }
    Quzah.
    Last edited by quzah; 12-15-2005 at 08:12 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well I think I already implied that due to the commutative properties of the + operator.

Popular pages Recent additions subscribe to a feed

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