Concatenate certain arrays elements into a int

Hi..

I have an array of 7 elements, wich contains some values combinations..

for example, I need to transform from 2nd to 4th into a 234 int.

char myarray[5] = {'1','2','3','4','5','6','7'};

int i = atoi(myarray);

printf("%d\n", i);

This way, it returns the int, but all array values...1234567

You're going to have some problems because Arduino's int data type can only count to 32,767, so you need it to be a long. Also, there is no printf() function for the Arduino. Tey this:

void setup() {

  char myarray[8] = {'1', '2', '3', '4', '5', '6', '7'};
  long val;

  Serial.begin(9600);

  myarray[7] = NULL;
  val = atol(myarray);

  Serial.println(val);

}

void loop() {
}

Hi... This will retrieve all elements from the array, right?

What I need is retrieve only from 2nd to the 4th elements only, so need to get the 234 integer.

Tks a lot anyway.

So, extract the three required characters into another four element array, terminate it, and use atoi.

Hello, there!

yes it worked perfect!

Learning how to get away from Strings and to finish, there is only one thing... maqybe you could help.

I have an integer

int myint=1450;

And need to put it inside the array 20,21,22, and 23 of my MyarrayVariable.

Any idea?

It's your turn to show some research and find out how to do it. You can use what we've shown here as a jump-off point. BTW, good idea to stay away from the String class.

econjack: Also, there is no printf() function for the Arduino.

Yes, there IS a printf for arduino, it's just disabled by default to conserve memory. There are several different ways to enable it, all very simple. Just Google "Arduino printf".

Regards, Ray L.

RayLivingston: Yes, there IS a printf for arduino, it's just disabled by default to conserve memory.

Aw, come on Ray... Do you think this helps a newbie or are you trying to prove something else? Do you really expect them to go in and enable the code? If not, what's the point?

econjack: Aw, come on Ray... Do you think this helps a newbie or are you trying to prove something else? Do you really expect them to go in and enable the code? If not, what's the point?

Almost as bad as getting pedantic on them about the difference between similar terms...

J/K bud. Couldn't resist that one.

Delta_G:
Almost as bad as getting pedantic on them about the difference between similar terms…

J/K bud. Couldn’t resist that one.

I understand, but I still maintain that there is a real difference between define and declare and understanding that difference makes it so much easier to teach certain other topics in C. It’s not that they aren’t similar, they are. But they really are sufficiently different that things like the meaning of extern is so much easier to explain if they understand the difference. Indeed, I don’t know how I would teach the meaning of extern without making the distinction. Pointer discussions can also be made more clear.

I realize I’ve painted a target on my back, but I sincerely believe it’s a very important distinction. It’s not meant to be pedantic.

econjack: It's not that they aren't similar, they are. But they really are sufficiently different

Not dissimilar to your own gross misinterpretation of the terms 'lvalue' and 'rvalue' which you continue to peddle here and in your book, despite numerous attempts to correct you.

RayLivingston: Yes, there IS a printf for arduino, it's just disabled by default to conserve memory. There are several different ways to enable it, all very simple. Just Google "Arduino printf".

Regards, Ray L.

Hi! Tks a lot! Just enabled it and its what I was looning for! Tks a lot!

Worked!

pYro_65: Not dissimilar to your own gross misinterpretation of the terms 'lvalue' and 'rvalue' which you continue to peddle here and in your book, despite numerous attempts to correct you.

Ok. How would you define lvalue and rvalue when teaching a group of students who know absolutely nothing about programming?

econjack: Ok. How would you define lvalue and rvalue when teaching a group of students who know absolutely nothing about programming?

It's very simple, per Microsoft: "An lvalue refers to an object that persists beyond a single expression." Anything that is not an lvalue is an rvalue.

Regards, Ray L.

RayLivingston: It's very simple, per Microsoft: "An lvalue refers to an object that persists beyond a single expression." Anything that is not an lvalue is an rvalue.

This isn't that much different that what I'm saying. although I've simplified it as much as possible, as is often done with an introductory-level course. On page 197 of K&R they say:

"An object is a named region of storage; a lvalue is an expression referring to the object."

Just a little further down, they use an assignment into variable j [the object] and say:

"That is, here j [the object] refers to the value stored at the memory location set aside for j..."

When first introducing the concept of a variable, I tell them to think of an lvalue as a memory address (i.e., "a named region of storage") and to think of an rvalue as what's stored at that address (i.e., "the value stored at the memory location"). K&R also state that data declarations are attribute lists without an lvalue (e.g., extern, function prototypes, etc.) while data definition are attribute lists with a known lvalue. Conceptually, I think this is a good way to introduce the concept to beginning students and it does make discussions about other C topics (extern, pointers, prototypes, etc.) much easier.

Obviously, we don't agree, so we'll just do the agree-to-disagree thing.

And back to my original point, I don't see how any of this pedantry helps the average HOBBYIST who is just trying to get his code to work. Even if he doesn't have the technically correct terms, as long as he understands what the lines of code do he should be in good shape at this level. If he wants to dive deep into the inner working then maybe those definitions (or are they declarations I don't know) become important. But for 99.999% of the people we see on this forum it just adds to their confusion. I've even started being careful to use the word "created" when talking about either just so @econjack doesn't swoop in and totally derail the thread to argue about something the OP wouldn't even begin to understand. So you "created" the variable and "gave it a value" and I avoid those terms entirely.