Array Problems

Discussion in 'C Programming' started by kohvirus@gmail.com, Oct 29, 2005.

  1. Guest

    Hello everyone. I'm currently trying to write a program with an array
    but for the life of me I can't seem to get the right syntax for the
    array. Here is the program as follows:

    #include <iostream>
    #include <stdio.h>


    void display(int A[] [], int m, int n)

    {
    int i;
    int j;

    std::cout << "Enter your value for m ";
    std::cin >> m;
    std::cout << "\n";

    for (i =0; i< m; i++)

    { for (j=0; j < n; j++)

    cout << " " << A[j];

    cout << "\n";
    }

    }



    Sorry if the way I write makes you cringe, still learning to to make
    code 'neat'. Anyway, here are the error outputs I am getting and I am
    unable to resolve any further:

    fail.cpp:9: declaration of `A' as multidimensional array
    fail.cpp:9: must have bounds for all dimensions except the first
    fail.cpp: In function `void display(int, int)':
    fail.cpp:21: `A' undeclared (first use this function)
    fail.cpp:21: (Each undeclared identifier is reported only once
    fail.cpp:21: for each function it appears in.)


    Any help at narrowing the problem would be appreciated. I'm using this
    to test for a CGI program I will be creating here soon.

    Thanks!
    , Oct 29, 2005
    #1
    1. Advertising

  2. "" <> writes:
    > Hello everyone. I'm currently trying to write a program with an array
    > but for the life of me I can't seem to get the right syntax for the
    > array. Here is the program as follows:
    >
    > #include <iostream>
    > #include <stdio.h>
    >
    >
    > void display(int A[] [], int m, int n)
    >
    > {
    > int i;
    > int j;
    >
    > std::cout << "Enter your value for m ";
    > std::cin >> m;
    > std::cout << "\n";
    >
    > for (i =0; i< m; i++)
    >
    > { for (j=0; j < n; j++)
    >
    > cout << " " << A[j];
    >
    > cout << "\n";
    > }
    >
    > }


    First of all, your code is C++, not C. If you have questions about
    C++, comp.lang.c++ is down the hall on the left.

    But as it happens, the array issues are (I'm fairly sure) common to C
    and C++.

    What looks like an array parameter declaration really declares a
    pointer parameter. For example,
    void func(int param[]);
    really means
    void func(int *param);

    For that matter, so does this:
    void func(int param[10]);
    The 10 is simply ignored.

    > Sorry if the way I write makes you cringe, still learning to to make
    > code 'neat'. Anyway, here are the error outputs I am getting and I am
    > unable to resolve any further:
    >
    > fail.cpp:9: declaration of `A' as multidimensional array
    > fail.cpp:9: must have bounds for all dimensions except the first
    > fail.cpp: In function `void display(int, int)':
    > fail.cpp:21: `A' undeclared (first use this function)
    > fail.cpp:21: (Each undeclared identifier is reported only once
    > fail.cpp:21: for each function it appears in.)


    As the compiler is telling you, you need to specify the bounds of
    all dimensions except the first. So this:
    void func(int a[][10]);
    is legal, and really means
    void func(int (*a)[10]);
    but this
    void func(int a[][]);
    is is illegal.

    There really isn't any simple way to declare a two-dimensional array
    (equivalently, an array of arrays) with both bounds allowed to vary.
    Array indexing is defined in terms of pointer arithmetic, which
    requires a fixed element size, so you have to decide in advance how
    long each row of your array needs to be.

    There are several approaches that give you more flexibility. One of
    the most common is to use a pointer-to-pointer:
    void func(int **a);
    The argument can be a pointer to an array of pointers, where element
    in turn points to an array of int. You have to explicitly allocate
    (probably using malloc()) the array of pointers *and* each row of
    ints, which means you have to explicitly deallocate everything when
    you're done.

    If you want to take this approach, section 6 of the C FAQ (google it
    if you don't know where it is) has a lot of good information -- as do
    all the other sections.

    <OT>
    On the other hand, if you're really using C++, there's probably
    something in the STL that will do most of the work for you -- but we
    can't discuss it here.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 29, 2005
    #2
    1. Advertising

  3. Guest

    I apoligize for posting in the wrong area. I'll repost there. Thank you
    for the info you did post, it did help point me in the right direction.
    I'll see how much farther I can take it with a little more insight.
    , Oct 29, 2005
    #3
  4. wrote:
    > Hello everyone. I'm currently trying to write a program with an array
    > but for the life of me I can't seem to get the right syntax for the
    > array. Here is the program as follows:
    >
    > #include <iostream>


    For some obscure reason you posted C++ code in <news:comp.lang.c>. C++
    is a different language and has a different newsgroup
    <news:comp.lang.c++>. On the off-chance that you thought you were
    writing C, here is a version of your code that conforms to the current C
    standard and works:

    #include <stdio.h>

    void display(size_t m, size_t n, int a[m][n])
    {
    size_t i, j;
    for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++)
    printf("%2d ", a[j]);
    putchar('\n');
    }
    }

    /* mha: driver added so you can see that this works */
    #define ROWS 32
    #define COLS 4
    #define UPPERBOUND 100.
    #include <stdlib.h>
    #include <time.h>

    int main(void)
    {
    int foo[ROWS][COLS];
    size_t i, j;
    srand(time(0));
    for (i = 0; i < ROWS; i++)
    for (j = 0; j < COLS; j++)
    foo[j] = UPPERBOUND * (rand() / (1. + RAND_MAX));
    display(ROWS, COLS, foo);
    return 0;
    }




    > #include <stdio.h>
    >
    >
    > void display(int A[] [], int m, int n)
    >
    > {
    > int i;
    > int j;
    >
    > std::cout << "Enter your value for m ";
    > std::cin >> m;
    > std::cout << "\n";
    >
    > for (i =0; i< m; i++)
    >
    > { for (j=0; j < n; j++)
    >
    > cout << " " << A[j];
    >
    > cout << "\n";
    > }
    >
    > }
    >
    >
    >
    > Sorry if the way I write makes you cringe, still learning to to make
    > code 'neat'. Anyway, here are the error outputs I am getting and I am
    > unable to resolve any further:
    >
    > fail.cpp:9: declaration of `A' as multidimensional array
    > fail.cpp:9: must have bounds for all dimensions except the first
    > fail.cpp: In function `void display(int, int)':
    > fail.cpp:21: `A' undeclared (first use this function)
    > fail.cpp:21: (Each undeclared identifier is reported only once
    > fail.cpp:21: for each function it appears in.)
    >
    >
    > Any help at narrowing the problem would be appreciated. I'm using this
    > to test for a CGI program I will be creating here soon.
    >
    > Thanks!
    >
    Martin Ambuhl, Oct 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Susan Baker
    Replies:
    2
    Views:
    805
    kelvSYC
    Jun 26, 2005
  2. Shelly

    Problems, problems for newbie

    Shelly, Sep 2, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    2,178
    Shelly
    Sep 3, 2007
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    485
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    206
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    470
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page