[Solved] Printng pointer address

Hi,

Why line:
Serial.println(&number);
produces an error when compiling IDE arduino,

and the line compiles correctly:
Serial.println(int(&number));

if the address of a memory is int .

In C++ (W3schools.com) it works without having to qualify the
variable as int?

Thanks everyone for the help.

void setup() {
  Serial.begin(115200);
  delay(10);
  String number = "Pizza";
  
  Serial.println(int(&number));
  Serial.println(&number);
}

void loop() {
}

W3Schools C++

string food = "Pizza";  // A food variable of type string
string* ptr = &food;    // A pointer variable, with the name ptr, that stores the address of food

cout << &food << "\n";

C/C++ on Arduino takes on a little different flavor because of the way the IDE does pre-processing of the .ino files and automatic function declarations.

A better place (examples) to learn about Arduino and pointers...

The Basics of C on an Arduino Part 3 Pointers and Arrays (digikey.com)

Hi,
thanks for your answer.

PS:
Why does it give error with arduino IDE does not give error with a C++ compiler?

I get it, but while you can write "sketches" in C++, Arduino abstracts a lots of concepts:
Arduino Reference - Arduino Reference

Once you learn C++ you can apply it to Arduino rather easily, but if you know Arduino you do not know C/C++.

A rather bad association: if you know BASIC you do not know assembler, but if you know assembler, you know BASIC.

Tks

print() is overloaded to handle different types. It simply lacks an overload to print pointers to an int.

size_t print(const __FlashStringHelper *);
    size_t print(const String &);
    size_t print(const char[]);
    size_t print(char);
    size_t print(unsigned char, int = DEC);
    size_t print(int, int = DEC);
    size_t print(unsigned int, int = DEC);
    size_t print(long, int = DEC);
    size_t print(unsigned long, int = DEC);
    size_t print(double, int = 2);
    size_t print(const Printable&);

    size_t println(const __FlashStringHelper *);
    size_t println(const String &s);
    size_t println(const char[]);
    size_t println(char);
    size_t println(unsigned char, int = DEC);
    size_t println(int, int = DEC);
    size_t println(unsigned int, int = DEC);
    size_t println(long, int = DEC);
    size_t println(unsigned long, int = DEC);
    size_t println(double, int = 2);
    size_t println(const Printable&);
    size_t println(void);
3 Likes

As long as you're learning C++ ..... the preferred "C++ way" would be:

 Serial.println(reinterpret_cast<intptr_t>(&number));
1 Like

Hi,
I'm a young man born in the late 40's trying to improve the knowledge of C++ and "C Arduino".

Thank you all for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.