Thread: constant structure array - hlep please

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    constant structure array - hlep please

    I've got a constant structure array that represents a song, i declare another const struct of the same type and make this constant structure array = the other one, what is the correct code


    Code:
    //**************************
    typedef struct 
    {
      int note;
      int beats;
    }Note_Beats;
    //**************************
    
    
    
    //***********************************************
    //***************Song list***********************
    //***********************************************
    
    //********When The Saints Go Marching In*********
    #define NumberOfBeats_Saints 16
    const Note_Beats Saints[NumberOfBeats_Saints] = { {C6, 1}, {E6, 1}, {F6, 1}, {G6, 5},      {C6, 1}, {E6, 1}, {F6, 1}, {G6, 5},       {C6, 1}, {E6, 1}, {F6, 1}, {G6, 2}, {E6, 2}, {C6, 2}, {E6, 2}, {D6, 5} };
    //***********************************************  
      
    //**************Choose Song Here*****************
    #define NumberOfBeats  NumberOfBeats_Saints
    const Note_Beats Song = Saints;
    //***********************************************
    I am getting the error where I decalare "Song" and make it = "Saints"
    ie at this code:
    Code:
    const Note_Beats Song = Saints;
    help please!

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    can you not make an array equal to another array or something??? sooo consfused :

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    const Note_Beats Song = Saints;
    When Song is pushed onto the stack, it's set to be const. Then you try and assign something to it but it's already const and you can't change something that's const. You could do it just like you did for the first const struct... or in C++ you could create a constructor for the struct and do the following:
    Code:
    const Note_Beats Song(Saints);
    but I'm afraid your options are limited in C. Would a memcpy of the contents work perhaps (copying from one const struct to another)?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just make a list of songs, and wander through it with a pointer?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    const Note_Beats Song = Saints;
    When Song is pushed onto the stack, it's set to be const. Then you try and assign something to it but it's already const and you can't change something that's const. You could do it just like you did for the first const struct... or in C++ you could create a constructor for the struct and do the following:
    Code:
    const Note_Beats Song(Saints);
    but I'm afraid your options are limited in C. Would a memcpy of the contents work perhaps (copying from one const struct to another)?
    Im not trying to change the value of the const, im declaring it and setting it to be equal to the const Saints struct

    and what is this code going to do??
    Code:
    const Note_Beats Song(Saints);
    doesnt make any sense to do this???

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are trying to assign an array to a single element. How can you fit several rooms inside one room?
    The line of code you are confused about is C++ syntax. If you do not use C++, you can ignore it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    { {C6, 1}, {E6, 1}, {F6, 1}, {G6, 5},      {C6, 1}, {E6, 1}, {F6, 1}, {G6, 5},       {C6, 1}, {E6, 1}, {F6, 1}, {G6, 2}, {E6, 2}, {C6, 2}, {E6, 2}, {D6, 5} }
    It lets you assign C6 etc for the int note part? Should have a 0x in front of that if it's supposed to be hex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot allocate an array of constant size 0
    By steve1_rm in forum C Programming
    Replies: 9
    Last Post: 05-26-2009, 03:57 AM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM