Array initialization...

This is a discussion on Array initialization... within the C Programming forums, part of the General Programming Boards category; Hi there, I was hoping someone might be able to help with this... to make it very simple, basically I ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    35

    Unhappy Array initialization... (parse error before '{' token)

    Hi there, I was hoping someone might be able to help with this...

    to make it very simple, basically I have a header file that contains (amoungst other things ), this:

    Code:
    double **c;
    and another header file containing this:
    Code:
    double **dmatrix(long nrl, long nrh, long ncl, long nch);
    and a function in another file (that #includes both those headers) that contains:
    Code:
        c = dmatrix( 1, 11, 1, 11 );
        c[1] = { 0.999024,-0.097646,-0.144791,0.000000,0.000000,-0.071434,-0.078089,0.000000,0.000000,-0.012807,-0.011995 };
        c[2] = { 0.016061,0.170886,-0.058516,0.000000,0.000000,0.307973,-1.998076,0.000000,0.000000,1.217080,-0.045655 };
        c[3] = { 0.000000,0.000000,0.000000,0.054546,0.000000,0.000000,0.000000,1.673483,0.000000,0.000000,0.000000 };
        c[4] = { 0.000000,0.000000,0.000000,0.000000,0.054546,0.000000,0.000000,0.000000,1.673483,0.000000,0.000000 };
        c[5] = { -0.007430,0.166496,-0.178301,0.000000,0.000000,-0.720239,0.732894,0.000000,0.000000,1.443381,-0.184702 };
        c[6] = { -0.008108,0.225381,0.815775,0.000000,0.000000,0.322442,2.143341,0.000000,0.000000,-0.655258,-0.326022 };
        c[7] = { 0.000000,0.000000,0.000000,0.955720,0.000000,0.000000,0.000000,-1.374817,0.000000,0.000000,0.000000 };
        c[8] = { 0.000000,0.000000,0.000000,0.000000,0.955720,0.000000,0.000000,0.000000,-1.374817,0.000000,0.000000 };
        c[9] = { 0.004465,0.068339,-0.490358,0.000000,0.000000,1.445710,-0.201925,0.000000,0.000000,-0.695100,-0.242895 };
        c[10] = { 0.000499,0.264514,-0.062677,0.000000,0.000000,-0.094937,-0.037686,0.000000,0.000000,-0.080919,-1.370790 };
        c[11] = { 0.001650,0.474061,-0.068550,0.000000,0.000000,-0.681962,-0.420221,0.000000,0.000000,-0.784010,1.503431 };
    when I try to compile it, I get this:
    Code:
    basis.c: In function `initializeBasis':
    basis.c:64: error: parse error before '{' token
    basis.c:65: error: parse error before '{' token
    basis.c:66: error: parse error before '{' token
    ...
    basis.c:74: error: parse error before '{' token
    I know I must be doing something wrong... perhaps with pointers...
    oh and one other thing - it works fine when I do
    Code:
    ...
    c[11][1] = 0.001650;
    c[11][2] = 0.474061;
    ...
    (the entire file is generated by a perl script i wrote that parses the output of a fortran program called GAMESS, which does quantum chemical calculations - just in case people were wondering why i had so many hardcoded values).

    can anyone suggest anything?

    oh! almost forgot! - the function dmatrix() does the memory allocation for the array... (and that all works fine btw - so no problems there that could be causing it)


    cheers

    James
    Last edited by eccles; 12-10-2004 at 12:56 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't do assignments like that. You can only assign that way when the variable is declared. Otherwise, you have to assign each member of a structure or element of an array one at a time:
    Code:
    struct foo
    {
        char *bar;
        int x;
    } array[] = 
    
    {
        { "one", 1 },
        { "two", 2 },
        { "tre", 3 }
    }; /* this is valid */
    
    struct foo instance;
    instance = { "four", 4 }; /* this is not */
    
    instance.bar = "five"; /* valid */
    instance.x = 5; /* also valid */
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    ahhhh thankyou!!

    I had a feeling it might have been something like that but I was kinda hoping it wasn't since the variable being assigned to really needs to be global (it's the basis set of eigenvectors for the quantum chemical calculations that the program does) - so i really wanted to keep the declaration in the header file.

    but earlier in the day i just did a simple
    Code:
    c[nbasis][nbasis] = {
            { ... },
               ...
            { ... },
    };
    and that worked perfectly.
    but unfortunately I'm new enough to C (it's just been a couple of weeks but i have done heaps of java and perl) to think that perhaps it had to do with the fact that c was declared as **c, not c[n][n]... (even though I have read about pointers and arrays and their special affinities...)

    well that's a shame!

    thanks so much for clearing that up!
    Last edited by eccles; 12-10-2004 at 01:07 AM.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    HEY!!! wait a second!

    this is C... so would i be able to declare a POINTER in my header file, and then after creating the array c[][], just do pc = &c; ??

    would that work;

    if so, god bless you C!


    P.S. I'm very sorry that I happened to have an array called c at the center of my problem, and also keep referring to the programming language C... but I'm sure you'll all cope!

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    damnit another problem!!

    every time I try to click on quzah's 'add to reputation' link, my browser bites the dust.

    I am using Mac OS 10.3.5 + Firefox 1.0 (and yes I have enabled popups for this site)

    (because i have been working on my code on the train and at work and so I am still using my ibook even though I am home and have a far more powerful linux box sitting just behind the cute laptop that's currently burning my left hand! :P)
    VIM + gcc + beer... does life get any better?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array initialization with int variable
    By tsantana in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 02:48 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Array initialization has me dumbfounded.
    By chad_crider in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2006, 10:13 PM
  4. Difference between array initialization
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 10:51 PM
  5. Basic Array Initialization in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 05:41 PM

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