ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

#BB8

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ส.ค. 2024
  • Let the compiler help you iterate through arrays.
    ► PCBWay $5 for 10 pieces www.pcbway.com
    We have all written a standard "for loop" to iterate through an array like this:
    char* myArray[] = {"John, "James", "Janet, "Julia", "Jeremy"};
    for (uint8_t cnt=0; cnt LT 5; cnt++) {
    // do stuff with myArray[cnt]
    }
    but there is a 'better' way, where we let the compiler work out the bounds of the array, and the variable type. Very easy.
    ► All code samples etc in my GitHub:
    github.com/Ral...
    ► Why bother?
    Basically, it means less risk that you have misidentified the number of elements in the array (causing a potential code crash or, at the very least, some nonsense results) and it makes the code much more readable.
    ► Useful or interesting video? You can support my channel:
    Buy Me A Coffee! www.buymeacoff...
    ► List of all my videos
    (Special thanks to Michael Kurt Vogel for compiling this)
    bit.ly/TH-camV...
    ► If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so and joining me on my Arduinite (and other μControllers) journey
    My channel, GitHub and blog are here:
    ------------------------------------------------------------------
    • / ralphbacon
    • ralphbacon.blog
    • github.com/Ral...
    • buymeacoffee.c...
    ------------------------------------------------------------------
    ► All code samples etc in my GitHub:
    github.com/Ral...

ความคิดเห็น • 143

  • @skillstacker9268
    @skillstacker9268 2 ปีที่แล้ว +11

    Took me a little while to realize that 'auto' had nothing to do with 'car' and was just a variable type :). Never used that one! Good stuff. I would have called these 'Bacon Bits' (is that a thing in the U.K.?)

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +2

      "Bacon Bits" are bacon-flavoured bits that one puts into a salad. I was going to call it Bacon Bytes Back but it didn't really work for a C++ series. Yes, I only noticed the "auto" and car reference whilst I was editing. Unintentional pun. Sorry to confuse you 🤣

    • @dennis8196
      @dennis8196 2 ปีที่แล้ว

      @@RalphBacon I love bacon bits, the food and the videos too 😂

  • @ArielBaravalle
    @ArielBaravalle 2 ปีที่แล้ว +1

    Thank you for these master classes!
    I'm learning things I didn't know after programming in C++ for 30 years. like your videos!

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Always good to find new techniques!

  • @fredflintstone1
    @fredflintstone1 2 ปีที่แล้ว +2

    Damn I wanted a Jaguar :-) nice little video as usual informative :-)

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Best I can offer is a shiny Merc this week, sorry. But glad you found it useful.

  • @TheUnofficialMaker
    @TheUnofficialMaker 2 ปีที่แล้ว +1

    Tried in the Arduino IDE, happy to report it works as described, very nice Ralph S Bacon. consuming all your bits!

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Fantastic! I'm so glad they got the Arduino IDE right! (although it;s the compiler that does the work, of course).

  • @OsoPolarClone
    @OsoPolarClone 2 ปีที่แล้ว

    I am enjoying these educational videos. I never learned C++ until I started learning microcontrollers in my retirement as a civil engineer that designed bridges.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      So now you're building bridges between the hardware and the software. See what I did there?

  • @sprinkdesign7170
    @sprinkdesign7170 2 ปีที่แล้ว +1

    You make this so simple and clear - amazed I haven't really understood these concepts before! Thank you.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      You're very welcome!

  • @MrJozza65
    @MrJozza65 2 ปีที่แล้ว +1

    Nice - I often use foreach in c#, didn't realise the same construct was available in Arduino C++, and the method to pass by reference to update the values of the array elements is a new trick learned for me, as I didn't know you could do that. Thanks!

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      One day you will use this technique for real and thank me (well, again).

  • @sledgex9
    @sledgex9 2 ปีที่แล้ว +2

    The type of the array in this specific case should be "const char*". Newer gcc versions warning you about this. C++ forbids converting a string constant (aka const char*) to char*. See the "-Wwrite-strings" warning switch for newer gcc.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Well... sort of. Yes the compiler does complain that the rules state you cannot convert a string to a char* array... but does it anyway, as does everyone else. If you switch on Verbose settings for the compiler you'll (briefly) see this error/warning as it flashes across the screen.

    • @fromgermany271
      @fromgermany271 2 ปีที่แล้ว +1

      @@RalphBacon
      The main feature of C++ (for me) is that can (in detail) write down your intention and the compiler finds a way to run that best on your machine.
      Downside: you have to clearly state your intend in his words. So if you say „I want a write pointer to a string literal, don’t expect it to be a constant afterwards. And if you ignore warnings, you might end up with code, that only runs most of the time. This is especially bad for inexperienced people (the typical Arduino users), because they will never find out.
      I have some respect for people teaching C++ to this target group, because I know that you might open Pandora’s box.

  • @basiljackson3829
    @basiljackson3829 ปีที่แล้ว

    Great video. Similar to FOR EACH in other languages. I've taught myself enough Arduino / C++ to do what I need to, but many tips such as this one have eluded me thus far. Makes code maintenance MUCH easier! Thanks!

    • @RalphBacon
      @RalphBacon  ปีที่แล้ว

      Thanks for sharing!

  • @OldBaloo33
    @OldBaloo33 2 ปีที่แล้ว

    Thank you for the very useful information you shared in this video. I wasn't aware that range-based loops are available in the Arduino IDE, they really make programmer's life easier.
    One small note: using reference not only allows the array members modification but also makes the code more efficient because it doesn't copy array's components to the "car" variable. It may slow the execution if you have a large array of "heavy" objects.
    You can still use reference and also prevent an accidental array modification by simply defining a "const auto& car" variable. In this case the compiler will give you an "assignment of read-only reference" error if you will try to upgrade your car to Mercedes.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Indeed. This particular bit of code benefits from references as it (could) contains many characters not just a two-byte integer. Imagine an array of a struct with 50 bytes each!

  • @chimpspecialist
    @chimpspecialist 2 ปีที่แล้ว

    I was hoping we'd get an example of "pass by reference." Can't wait for the next one, Ralph!

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      So there we are, your wish has been fulfilled!

  • @keen2461
    @keen2461 2 ปีที่แล้ว

    I never actually learned C++, so this is new to me but I find it quite useful. Hopefully I'll be able to remember this video when coding my next project. Thanks for your educational videos.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Don't forget me, Elkeen! Why not subscribe then you can always find your way back to me 👍🏻

  • @Mike-qh5bn
    @Mike-qh5bn 2 ปีที่แล้ว +1

    Very good video! Couldn't agree more with your sentiments on readability.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Thank you! Readable code is more likely to work and be better maintained for sure!

  • @TBoy58_405
    @TBoy58_405 2 ปีที่แล้ว

    Maestro! Have not seen ’auto’ used like that. Nicely done! 👍🏾🤓

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      It's a useful feature and lets the compiler do the work for us.

  • @Mr.Leeroy
    @Mr.Leeroy 2 ปีที่แล้ว +1

    did not expect such higher level syntax in a context of C..
    Guess, I really need to go through some book on C++

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      It's a pity we are stuck on C++ 11, as there is some really cool stuff in C++ 20 (not least, using range-based loops backwards!)

  • @david_pilling
    @david_pilling 2 ปีที่แล้ว +1

    for(car=0;car

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Quite so; not exactly beginner friendly! In fact, not friendly for anyone. thank goodness for range-based loops!

    • @david_pilling
      @david_pilling 2 ปีที่แล้ว

      #define NITEM(a) (sizeof((a))/sizeof((*a)))
      for(car=0;car

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Ah, yes, one of my ancestors probably wrote that #define. 😁

    • @Enigma758
      @Enigma758 2 ปีที่แล้ว

      But there ARE still times when you need the index within the loop.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Yes, indeed, and then we need to fall back to the original way to code a for statement.

  • @vonries
    @vonries 2 ปีที่แล้ว

    Great to see you again my friend.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      You too, Steven, hope you are well. And enjoyed the video. Difficult making range-based loops that exciting, really.

  • @sledgex9
    @sledgex9 2 ปีที่แล้ว +1

    Optimized code: If you don't want to modify the value then take it by const reference aka "const auto &car". If you take it by value aka "auto car", it will create a temporary copy of the value. You don't want that in the embedded world.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Yes, I'd always use the "pass by reference" approach, with a "const" to protect the array from inadvertent modification if required.

    • @fromgermany271
      @fromgermany271 2 ปีที่แล้ว +1

      „Temporary copy“
      Correct, though for a char* it‘s not a disadvantage. A reference would need the same space, but if the array would contain String objects, it would be different.

    • @sledgex9
      @sledgex9 2 ปีที่แล้ว +1

      @@fromgermany271 Correct. My comment was for the general case. Exceptions apply.

  • @CTCTraining1
    @CTCTraining1 2 ปีที่แล้ว

    Very good work Ralph ... and you have to be careful with those references which international (and younger?) audiences might not understand. 😀👍

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +2

      For such a noddy bit of code I think I got away with it. Oh.

  • @chlordk
    @chlordk 2 ปีที่แล้ว +1

    Good. I wrote a program in year 2000 where that would have been handy. I had a lot of lists and used
    #define CARS_MAX (sizeof(cars)/sizeof(cars[0]))
    Not as elegant as with C++11 but better than a constant number in the code.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Indeed. I do this too. I mentioned (did it make it into the video?) about doing something about the button-push detection. I was going to use a #define for that. I still might.

  • @romancharak3675
    @romancharak3675 2 ปีที่แล้ว +1

    I am still trying to grasp the concept of "pass by value" from the previous video. gggrrrrrrr. Not your fault, Ralph.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Keep trying out the code, Roman. Only by actually doing it will you get good at it.

  • @mahudson3547
    @mahudson3547 2 ปีที่แล้ว +1

    Lovely lardons! Great series. As a retired electronics engineer, I guess you were a programmer! But these gadgets need both skills. How about some clear guidance on pointers?

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Yes, pointers are on my list but I dread the video take-up. But even if beginners don't use them, being _aware_ of what they are and why they might be used would be a good thing!

    • @xylopyrographer9664
      @xylopyrographer9664 ปีที่แล้ว

      @@RalphBacon +1 vote for a video on pointers. It'll be another classic BB!

  • @rfdave3980
    @rfdave3980 2 ปีที่แล้ว +1

    I always like printf, Dont care for std::cout. Your spot on using printf. Ralph I must say I enjoy your videos, learnt so much from them. Big Thank you. Brilliant !

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Glad you like them!

  • @shanearnfield3350
    @shanearnfield3350 2 ปีที่แล้ว

    Great tip! I would usually have achieved this by assigning an intermediate variable, made some change, and then assigned it back to the array variable. Who said something about old dogs...!!!???

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Who are you calling an Old Dog? Oh, sorry, my mistake. 😄 Yes, useful technique (which is why I told you all about it!).

  • @castlecodersltd
    @castlecodersltd 2 ปีที่แล้ว

    Thanks for this. Very interesting

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Glad you liked it!

  • @andymouse
    @andymouse 2 ปีที่แล้ว

    Top tip Ralph !...cheers.

    • @fredflintstone1
      @fredflintstone1 2 ปีที่แล้ว +1

      Cheese!!!!!!!!!!!!!!!!

    • @andymouse
      @andymouse 2 ปีที่แล้ว

      @@fredflintstone1 Squeak!!!!

    • @TheEmbeddedHobbyist
      @TheEmbeddedHobbyist 2 ปีที่แล้ว

      @@fredflintstone1 Cheese!!!!!!!! on toast!!!!!!!!!!!!!!

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Glad you enjoyed it

  • @jstro-hobbytech
    @jstro-hobbytech 2 ปีที่แล้ว

    Very cool Ralph.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Certainly worth adding to your programming skills, Joey.

    • @jstro-hobbytech
      @jstro-hobbytech 2 ปีที่แล้ว +1

      @@RalphBacon definitely. I cramed it in my brain box. I never seen it before but understood it immediately. Learning is great. Being retired I find it's all I like to do.

    • @jstro-hobbytech
      @jstro-hobbytech 2 ปีที่แล้ว

      @@RalphBacon I don't know if I told you Ralph but I've been accumulating all sorts of arduino kits, new multimeters and just a ton of stuff to donate. I do this with my spare money when I see a good deal. There's so much stuff it's beyond comprehension. I've decided to donate all of it to a local makerspace for the people that go there who want to take this stuff home and watch videos like your's to learn. I'm remaining anonymous but I have to jump through all these hoops to give away a couple grand worth of stuff. It's like people don't believe in human kindness anymore. Haha

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      It's a shame you have hurdles to clear just to give away stuff, but the end results justifies the effort.

    • @jstro-hobbytech
      @jstro-hobbytech 2 ปีที่แล้ว +1

      @@RalphBacon sorry for venting Ralph. It's not the place to do so

  • @kissingfrogs
    @kissingfrogs 2 ปีที่แล้ว

    I did not know about this. Cool tip. Thanks for sharing. What is the IDE, I think I missed it.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      I'm using Visual Studio Code with PlatformIO add-on. Worth looking into if you're up for a challenge.

  • @Bob_Burton
    @Bob_Burton 2 ปีที่แล้ว +1

    Ralph - I strongly suspect that most C++ programmers will not have used the auto keyword in a standard for loop many times, if at all, despite what you say at about 04:50 It is surely more normal to use a specific data type (usually int) for the loop variable even when byte would be big enough
    Using auto in this way is a neat way to introduce the concept but I feel that it would have been better to use a specific data type as the first example, then to introduce auto before moving on to range based loops

    • @lezbriddon
      @lezbriddon 2 ปีที่แล้ว

      had me puzzled, never seen it before, still not sure, never using it, never going to google it...... i'm not one for change or modern adventurous new ways.

    • @electricalychalanged4911
      @electricalychalanged4911 2 ปีที่แล้ว

      I have seen it when I tried out qt5 for programming a user interface. It had me puzzling about it until now so this is great.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      @UKHeliBob I deliberately slid this into the conversation as a "by the way" rather than making a big deal of it. I think it's a much better approach than using a specific data type, _especially_ when dealing with some esoteric data types:
      Instead of:
      std::map::iterator
      just use
      auto
      I know which one I'd rather use.

    • @Bob_Burton
      @Bob_Burton 2 ปีที่แล้ว

      @@RalphBacon You didn't slide it into the conversation. It was never mentioned, which I think is a shame. It would have made a perfect introduction to the use of auto in range based for loops and I think that you missed an opportunity

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Er... th-cam.com/video/qw7281Ab-QI/w-d-xo.html Admittedly it was a _mention_ but I did explain what it did.

  • @TanjoGalbi
    @TanjoGalbi 2 ปีที่แล้ว +1

    @5:42 What's that I see between the 2 for loops? Is that a Serial.println() call?!! After all your going on about using LibPrintf! Tut tut! 🤔😏

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Bad Ralph! Then again, it's only a dividing line 😜

  • @cirdiam1800
    @cirdiam1800 2 ปีที่แล้ว

    In the pass by value example car=“Hyundai” is trying to update the counter called car. Hence it doesn’t update the array. I think what you meant to do was cars[car]=“Hyundai” (I forget if it should be round or square brackets). This will then update the car’th item in array cars with Hyundai.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      In the pass-by-value example, 'car' is the variable containing the nth entry of the array. The compiler has done the hard work behind the scenes. We don't have access to any 'cnt' variable (the index) in that loop mechanism.

    • @cirdiam1800
      @cirdiam1800 2 ปีที่แล้ว

      @@RalphBacon interesting- I’ll have to take another look at that.

  • @AlbertDongler
    @AlbertDongler 2 ปีที่แล้ว

    Neat! Thanks for this :-)

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Glad you liked it!

  • @jcxtra
    @jcxtra 2 ปีที่แล้ว

    When I first saw the title, I thought it was gonna be using negative counting for loop iterations, as used in the manual Atmel released for more efficient code, but how could I have done so much on AVR micros and not know this? Now I don't need to remember how many entries are in an array, and if I combined the pass by reference with a const, it'd probably save memory but also keep it read only, wouldn't it? ... Still, the auto loop, that's nice. Thanks for sharing :)

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Yes, this is a very useful technique (once you know 😉), glad you found it useful. Passing by reference saves memory (no copying of variables) and the const would keep it read only. Listened well, you have, my young padawan.

  • @TuttleScott
    @TuttleScott 2 ปีที่แล้ว

    might not be something I use but it could come in handy to know when reading someone elses code

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Everyone can find a use for range-based loops, Scott. It will come up one day, you'll see.

  • @yogeshitaliya473
    @yogeshitaliya473 2 ปีที่แล้ว

    😍😍😍😍😍

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Yogesh, always first to the party!

  • @newburypi
    @newburypi 2 ปีที่แล้ว

    Great tip! Do you know when this was added to C++? I'm an ancient C user, only falling over to C++ in order to use OOP constructs. I could have well used this back in the day.

    • @newburypi
      @newburypi 2 ปีที่แล้ว

      Nvm. Released September 2011 in C++ 11. So... not available in the 1980s 🙂

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Yes, a C++11 thing. Improved since and updated in C++20.

  • @steveroberts1861
    @steveroberts1861 2 ปีที่แล้ว

    Interesting stuff. I do find it hard to follow without an index. I'll give it go.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      If you _need_ the index then you have to fall back to the long-hand method. But usually we just want the nth item in the list so this is much simpler.

  • @TheEmbeddedHobbyist
    @TheEmbeddedHobbyist 2 ปีที่แล้ว +1

    Nice video, I'm still old school and stick to "For" and "While" and "Do" loops.
    Also you kinda get stuck in the code style guides, and of course "Motor Industry Software Reliability Association" (MISRA) C and C++ coding standards.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      No, no, use the new feature of C++ 11 - the auto keyword can really work out all types of variable not just strings (char *) or integers but some strange ones too. Tuples, for instance.

    • @TheEmbeddedHobbyist
      @TheEmbeddedHobbyist 2 ปีที่แล้ว +1

      @@RalphBacon but Ralph, I'm stuck in the mindset of certified compliers, which take years to move or keep up with changes. Can you think of how much testing would have to be performed to prove that the compiler generated the code correctly everytime. Looked at one compiler once and it was £80k+ just for the documents ,the program was extra a lot extra. At least hardware was a lot easier unless you had an fpga to get certified.
      Funny that a microcontroller was classed as too complex to test so didn't need to, but an fpga was too complex to test so you had to code review its set up program code in the same way as for S\W. How much fun it all was , don't miss it at all :-)

    • @mikelopez9893
      @mikelopez9893 2 ปีที่แล้ว +2

      @@TheEmbeddedHobbyist Yes, Misra is still stuck in the 03 world as far as I know. However, Ralph's 'channel is aimed at hobbyists, especially Arduino, and I doubt any readers are writing safety critical systems on the Arduino platform. Just use certified compilers for your day job and whatever you want for hobby work. It is more fun that way!

    • @TheEmbeddedHobbyist
      @TheEmbeddedHobbyist 2 ปีที่แล้ว

      @@mikelopez9893 Mike i was just pointing out how your ex day job influenced your hobbies! With Auto in the loop statement you don't need the variable type but with the printf is this there a %auto i don't think so, I maybe wrong. So on one hand you don't need the variable type then on the other you do.

  • @yayser
    @yayser ปีที่แล้ว

    Thanks a lot for the great video. I have question, how do I define a matrix with variable rows but constant columns. What I mean is like, say I know that I will have 5 columns but the rows will be upto the user. So let's say I have a variable numOfApples = (a number set by user) and I will then have a matrix like M [numOfApples] [5] = {0} . I tried this but Arduino doesn't like it, so I couldn't do it.

    • @RalphBacon
      @RalphBacon  ปีที่แล้ว

      You can have multidimensional arrays.
      Whereas an array defined as myArray[6] is single dimensioned (and easy to understand), an array defined as myArray[3][6] gives you three "rows" each of six "columns" (if you want to view it like that).
      Incidentally, the first value (rows) must be defined at definition time, but the second value (columns) can be variable. But if you just mentally switch rows for columns, then you have your variable "rows" and fixed "columns". Yes, it hurts my head too.
      To initialise a multidimensional array, you can do this:
      int myArray [3][4] = {
      {1,2,3,4},
      {5,6,7,8},
      {9,0,1,2}
      };
      Finally, although we _declare_ rows & columns in the usual (human) way starting from 1, we must _address_ them in our code starting from [0].

    • @RalphBacon
      @RalphBacon  ปีที่แล้ว

      Worth reading: bit.ly/MultiDimArrays

    • @yayser
      @yayser ปีที่แล้ว

      @@RalphBacon Thank you for the quick answer Ralph. I got your point. It hurst as much as it makes sense. So you are saying, if I declare a multi.dim. array as Result [5] [whatever] then C would be happy to accept that definition. So if I declare whatever = 10 down below somewhere, the compiler would then take Result array as Result [5] [10] ?? Is that right?

  • @flemmingchristiansen2462
    @flemmingchristiansen2462 2 ปีที่แล้ว

    Ok- a " Like" and a comment.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Always welcomed, Flemming; I hope things are good with you.

  • @alexrossouw7702
    @alexrossouw7702 2 ปีที่แล้ว

    Looks cleaner, any performance penalties on microcontrollers worth worrying about?

    • @IanSlothieRolfe
      @IanSlothieRolfe 2 ปีที่แล้ว +1

      There shouldn't be any performance penalty because the compiler will actually generate code that actually does what the original count based loop. So technically it might take sightly longer to compile, but when uploaded to the arduino its actually doing the same thing.

    • @alexrossouw7702
      @alexrossouw7702 2 ปีที่แล้ว

      @@IanSlothieRolfe Okay thanx!

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      And in addition to what Ian says, it just looks so much more maintainable.

  • @hansdegroot652
    @hansdegroot652 2 ปีที่แล้ว

    Hi Ralph. sorry a bit off topic. but i lost my laptop. (or thew ssd drive failed) and i was using a terminal progam you were using in a video some time go. but i cannot rember the name. could you pls tell me the name? i really miss an extra terminal tool. thx

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Yes, it was the freeware (but donations gratefully received, I'm sure) CoolTerm and the link is here: freeware.the-meiers.org/ 😉

  • @sienervanrensburg6644
    @sienervanrensburg6644 2 ปีที่แล้ว +1

    No adds, no pop ups would make it more pleasant to view

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +4

      It would also make for a very poor return on investment. 😉

  • @Enigma758
    @Enigma758 2 ปีที่แล้ว +2

    "auto car" :)

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +2

      Completely unintentional, I assure you 😁

    • @thorntontarr2894
      @thorntontarr2894 2 ปีที่แล้ว +1

      WELL SPOTTED!

  • @willofirony
    @willofirony 2 ปีที่แล้ว

    Imagine a world before for next loops. For, in fact, there was such a world and it was long before the 'C' language. In these olden days, your for next loop would have been: cnt =; 0 while(cnt < 5) {printf("Car std loop %s
    ", cars[cnt]); ++cnt;}. So the powers that be came up with the for next loop. Why? Well, this was a very common pattern in programming and it was easier for the compilers if the initiation and the count and the comparison where all in the same place. Hence the for next loop. Programmers are human and like the rest of their species, they always think they can do better (in the big picture "better" is a very complex abstract; ask any polar bear). So they came up. after may decades, with the range based for loop. This is a good thing BUT don't be fooled into thinking you are generating less code. For (pun intended), the code generated by the compiler is the same for both for loops and, if written as I wrote it above, for the ancient while loop. I am not saying there is anything wrong with that; I will always use the range based for loop whenever I can. It is a thing of beauty and so easy to read, it avoids the "off by one" error so ubiquitous in software. Just don't be lulled into a false sense of security that "syntactic sugar" saves those valuable bytes of embedded resources.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      10 LET A=0
      20 do stuff here
      30 more stuff here
      40 A=A+1
      50 IF A < 10 THEN GOTO 20
      Looks fine to me. 🤣😅😫 What? What? 🙄

  • @Kosmonooit
    @Kosmonooit 2 ปีที่แล้ว

    "stacks" up ... lol .. I prefer the nuts & bolts approach

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว +1

      Use whichever method suits you best - but now you know about it!

    • @Kosmonooit
      @Kosmonooit 2 ปีที่แล้ว

      @@RalphBacon Indeed, thank you. Guess I have my assembler roots to blame.... also like literal code that is easy to read, albeit long winded.

  • @hansdegroot652
    @hansdegroot652 2 ปีที่แล้ว

    I am still waiting for perl for arduino.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      Don't you mean FORTRAN? Or CP/M maybe? 😜

  • @FredericRoussey
    @FredericRoussey 2 ปีที่แล้ว

    Your code is not safe. You put a local string "Mercedes" il your array that can not longer exist after the first loop.

    • @RalphBacon
      @RalphBacon  2 ปีที่แล้ว

      😕 ← that's me with a confused face. The string is copied into the array which is declared as a global so will survive (as will its contents). Try it out!

    • @stephenwong9723
      @stephenwong9723 2 ปีที่แล้ว +2

      I agree, it's actually a poor practice to write code this way, you overwrite the pointer to point to a local string, the original strings' memory are lost, and the local string is not guaranteed to remain when the function exists. Although in 8-bit microcontroller world, there is no memory protection / virtual memory stuff, so, the code works, but still not a clean way to code.

    • @emislive
      @emislive 2 ปีที่แล้ว +3

      ​@@RalphBacon a pointer to the "Mercedes" string literal is stored into the array. String literals are usually stored in program text with other constants, so pointers to them are valid for the life of the program. The issue is const-correctness: your array should be "const char *", as string literals should be constant since C++11. This was undefined/implementation defined, even in ancient C.

    • @fromgermany271
      @fromgermany271 2 ปีที่แล้ว +1

      @@RalphBacon
      No!
      The string itself is located in the current stack frame. The pointer to the string is stored in the (global) array. And the array is valid as until you leave the current stack frame and call another function. Like in Arduino, when main() calls loop() and then another function before it goes back to loop. It works for the printf() inside the „for“, because the stack frame of the calling function is still available.
      Also be aware, that avr-gcc does not place string literals into Flash. They are in (and use) RAM. You can also see this by the missing compiler complaint about pointing to a const with a non-const referring pointer. With „char *“, both the pointer variable and the pointed to variable are non-const.
      Unfortunately the original Arduino uses AVR8 chips. They are supported by a great compiler, but C++ does not use the AVR8 memory model of separate address spaces for ROM and RAM. That makes placing constants to ROM a thing the programmer has to take care about.
      Also, if you use Arduino „String“s instead of string literals, you are touching the „copy object“ area. You would need „auto &“ to avoid copying, but should add a const to avoid changes. That all has big impact on your generated machine code and your RAM-usage. Strings are on the heap, that might grow into your stack and suddenly you crash and the average Arduino programmer had no clue why.

    • @dozog
      @dozog 2 ปีที่แล้ว

      I think the use of "auto" is obfuscating what type the variable "car" is in each of your example loops.
      In every case the scope of "car" is local to within the loop.
      But it is sometimes an integer (you *say* it usually puts in an integer), sometimes a string, sometimes a pointer....
      Variable types are not trivial!