About double dimensional array initialisation

Discussion in 'C Programming' started by mukesh tiwari, Jun 18, 2007.

  1. Hi everybody i want to know about array initialisation .
    Today i was solving a problem and here is my code

    #include<cstdio>
    #include<cstring>
    int l;
    int div(char *a,int d)
    {
    int k;
    char c[120];
    k=a[0];
    c[0]=a[0]/d;
    for(int i=1;i<l;i++)
    {
    k=10*(k-d*c[i-1])+a;
    c=k/d;
    }
    return(k%d);
    }


    int main()
    {

    char m[120],n[100];
    int k,j,p;//value[10][10];
    int value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},
    {1,7,9,3},{6,8,4,2},{1,9}};

    while(scanf("%s%s",m,n) && (m[0]!='0' || n[0]!='0') )
    {
    l=strlen(m);
    p=m[l-1]-'0';//last digit
    l=strlen(n);
    for(j=0;j<l;j++)
    n[j]=n[j]-'0';
    if(n[0]==0)
    printf("1\n");
    else if(p==0 || p==1 || p==5 || p==6)
    printf("%d\n",p);
    else if(p==2 || p==3 || p==7 || p==8)
    {
    k=div(n,4);
    printf("%d\n",value[p][k]);
    }
    else
    {
    k=div(n,2);
    printf("%d\n",value[p][k]);
    }
    }
    }

    now my problem is
    int k,j,p,value[10][10];
    value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},
    {6,8,4,2},{1,9}};
    was giving me compiler (gcc) error and when i initialise with
    declaration it worked fine as in my program . Plz some one tell me
    why is it so .
     
    mukesh tiwari, Jun 18, 2007
    #1
    1. Advertising

  2. mukesh tiwari

    Eric Sosman Guest

    mukesh tiwari wrote On 06/18/07 15:42,:
    > Hi everybody i want to know about array initialisation .
    > Today i was solving a problem and here is my code
    >
    > #include<cstdio>
    > #include<cstring>


    You may find better answers in comp.lang.c++, second
    door on the left where that noisy party is in progress
    (the party-goers aren't just loaded, they're overloaded).

    But *if* you'd like to write C instead, the question
    boils down to

    > now my problem is
    > int k,j,p,value[10][10];
    > value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},
    > {6,8,4,2},{1,9}};
    > was giving me compiler (gcc) error and when i initialise with
    > declaration it worked fine as in my program . Plz some one tell me
    > why is it so .


    ... and this turns out to be Question 8.3 in the
    comp.lang.c Frequently Asked Questions (FAQ) list at

    http://www.c-faq.com/

    --
     
    Eric Sosman, Jun 18, 2007
    #2
    1. Advertising

  3. mukesh tiwari <> writes:
    > Hi everybody i want to know about array initialisation .
    > Today i was solving a problem and here is my code
    >
    > #include<cstdio>
    > #include<cstring>


    Those are C++ headers. Either write C or post to comp.lang.c++.

    But as it turns out, the rest of your program appears to be valid C
    (apart from the error you're asking about).

    Incidentally, the way you format your code makes it very difficult to
    read. Use consistent indentation. I use 4 columns for each level;
    some poeple use up to 8. Avoid tab characters for code posted to
    Usenet; try to use just spaces for indentation. (I suspect your own
    tab settings are different from mine. I see your code with 8-column
    tabstops, which is normal.

    [snip]

    > now my problem is
    > int k,j,p,value[10][10];
    > value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},
    > {6,8,4,2},{1,9}};
    > was giving me compiler (gcc) error and when i initialise with
    > declaration it worked fine as in my program . Plz some one tell me
    > why is it so .


    Your first line declares the objects k, j, p, and value; it doesn't
    initialize them.

    Your second line is not an initialization, it's an assignment. In an
    assignment, the right hand side has to be an expression; an
    initializer can be either an exression or a more complex construct
    such as the one yoy're trying to use here.

    You need to make the initialization part of the declaration of value:

    int k, j, p;
    int value[10][10] =
    { {0},
    {0},
    {6, 2, 4, 8},
    {1, 3, 9, 7},
    {6, 4},
    {0},
    {0},
    {1, 7, 9, 3},
    {6, 8, 4, 2},
    {1, 9} };

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 18, 2007
    #3
    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. Venkat
    Replies:
    4
    Views:
    975
    Venkat
    Dec 5, 2003
  2. Sydex
    Replies:
    12
    Views:
    6,507
    Victor Bazarov
    Feb 17, 2005
  3. Tim Clacy
    Replies:
    8
    Views:
    396
    Tim Clacy
    May 30, 2006
  4. Olaf P.

    Array of Array Initialisation

    Olaf P., Apr 24, 2007, in forum: C Programming
    Replies:
    2
    Views:
    326
    Duncan Muirhead
    Apr 24, 2007
  5. Replies:
    5
    Views:
    434
    James Kanze
    Jun 27, 2008
Loading...

Share This Page