Notifications
Clear all

Help with c++ pointers

45 Posts
7 Users
5 Likes
2,168 Views
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
Topic starter  

I was playing around with code to print out binary digits of a list of 8 unsigned bytes,
Using two nested loops seemed to work ok.

 

#include <iostream>

using namespace std;

uint message[8] = {0x88,0x33,0x11,0x66,0x22,0x77,0x44,0x99};
uint digit;

int main()
{
    for (int j=0;j<8;j++){ 
        digit = message[j];
        for (int i = 8; i >= 0; i--){
            cout << ((digit >> i) & 1);
        }
        cout << "\n";
    }

    return 0;
}




 

 

But when I tried this example using a pointer, (based on some code in a recent post on Swarm Robotics).

//#include <iostream>

using namespace std;


uint8_t message[8] = {0x88,0x33,0x11,0x66,0x22,0x77,0x44,0x99};  
uint8_t msg_len = 0; 
uint64_t *ip;  
uint8_t digit;

int main()
{
    ip = &message;

    for (int y = 63; y >= 0; y=y-1) {
        digit = (*ip >> y) & 0b0001;  // access the value using the pointer
        cout << digit;
    }
    cout << "\n";

    return 0;
}

 

I got this error?

main.cpp: In function ‘int main()’:
main.cpp:21:10: error: cannot convert ‘uint8_t (*)[8]’ {aka ‘unsigned char (*)[8]’} to ‘uint64_t*’ {aka ‘long unsigned int*’} in assignment
21 | ip = &message;
| ^~~~~~~~
| |
| uint8_t (*)[8] {aka unsigned char (*)[8]}

It has been a while since I did any programming c++  (except with Arduino IDE).

 


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

Posted by: @robotbuilder

 

But when I tried this example using a pointer, (based on some code in a recent post on Swarm Robotics).

I got this error?

main.cpp: In function ‘int main()’:
main.cpp:21:10: error: cannot convert ‘uint8_t (*)[8]’ {aka ‘unsigned char (*)[8]’} to ‘uint64_t*’ {aka ‘long unsigned int*’} in assignment
21 | ip = &message;
| ^~~~~~~~
| |
| uint8_t (*)[8] {aka unsigned char (*)[8]}

It has been a while since I did any programming c++  (except with Arduino IDE).

It's too big a jump for the compiler doing that assignment, you'll need to make an explicit cast to override the compiler's sense of modesty so change that line to ...

ip = (uint64_t*)message;

It looks like cout doesn't have a specific overridden method for uint8_t, so change the definition of digit back to uint as it was in the first version.

Delete the cout << "\n" as well.

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
Topic starter  

   
ReplyQuote
ron bentley
(@ronbentley1)
Member
Joined: 2 years ago
Posts: 385
 

@robotbuilder @will

Hi, I converted your code to a standard arduino c++ structure and it worked as expected with will's data typing suggestion. The message preset values were printed correctly. Your for loop logic is fine.

I have previously posted an article that defines the standard bit manipulation macros for 64 bit variables.

If you think this might be helpful then let me know and I will find and post the link.

Regards

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
frogandtoad reacted
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
Topic starter  

@ronbentley1

@will

ip is pointing at a 64 bit register.

The y variable is used as the current bit number to extract.

The code works now I have converted the digit value to a ascii character.

 

#include <iostream>

using namespace std;

uint8_t message[8] = {0x88,0x33,0x11,0x66,0x22,0x77,0x44,0x99};  
uint8_t msg_len = 0; 
uint64_t *ip;  
uint8_t digit;

int main()
{
   ip = (uint64_t*)message;

    for (int y = 63; y >= 0; y=y-1) {
        digit = (*ip >> y) & 0b0001;  // access the value using the pointer
        cout << char(digit | 0x30);
    }


    return 0;
}

 

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

Posted by: @robotbuilder

@will

Implemented your suggestions but it didn't print anything out but did compile and printed out the y but not the digit.

It didn't print digit because you didn't make the change I suggested to switch the datatype to uint. 

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@robotbuilder You don't have the Arduino IDE?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
THRandell
(@thrandell)
Brain Donor
Joined: 3 years ago
Posts: 224
 

@robotbuilder

@ronbentley1

@will

For what it's worth, I'm running my builds on a Macbook and the compilier throws this warning:

 

Silver:build trandell$ make

[  2%] Built target bs2_default

[  4%] Built target bs2_default_padded_checksummed_asm

[  6%] Performing build step for 'ELF2UF2Build'

[100%] Built target elf2uf2

[  7%] No install step for 'ELF2UF2Build'

[  8%] Completed 'ELF2UF2Build'

[ 14%] Built target ELF2UF2Build

Consolidate compiler generated dependencies of target test

[ 15%] Building C object CMakeFiles/test.dir/test.c.obj

/Users/trandell/pico/test/test.c: In function 'main':

/Users/trandell/pico/test/test.c:424:8: warning: assignment to 'uint64_t *' {aka 'long long unsigned int *'} from incompatible pointer type 'uint8_t (*)[8]' {aka 'unsigned char (*)[8]'} [-Wincompatible-pointer-types]

  424 |     ip = &message;            // store address of message array in pointer variable

      |        ^

[ 16%] Linking CXX executable test.elf

[100%] Built target test

 

Tom

To err is human.
To really foul up, use a computer.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@thrandell My C is very dated, but isn't that just in need of a cast?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@thrandell @will fixed that in post https://forum.dronebotworkshop.com/postid/36046/ as well as one other minor issue.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
Topic starter  

@will 

Changing digit to uint did work.  I don't see why the others did not. I wasn't sure about uint which doesn't indicate its length which probably can be up to 64 bits on a 64 bit bus computer?

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

Posted by: @robotbuilder

@will 

Changing digit to uint did work.  I don't see why the others did not. I wasn't sure about uint which doesn't indicate its length which probably can be up to 64 bits on a 64 bit bus computer?

Here's what I did (using CodeRunner on an iMac) with the result

Screen Shot 2022 12 09 at 11.04.17 AM

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
Topic starter  

@will 

Yes I did the same.  However you cant make the message array uint because they will be longer than 8 bits resulting in an incorrect result being printed.

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

Posted by: @robotbuilder

@will 

Yes I did the same.  However you cant make the message array uint because they will be longer than 8 bits resulting in an incorrect result being printed.

Read it again, I only changed digit to uint, exactly the same as you had it in your first (working) example before you changed to using an 8 byte value type. No one is suggesting changing the input message type.

It appears that iostream doesn't have an overloaded method for uint8_t so I just changed the type of digit to a data type for which upstream DOES have a method.

The other change was merely to apply a cast to manually force the conversion of data type.

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
Topic starter  

@zander 

Sure but I use that to write code to download to the Arduino board in order to run it.

The code in question is actually for the RPi Pico that @thrandell is using in his swarm bots.

I was just trying to get it clear in my head how the code worked that he used to dump the 64 bits.

 


   
frogandtoad reacted
ReplyQuote
Page 1 / 3