Multidimensional arrays

This is a discussion on Multidimensional arrays within the C Programming forums, part of the General Programming Boards category; that's not the assignment operator, that's just the syntax of initialization....

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    that's not the assignment operator, that's just the syntax of initialization.

  2. #17
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Could you elaborate on that, please? I'm not quite following you.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Niels_M View Post
    Could you elaborate on that, please? I'm not quite following you.
    Initialize:
    Code:
    int bob[] = {2,3,4};
    Assign:
    Code:
    int bob[3];
    bob = {2,3,4};
    The first makes sense, the second does not -- assignment can only happen element by element, not to the array-as-a-whole. Arrays are passed to function by pointer -- so your array[5][11] is passed as a pointer to an array of arrays of size 11, and we can assign a pointer value to a pointer variable no problem:
    Code:
    int array[5][11];
    int (*ptr)[11]; //a pointer to array(s) of 11 integers;
    ptr = array; //wonderful
    Indexing on ptr will work the same as indexing on array.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Niels_M View Post
    Could you elaborate on that, please? I'm not quite following you.
    here's the BNF for declarations in N1256 (C99 + TC1 + TC2 + TC3):
    Code:
    declaration: 
        declaration-specifiers init-declarator-listopt ; 
    declaration-specifiers: 
        storage-class-specifier declaration-specifiersopt 
        type-specifier declaration-specifiersopt 
        type-qualifier declaration-specifiersopt 
        function-specifier declaration-specifiersopt 
    init-declarator-list: 
        init-declarator 
        init-declarator-list ,init-declarator 
    init-declarator: 
        declarator 
        declarator =initializer
    there's a = character there, but it's not the assignment operator. it's just a = character.
    Last edited by robwhit; 09-05-2009 at 04:54 PM. Reason: quoted the initialization section instead of the declaration section. same concept, but not what the discussion was about.

  5. #20
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Ok, so in C, when we have the follwing code

    Code:
    void testfnc(int a)
    {
    printf("%d", a);
    }
    
    int main()
    {
    int b = 5;
    testfnc(b);
    return 0;
    }
    then when "testfnc" is called, the variable "b" is transfered to "testfnc" by

    Code:
    int a;
    a=b;
    and NOT

    Code:
    int a=b;
    ?

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Niels_M
    then when "testfnc" is called, the variable "b" is transfered to "testfnc" by
    I would say that the (formal) parameter a is initialised to be a copy of the (actual) argument b. There is no assignment involved, at least not in the way that has been mentioned in this thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    In the context of C (and not C++ classes) the two examples are essentially identical (compiler implementation details aside, at least). Some memory is set aside for the variable and then the data is copied to that address. Simple as that. As to why you can't assign to an array what you can initialize it with, well, that's just the way C works. And just to be clear, in this case:

    Code:
    void f(int (*a)[5])
    The array is not copied, just a pointer to it.

  8. #23
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Sebastiani View Post
    In the context of C (and not C++ classes) the two examples are essentially identical (compiler implementation details aside, at least).
    Ok, the example was poorly chosen. In the case with arrays as parameters, then the difference is clear, just like in post #18.

  9. #24
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Right.

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by laserlight View Post
    I would say that the (formal) parameter a is initialised to be a copy of the (actual) argument b. There is no assignment involved, at least not in the way that has been mentioned in this thread.
    hmm so you're right. I thought it was copied as if by assignment like the op said, but it seems it's only *converted* as if by assignment. it doesn't say how it's copied.

  11. #26
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Copied, converted - what's the distinction being made there?

  12. #27
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    I have a final question. We have the following code

    Code:
    int main()
    {
    char* test[4] = {"one", "two"};
    return 0;
    }
    Here "test" will point at "one", and "test+1" will point at "two".

    When I write "*test", then I access the string "one", thus "*(*test+1)" will give us the value 'n'.

    When I say "access", then what is it that we are doing? I think it has to do with pointers to pointers, but I cannot see how.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Niels_M
    Here "test" will point at "one", and "test+1" will point at "two".
    More accurately, test is an array of pointers to char, not a pointer. test+1 is a pointer, and it points to a pointer to the first character of "two".

    Quote Originally Posted by Niels_M
    When I write "*test", then I access the string "one", thus "*(*test+1)" will give us the value 'n'.
    In the expression *test, test is converted to a pointer to its first element, i.e., a pointer to a pointer to char. Hence, *test is a pointer to a char, in particular, a pointer to the first character of "one". Hence, (*test+1) is a pointer to the second character of "one", thus *(*test+1) results in the value of the second character of "one", i.e., 'n'.

    By the way, since string literals should not be modified, you should declare test to be an array of const char* instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Sebastiani View Post
    Copied, converted - what's the distinction being made there?
    copying is making a duplicate value of another value. converting is taking a value of type A and giving a value of type B. I guess.

  15. #30
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by Niels_M View Post
    I have a final question. We have the following code

    Code:
    int main()
    {
    char* test[4] = {"one", "two"};
    return 0;
    }
    Here "test" will point at "one", and "test+1" will point at "two".

    When I write "*test", then I access the string "one", thus "*(*test+1)" will give us the value 'n'.

    When I say "access", then what is it that we are doing? I think it has to do with pointers to pointers, but I cannot see how.
    Maybe this will make it more clear:

    Code:
    int main( void )
    {
        typedef size_t*
            address;
        char const
            * one = "one", 
            * two = "two", 
            * array[ ] = 
            {
                one, 
                two
            };
        cout << "Address of '&array[ 0 ]': " << address( array ) << endl;
        cout << "Address of '&array[ 1 ]': " << address( array + 1 ) << endl;
        cout << "Address of 'one': " << address( one ) << endl;
        cout << "Address of 'two': " << address( two ) << endl;
        cout << "Value of 'array[ 0 ]': " << address( array[ 0 ] ) << endl;
        cout << "Value of 'array[ 1 ]': " << address( array[ 1 ] ) << endl;
        return 0;
    }

    Quote Originally Posted by robwhit View Post
    copying is making a duplicate value of another value. converting is taking a value of type A and giving a value of type B. I guess.
    Yes, but isn't that basically saying the same thing? I'm not trying to be difficult here - I just don't see any difference, really.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 02:58 PM

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