• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

call to non-constexpr

Q&A Forum › Category: ESP32 › call to non-constexpr
0 Vote Up Vote Down
Jean Pierre Daviau asked 5 years ago

Hi,
 
I tried to create my own function but fell on this problem:
 
OTAWebUpdaterWebPage3.ino:319:19: error: call to non-constexpr function ‘int mystrcmp(char*, char*)’


char toswitch[] = {"Page disponible pour 2 heures entre 16 et 18:00 heures.\nDonc peut être consultée jusqu'à 19:00 heures"};
//const int ii = (const)strlen(toswitch);
RTC_DATA_ATTR char awakin[102];

int mystrcmp(char *s, char *t)
{
for (; *s == *t; s++, t++)
if (*s == '\0')
return 0;
return (*s - *t);
}

Regards,

JPD
Question Tags: call to, Error, non-constexpr
2 Answers
0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

Hi,
 
I red a bit on it and I wonder how I could force the compiler not to consider it   a constexp  function.

By adding a useless  analogRead() or else tasks  to do.

Regards,
 
 
JPDaviau

1 Vote Up Vote Down
Steve Mercer answered 5 years ago

Your function accepts a volatile pointer to a character. There is an excellent response to a Stack Overflow question at https://arduino.stackexchange.com/questions/37718/how-to-pass-a-string-pointer-to-a-function which I’ll reproduce below:
 
You can pass parameters to functions by reference by value or ‘by pointers’.
Passing a parameter by value means that the function has a separate copy of the parameter so this is more expensive in terms of memory. It also means that any changes you make to the parameter inside the function are not reflected outside the function, they are two separate variables after all.
Passing by reference in C/C++ is done by adding a & after the parameter’s type, (int& byRef). This means that you are using the same variable inside and outside of the function. If you want to prevent the function changing the value then you set the parameter to be a constant reference. const int& constByRef
There is a third method which is passing a pointer. This is very useful for parameters that are a few bytes in size, i.e. strings and structures char* ptr. When you pass a pointer it is passed by value, you can modify the pointer or the data it points to. Since a pointer is usually only the word size of the processor it is only as expensive as passing an int parameter.
When you pass a const char* cPtr you can not modify the value of the pointer, but you can modify the value of the data pointed to. This type of parameter will let you pass in string literals.
To make you code cleared always write functions to accept constant parameters. If the parameter is larger than a word pass it by reference unless its an array (including char*) in which case pass it by pointer. If in doubt it should be a constant reference to start with. Sometimes embedded constraints affect these rules.
 
Also, take a look at this article.

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.