0

I have this simple lcd xc8 header file:

#ifndef XC_PANTALLACWS_H
#define XC_PANTALLACWS_H

#include "lcd.h"

void pantallaCWS (const char stringProyecto){
    const char stringProyecto[16] = "__proyNombre____";
    Lcd_Init();
    Lcd_Out(1, 0, stringProyecto);
}

#endif

What I would like to do is that, if an argument is not given in the function, put one by default.

is this possible?

1 Answers1

1

First, I think there is an error in your argument declaration as it is only one char but you want a string.

Then the trick is to check for a null argument, and if so, provide your default argument in the function, for example:

void pantallaCWS (const char *stringProyecto){
    const char *myString;
    if (stringProyecto==0)
        myString= "__proyNombre____";
    else
        myString= stringProyecto;
    Lcd_Init();
    Lcd_Out(1, 0, myString);
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    I don't think this matches the requirement: *"if an argument is not given in the function"* – Jonathon Reinhart May 02 '19 at 11:39
  • This work for me. I´ll call the function in two ways: 1: pantallaCWS (null) or 2: pantallaCWS (anyString) ..... thanksss – covraworks May 02 '19 at 17:45
  • @JonathonReinhart, the alternative solution is to work with varargs, but I don't think the OP is up to that. (Besides, his declaration doesn't allow it.) – Paul Ogilvie May 02 '19 at 18:13