Thread: Array initialization...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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 02:07 AM.

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, 09:51 AM
  3. Array initialization has me dumbfounded.
    By chad_crider in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2006, 11:13 PM
  4. Difference between array initialization
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 11:51 PM
  5. Basic Array Initialization in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 06:41 PM