Help with 2D arrays

This is a discussion on Help with 2D arrays within the C Programming forums, part of the General Programming Boards category; Hi, I normally program in java but am having to use C for some coursework and am getting a bit ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    Help with 2D arrays

    Hi, I normally program in java but am having to use C for some coursework and am getting a bit stuck.

    I have a function which should return a 2D array which is then assigned to a variable. ie

    Code:
    float[4][2] func1(){
      float 2Darray[4][2] = ...
      return 2Darray;
    }
    
    void func2(){
      ...
      float var[4][2] = func1();
      ...
    }
    But for the line in which func1 is called I get the following error:
    error: invalid initializer


    Can anyone point out what I'm doing wrong?
    I'm compiling with MinGW if that's of any significance.

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    You can't assign an array in C or C++, only pointers (to arrays).

    The second point is that you can't return a pointer to a local variable, because the variable goes out of scope (and the pointer becomes invalid) when the function exists. To counter this, we can make the variable static (see code).

    Eg.
    Code:
    #include<stdio.h>
    
    float (*func1 ( void ) )[2] {
        static float twoDarray[4][2] = {
            { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }
        };
        return twoDarray;
    }
    
    void func2 ( void ) {
        float (*var)[2] = func1();
        printf( "%f\n", var[1][1] );
    }
    
    /*
     * But the raw pointer notation can get heavy (and obscure)
     * so a typedef can make the code much more readable.
     */
    typedef float (*twoDtype)[2];
    
    twoDtype func3 ( void ) {
        static float twoDarray[4][2] = {
            { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }
        };
        return twoDarray;
    }
    
    void func4 ( void ) {
        twoDtype var = func3();
        printf( "%f\n", var[1][1] );
    }
    
    
    int main ( ) {
        func2();
        func4();
        return 0;
    }
    Note that the array is never copied, func2 only has a pointer to the array declared static in func1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Recommending static variables?
    The best way to achieve it is to let the function take a pointer to a 2D-array (along with array size, of course) to which it can write to. This is the usual approach to solving a problem when you need to return a local variable from your function(s).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Recommending static variables?
    Nope, just fixing the problem with as few changes as possible
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 08:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 06:33 AM

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