Thread: Incrementing array names

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Incrementing array names

    Hello guys,

    Sorryto interrupt, but I have a problem what is driving me nuts.
    First of all, I have some programming background, but I am not a professional.
    I learned C & C++ just by doing and informing myself. Not classes visited in college whatsoever.

    So, I try to program a Atmel microcontroller using WINAVR libs and wxDevC++ (GCC compiler based).
    In my coding, I have a function:


    ->
    Code:
    void partPicTransfer(volatile uint16_t startAddress,
                                         volatile uint8_t xSize,
                                         volatile uint16_t picSize,
                                         const uint8_t picture[]);
    As you can see, "const uint8_t picture[]" is a unsigned int array of variable size.
    While I go through the code, at one point I use a for loop and try to use global varibles to modify the array name in that function in order to get the right array at a given point.
    Example:

    -> Array I try to access:
    Code:
    const uint8_t GBNum1[] PROGMEM = {.....};
    -> Global variable:
    Code:
    unsigned int thr = 0;
    -> Execution:
    Code:
    partPicTransfer(0x681, 4, 128, GBNum(thr));

    "thr" is the value, which should increment the array name in the loop.
    That way, I get of course linker problems, since GBNum is unknown.
    So, how can I make the program to recognize my operation?

    Again, sorry if this is annoying you, but I worked the last 3 hours on that and still am lacking some C background.

    I would really appreciate it, if you could give me a hand here.
    Greetings,

    Thomas. (LFrieza)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    partPicTransfer(0x681, 4, 128, &GBNum[thr]);
    perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Nope.
    The compiler tells me that GBNum is undeclared, which is understandable, since the arraysnames are the ones I try to increment.

    Array names I try to access that way (listup):

    Code:
    const uint8_t GBNum0[] PROGMEM = {.....}
    
    to
    
    const uint8_t GBNum9[] PROGMEM = {.....}
    As you can see, I try actually to access the array named "GBNum0" - "GBNum9".
    "thr" should therfore only increment the last part of the arrayname.

    Thanks for the try though.

    Greetings,
    Thomas

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    make it 2-dimentional array instead. GBNum[9][]
    If you cannot do it - use array of pointers
    GBNum[9], initialize pointer to point to the corresponding arrays GBNum1, ..., GBNum9
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by LFrieza View Post
    I learned C & C++ just by doing and informing myself.
    I.e., the usual way.

    You need a 2-dim array, like vart says. Or something like:
    Code:
    uint8_t **GBNum[9];
    GBNum[0] = GBNum0;
    ...

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Smile Helped perfectly!

    Hello again,

    thank you all very much for your help!
    Finally, I got that problem out of my code...

    Adding:
    Code:
    uint8_t *GBNumbers[10] = {GBNum0, GBNum1, GBNum2, GBNum3, GBNum4, GBNum5, GBNum6, GBNum7, GBNum8, GBNum9};
    Using as follows:
    Code:
          partPicTransfer(0x68C, 4, 128, GBNumbers[7]);
          partPicTransfer(0x68F, 4, 128, GBNumbers[0]);
    Now I get to the information I wanted; thanks again for your time!
    Hopefully, I will be able to help you also in the near future!

    Have good nights,
    Thomas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Array names and char*'s
    By NickESP in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 12:12 PM