Connect with us

Question about passing a variable value to an array in C

Discussion in 'Microcontrollers, Programming and IoT' started by chopnhack, Feb 10, 2016.

Scroll to continue with content
  1. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    Hello all, as the title states, I am trying to pass a variable into an array in C. Specifically I am trying to get the random value of TMR0 of the pic that I am working on into the array. The TMR0 status is an unsigned number from 0-255(I believe). I don't find much information on Google on passing a variable into an array. Is this not the right way of doing things? Is there an intermediate step in between?

    My thinking is:
    Code:
    char Random[2] = TMR0;
    
    It would be just too easy to simply feed a number into an array :rolleyes:
    Thanks for any help
     
  2. dorke

    dorke

    2,342
    665
    Jun 20, 2015
    A few things:

    1. You need to declare the array size and type .

    2. What you "printed " is accessing element 3
    in the "Random "array,is that what you wanted?

    3. How is "TMR0" defined in your code?

    4. If you just stick a value directly,you do "find" it there right?
     
    chopnhack likes this.
  3. Arouse1973

    Arouse1973 Adam

    5,164
    1,081
    Dec 18, 2013
    What I think you want John is.

    Char Random [2]; Define array size and name
    Random [0] = TMRO; Put the value of TMRO into the first element of the array.

    Adam
     
    chopnhack likes this.
  4. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    Spot on Adam.

    Yes, I am taking the value of TMR0 which is a three digit unsigned number, and asking to place each digit into an array so that I can access only the last digit (I actually only need the last digit so other methods that don't take all of the numbers are ok too).
    From some research, its a bit more complex than I thought. Apparently using string copy and limiting it to the last digit and then placing that last digit into the new variable should do the trick. Do I need to typecast into int? I intend to use the value as a number in the next step.

    This is trivial, but my syntax and grasp of C is novice at best. This is what I think I should be looking at, your thoughts please. Thanks!

    Code:
    char RANDOM[1];                             //create a destination array 1 cell for last digit from var. TMR0
    strcpy(RANDOM, &TMR0[3]);                   //copy last digit of TMRO (third digit) into var. RANDOM
    
     
  5. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,419
    2,790
    Jan 21, 2010
    int x = tmr0 % 10;
     
    chopnhack likes this.
  6. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    Thanks Steve, I went the modulo route, but it was too taxing on the word count of the pic. 63 word count for modulo!!
     
  7. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,419
    2,790
    Jan 21, 2010
    Do you actually want the last digit, or could you use either a mod 8 or mod 16 number? Both can be done with masking and won't require a tine consuming divide.
     
  8. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    Yes sir, just need the last digit.
     
  9. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,419
    2,790
    Jan 21, 2010
    Assuming TMR1 is an unsigned byte:

    Code:
    byte a, b, c, d;
    a = b = TMR1;
    c = 0;
    
    for (d = 160; d >= 10; d >>= 1) {
    if (b >= d) {
    b -= d;
    c += d;
    }
    }
    
    d = a - c;
    
    d should be what you're after.

    You can unroll the loop to make it marginally faster.

    Sorry about the formatting, my phone wont let me type spaces unless it thinks they mean something :(.
     
  10. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,419
    2,790
    Jan 21, 2010
    The above code can be further optimized by removing all reference to a and c because b ends up with the value you want. It then becomes even easier to unroll the loop

    Code:
    byte b = TMR1;
    b = b >= 160 ? b - 160 : b;
    b = b >= 80 ? b - 80 : b;
    b = b >= 40 ? b - 40 : b;
    b = b >= 20 ? b - 20 : b;
    b = b >= 10 ? b - 10 : b;
    
     
    BobK likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-