i want to print a array

This is a discussion on i want to print a array within the C Programming forums, part of the General Programming Boards category; i have written code to print array rain[3][5], the code is like this: Code: #include<stdio.h> void f1(float a[][],int c); int ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    i want to print a array

    i have written code to print array rain[3][5], the code is like this:
    Code:
    #include<stdio.h>
    void f1(float a[][],int c);
    
    int main(void)
    {
        int a;
        float *b;
        static float rain[3][5]={
        {10.2,8.1,6.8,4.2,1.5},
        {10.2,8.1,6.8,4.2,1.5},
        {10.2,8.1,6.8,4.2,1.5}};
        
        f1(rain,3);
      
        system("pause");
        return 0;
    }
    void f1(float a[][],int c)
    {
         int d,d1;
         for (d=0;d<c;d++)
         {
           for(d1=0;d1<5;d1++)
            printf("%5.1f",a[d][d1]);
           putchar('\n');
         }
    }
    but i get a error "invalid use of array with unspecified bounds" for "printf("%5.1f",a[d][d1]);" statement. what is the problem? thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > void f1(float a[][],int c)
    Change the prototype and definition to
    void f1(float a[][5],int c)

    It's only the left-most dimension which can be left empty.
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    timhfx, that is probably the last of a number of errors being reported by the compiler. As a rough rule you should begin by trying to address the errors a compiler reports first because errors detected later may be a side effect of previous ones.

    The first errors you would have received would have been about the declaration of function f1(); when passing multi-dimensional arrays, you can get away with omitting only the first dimension: all of the others have to be specified.

    In your case, f1() should presumably be declared as;
    Code:
    void f1(float a[][5],int c);
    The implementation, which comes later on, would need to do the same thing.

    Your compiler would almost certainly have complained about these first. How do I know? Because the problem with the printf() call is a side-effect of the earlier errors - despite the fact you didn't bother to look at, or give us information about, those earlier problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 12:41 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 10
    Last Post: 07-13-2003, 01:48 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

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