Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/16/08 17:11
Modified:
  04/16/08 17:17

Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#153527 - Stop now, before you make things worse!
Responding to: ???'s previous message
Russ said:
What is in 'byValue1' and 'byValue2' when this statement is executed?

Please explain what the statement does, and what you expect to find in 'byValue' after it has executed.

Chris said:
Since I was getting "can not add two pointers together", what I've done was assign byValue1 and byValue2 as a place holder for each array member 0 and 1 then then conconetate the two byValues together to "build" the device in hex.

So for example you type 2 in the edit box, its captured, converted to hex, stored in byValue1, type 3, its captured, converted to hex, stored in byValue2, and then "device" is the concanotation of the two byValues together, and that device "byte" is sent via I2C to communicated with the slave MPU, ADC, and EEPROM in hardware.

It was really just a way to get around the pointer errors that I wasn't sure how to get running properly.

Chris,

I don't mean to be offensive in any way, but it's evident from the code you posted earlier and from your answer above that you are not even remotely competent as a C/C++ programmer. Here's your code again, with some line numbers added for ease of reference:
 1  void CHC_controllerDlg::OnChangeDevice() 
 2  {
 3      UpdateData(TRUE);
 4      byte byReadData[2];
 5      for (int i = 0; i < 1; i++)
 6      {
 7          byReadData[0] = (byte)strtoul( m_strDeviceAddress, NULL, 16 )
 8          byReadData[0] = ("%x", byReadData[0]);
 9      }
10      
11      for (i = 0; i < 1; i++)
12      {
13          byReadData[1] = (byte)strtoul( m_strDeviceAddress, NULL, 16 )
14          byReadData[1] = ("%x", byReadData[1]);
15      }   
16      
17      byte byValue1 = byReadData[0];
18      byte byValue2 = byReadData[1];
19  
20      byte byValue = (byValue2) & (byValue1)  ;
21      
22      device = byValue;
23      m_strDeviceAddress.Empty();
24      m_strDeviceAddress.Format("0x", byValue1);
25      byte temp = (byte)strtoul( m_strDeviceAddress, NULL, 16 )
26      m_strDeviceAddress.FormatMessage("%x", byValue2); 
27  
28      allset |=0x8;
29      UpdateData(FALSE);
30  }
This code is flawed in multiple ways:
  1. The 'for' loops at line 5 and line 11 serve no purpose. Each of them runs only once and so there is no reason at all to have them.
  2. On lines 7 and 13, the parts on the right hand side of the assignment are identical. How do you expect them to read the two different input digits?
  3. The comma operator returns the value of its rightmost expression. That means that the statement at line 8 is equivalent to "byReadData[0] = byReadData[0];". In other words, it does nothing. Why is it there?
  4. Same thing for the statement at line 14.
  5. Your explanation of the statement at line 20 is totally wrong. In C/C++, & is the bitwise AND operator. It has nothing to do with concatenation.
  6. Even if & was a concatenation operator of some sort, the result of concatenating two bytes would not fit into the single-byte variable 'byValue'.
  7. The format string "0x" at line 24 doesn't contain a conversion specification for 'byValue'
  8. The variable 'temp', set at line 25, is never used.
  9. I'm not an expert on FormatMessage(), but I suspect that line 26 completely wipes out the effect of line 24.
Again, I don't mean to offend, but we're all grownups here and a blunt message sometimes calls for a blunt delivery. From all indications, you are just guessing at things that might work and then trying them without understanding what you are doing. I suggested earlier that you grab Prosise's book to learn about MFC. I now see that my suggestion was premature. You really need to grab a book on C and learn the language fundamentals before you do any more damage to this program you're messing with.

-- Russ



List of 18 messages in thread
TopicAuthorDate
C++, 8051, cleaning up a mess......need advise            01/01/70 00:00      
   Trying to convert after typing is too hard            01/01/70 00:00      
   Take a look here...            01/01/70 00:00      
      I gave that a run,            01/01/70 00:00      
   Maybe this will help            01/01/70 00:00      
      Thanks Russ, some thoughts            01/01/70 00:00      
         More thoughts            01/01/70 00:00      
            Your right, you know how it is            01/01/70 00:00      
      Is this it?            01/01/70 00:00      
         Well, no            01/01/70 00:00      
            Hopefully I can answer this correctly            01/01/70 00:00      
               Is Prosise onlin?            01/01/70 00:00      
                  Emailed link to you            01/01/70 00:00      
               Two more questions            01/01/70 00:00      
                  Placeholders for array data            01/01/70 00:00      
               Stop now, before you make things worse!            01/01/70 00:00      
                  No offense taken at all            01/01/70 00:00      
                     I got the link            01/01/70 00:00      

Back to Subject List