Structure pointer frustration [SOLVED]

Just in case: IDE 1.8.9, UNO, Win7 pro.

I've browsed the web and tried myriad combinations of parentheses, address of/dereference operators and cannot get the desired result - which is simply to see the contents of searchData printed three times in succession, with one intervening change.

Referring to the below sketch:

What I expect to see:

struct expected output.PNG

What actually comes out:

struct actual output.PNG

The sketch:

struct searchData
{
  int srchAddress;
  bool found;
} directVal;

struct searchData *foundAndAddr = &directVal;

void setup() {
  Serial.begin(115200);
  foundAndAddr->srchAddress = 5280;
  Serial.println(foundAndAddr->srchAddress);
  foundAndAddr->found = true;
  Serial.println(foundAndAddr->found);
 
  print_struct(foundAndAddr);
  alter_struct(foundAndAddr);
  print_struct(foundAndAddr);
}

void loop() {
}

void print_struct( struct searchData *ptr)
{
//  Serial.println(__FUNCTION__);
  Serial.print("address: ");
  Serial.println(ptr->srchAddress);
  Serial.print("found ");
  Serial.println(ptr->found);
  printf("-----------------------\n");
}

void alter_struct(struct searchData *change) {
  change->srchAddress = 1600;
  change->found = false;
}

As far as I can tell I've put this together correctly but obviously something's wrong.

What?

TIA

struct expected output.PNG

struct actual output.PNG

That's really weird because your code is correct. I get the correct results with your code (UNO compiler 1.6.9, Win7).

5280
1
address: 5280
found 1
address: 1600
found 0

You could try this slightly different version:

struct searchData
{
  int srchAddress;
  bool found;
} directVal,*foundAndAddr;

void setup() {
  foundAndAddr = &directVal;
  Serial.begin(115200);
  foundAndAddr->srchAddress = 5280;
  Serial.println(foundAndAddr->srchAddress);
  foundAndAddr->found = true;
  Serial.println(foundAndAddr->found);
 
  print_struct(foundAndAddr);
  alter_struct(foundAndAddr);
  print_struct(foundAndAddr);
}

void loop() {
}

void print_struct(searchData *ptr)
{
//  Serial.println(__FUNCTION__);
  Serial.print("address: ");
  Serial.println(ptr->srchAddress);
  Serial.print("found ");
  Serial.println(ptr->found);
  printf("-----------------------\n");
}

void alter_struct(searchData *change) {
  change->srchAddress = 1600;
  change->found = false;
}

Note that printf() won't work with the UNO compiler though (But correct with a DUE).

Your code works on a Pololu A-StarPB 20MHz, 1.8.10, Win7.

For your little example there is really no need for pounters or references,
if you move the functionality into member functions.
(your alter_struct functions is pretty inflexible anyway)

BTW why are you using a singned quantity as an address?

struct searchData {
  int srchAddress;
  bool found;
  void set(int adr, bool fnd) {
    srchAddress = adr;
    found = fnd;
  }
  void print() {
    Serial.print(F("address: "));
    Serial.println(srchAddress);
    Serial.print(F("found "));
    Serial.println(found);
    Serial.println(F("-----------------------"));
  }
} directVal;

void setup() {
  Serial.begin(115200);
  directVal.set(5280, true);
  directVal.print();
  directVal.set(1600, false);
  directVal.print();
}
void loop() {}
address: 5280
found 1
-----------------------
address: 1600
found 0
-----------------------

Changing the " printf("-----------------------\n");" to " Serial.print("-----------------------\n");" fixes the problem. Looks like the printf() call is crashing the sketch and a buffer-full of text is never seen.

Whandall:
Your code works on a Pololu A-StarPB 20MHz, 1.8.10, Win7.

Ok, two replies to that effect now. So it's not the code, however poor it may be.

Whandall:
BTW why are you using a singned quantity as an address?

An oversight. ::slight_smile:

johnwasser:
Changing the " printf("-----------------------\n");" to " Serial.print("-----------------------\n");" fixes the problem. Looks like the printf() call is crashing the sketch and a buffer-full of text is never seen.

The answer! Thank you! This has been torturing me for several days. There were a handful of previous incarnations of this basic layout, and none of them worked either. The printf() must have gotten in there from a webpage fragment copy/pasted in and I never noticed it. Not suspecting an innocent print statement I concentrated my efforts on the structure/addressing side of things.

Karma++

ard_newbie:
Note that printf() won't work with the UNO compiler though (But correct with a DUE).

I didn't pick this up 'til I'd replied to johnwasser. Thanks.

Karma++

Whandall:
(your alter_struct functions is pretty inflexible anyway)

Intentionally. That one was just to prove, or disprove, the ability to change the structure contents, with known data, through a pointer passed to a function.