Simply initializing an array AFTER defining it

This is a discussion on Simply initializing an array AFTER defining it within the C++ Programming forums, part of the General Programming Boards category; What's the syntax? For example, I would like to achieve the following: Code: int a[3] = {1, 2, 3}; ...Only ...

  1. #1
    fd9
    fd9 is offline
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Simply initializing an array AFTER defining it

    What's the syntax? For example, I would like to achieve the following:

    Code:
    int a[3] = {1, 2, 3};
    ...Only this way:

    Code:
    int a[3];
    
    
    ...
    
    //syntax goes here for achieving the same darn thing
    //and I do not want to do a[0] = 1, a[1] = 2, etc
    Last edited by fd9; 12-02-2005 at 09:07 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    int a[3] = {1, 2, 3};
    When you do this the compiler places the values in the executable and creates an array who's address is the first value. At run time the only way to assign values is for those values to be moved into the location of the variables. Therefor if it were possible to do something like that at run time (after an array was allready declared) it would result in lots of assignments
    Code:
    a[0]=1;
    [1]=2;
    ...
    The closest thing you could do is to set up an array as above and then use memcpy to copy the contents to another array.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use a loop and assign to each member of the array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Woop?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Simply initializing an array AFTER defining it
    By definition, you can't initialize an array after defining it.

  6. #6
    fd9
    fd9 is offline
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    Quote Originally Posted by Dave_Sinkula
    Use a loop and assign to each member of the array.
    The thing is I need to initilize it in a "visual way", which I can only do using the syntax in my first post. My only other choice is to look into those other functions. - Thanks

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You could always store your data in a file and read it in.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by fd9
    The thing is I need to initilize it in a "visual way", which I can only do using the syntax in my first post. My only other choice is to look into those other functions. - Thanks
    Well, here's an obfuscation:
    Code:
       static const struct block { int z[3]; } init = {{1,2,3}};
       int a[3];
       *(struct block*)a = init;
    Any more "visual"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  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