element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Problem with c/c++ program - pointers/references
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 5 replies
  • Subscribers 389 subscribers
  • Views 483 views
  • Users 0 members are here
  • c-c++ programming
Related

Problem with c/c++ program - pointers/references

Andrew J
Andrew J over 5 years ago

Here's a snippet of a program I've written:

 

int8_t extractParameters(struct scpi_token* command, bool parmsRequired, uint8_t minCount, uint8_t maxCount, struct scpi_token* param1, struct scpi_token* param2, struct scpi_token* param3) {
  struct scpi_token* args;
  args = command;
  while(args != NULL && args->type == 0) { // Ignore the parsed command token to leave args pointing at the first parameter
    args = args->next;
  }

  int8_t count = 0;
  while(args != NULL && args->type == 1) { // Then grab the actual parameters
    if (count == 0)
      param1 = args;
    if (count == 1) 
      param2 = args; 
    if (count == 3)
      param3 = args;
    count++;
    args = args->next;
  }
//  Serial.print(param2->value);

  // If paramaters aren't required and none passed, then ok
  if (count == 0 && !parmsRequired) 
    return count;


  if (count < minCount) { 
    scpi_error error;
    error.id = -109;
    error.description = "Missing Parameter Error; too few parameters passed";
    error.length = 50;
    scpi_queue_error(&ctx, error);
    return -1;
  }
  if (count > maxCount) {
    scpi_error error;
    error.id = -108;
    error.description = "Parameter not allowed; too many parameters passed";
    error.length = 49;
    scpi_queue_error(&ctx, error);
    return -1;    
  }
  return count;
}
scpi_error_t measureVoltage(struct scpi_parser_context* context, struct scpi_token* command){
//  struct scpi_token* args;
  struct scpi_token* expectedValue;
  struct scpi_token* resolution;
  float voltage;
  String unit;
  float divisor = 0.0;


  int8_t count = extractParameters(command, false, 2, 2, expectedValue, resolution, NULL);
  if (count != -1) {  
    if (count == 0 || (count == 2 && resolution->length == 1 && memcmp(resolution->value, "V", resolution->length) == 0)) {
      divisor = 1000.0;
      unit = "V";
    } else {
        if (count == 2 && (resolution->length == 2 && memcmp(resolution->value, "mV", resolution->length) == 0)) {
          divisor = 1.0;
          unit = "mV";
        } else {
            // SCPI ERROR        
            scpi_error error;
            error.id = -224;
            error.description = "Illegal Parameter Error;Invalid resolution";
            error.length = 42;
            scpi_queue_error(&ctx, error);
        }
    }
  }
  if (divisor > 0) {
    voltage = ( (rand() % (30000 + 1 - 0)) + 0) / divisor;
    Serial.print("Voltage is: ");
    Serial.print(voltage,3);
    Serial.println(unit);
  }
  scpi_free_tokens(command);
  return SCPI_SUCCESS;
}

 

It's actually a bit of SCPI processing code but what it does, is not relevant to my question, but here's the problem.  As the code stands above it doesn't work but if I uncomment this line:

//  Serial.print(param2->value);

 

in the first function under the while{} loop - line 19 - it does.  It compiles, but on line 53 I call the first function, expecting 'resolution' to have the value of 'param2' on return from the first function.  With the line uncommented, it does; with it commented it doesn't.

 

I'm assuming the compiler is tidying stuff up - perhaps it thinks param2 isn't used given that it isn't referenced in that function once set.  So, I further suspect that I'm not passing declaring the function correctly, or passing the parameters correctly.  I've scratched my head for a few hours now but I can't work out the issue - c/c++ is not a language I'm overly familiar with.

 

Can anyone help me work out what is going on?

 

Thanks

 

Andrew

  • Sign in to reply
  • Cancel

Top Replies

  • Fred27
    Fred27 over 5 years ago +4 verified
    You're not actually setting the value of resolution in your first function. This probably explains it better than I could: https://stackoverflow.com/questions/9459691/how-to-change-value-of-variable-passed…
  • Andrew J
    Andrew J over 5 years ago in reply to Fred27 +3
    Thanks Fred, that's seems to have got it. I'd tried a multitude of variations of passing by reference which failed to compile. The 'only' thing that worked was when I had that Serial.print line in but…
  • Fred27
    Fred27 over 5 years ago in reply to Andrew J +1
    Pointers are a necessary evil in C. They seem simple but always cause trouble. I think I get them, but still get caught out as I don't do C often enough.
Parents
  • Fred27
    0 Fred27 over 5 years ago

    You're not actually setting the value of resolution in your first function. This probably explains it better than I could:

    https://stackoverflow.com/questions/9459691/how-to-change-value-of-variable-passed-as-argument

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Andrew J
    0 Andrew J over 5 years ago in reply to Fred27

    Thanks Fred, that's seems to have got it.  I'd tried a multitude of variations of passing by reference which failed to compile.  The 'only' thing that worked was when I had that Serial.print line in but I think that is possibly just co-incidence with memory.  Because just prior to seeing this it stopped working with that line!!

     

    I had to pass a pointer to a pointer!  Man....

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Fred27
    0 Fred27 over 5 years ago in reply to Andrew J

    Pointers are a necessary evil in C. They seem simple but always cause trouble. I think I get them, but still get caught out as I don't do C often enough.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Fred27
    0 Fred27 over 5 years ago in reply to Andrew J

    Pointers are a necessary evil in C. They seem simple but always cause trouble. I think I get them, but still get caught out as I don't do C often enough.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • Andrew J
    0 Andrew J over 5 years ago in reply to Fred27

    I'm the same.  I understand the concepts, I understand pass-by-value and pass-by-reference but I just don't do it often enough and then I miss the nuances!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neilk
    0 neilk over 5 years ago in reply to Fred27

    I don't use pointers often enough. I always have to refer back to some code that uses pointers, in order to understand what to do, all over again!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube