change macro to valiable

Hello,

I am trying to edit existing header file code of ethernet library,
such that hostname can be set in variable/string.


library / header file:
in dhcp.h
 
#define HOST_NAME "MYBOARD1" 


in dhcp.cpp
1
2
3
4
#include network.h
 buffer[16] = hostName;
 buffer[17] = strlen(HOST_NAME); // length of hostname (modified)
 strcpy((char*)&(buffer[18]), HOST_NAME);


my file:

in myfile.cpp
 
 #include network.h 


I want to assign hostname in my cpp file.
Eg: HOST_NAME = "Board1";

Not able to figure how to do this.

Thank You
Sudheer
const char *const HOST_NAME = "Board1";
Assuming above code need to be used in my source code.
I added line in myfile.cpp

onst char *const HOST_NAME = "Board1";


in Dhcp.h

 
#define HOST_NAME "MYBOARD1" 


Error:
1
2
3
4
5
Dhcp.h:47:19: error: expected initializer before string constant
 #define HOST_NAME "MYBOARD1"
                   ^
DhcpAddressPrinter.ino:25:19: note: in expansion of macro 'HOST_NAME'
Error compiling.



In Dhcp.h

1
2
#define HOST_NAME "MYBOARD1"
#undef HOST_NAME 



in myfile.cpp

const char *const HOST_NAME = "Board1";

I get following error from dhcp.pp

1
2
'HOST_NAME' was not declared in this scope
     buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address 


Just replace #define HOST_NAME "MYBOARD1" with const char *const HOST_NAME = "Board1"; (in your header).

#define should be used in rare circumstances (like include guards) only. Otherwise you might get strange errors.
I figured that out, its working when I change id dhcp.h file.

but this will affect all projects as dhcp.h and dhcp.ccp are library files in arduino.
this library is used by various other projects.

I need to store values for host name in program, so each project has different host name depending on boards

I need to assign value in project file, not library files.



One solution would be making a function:

Dhcp.h
const char *get_host_name();

myfile.cpp
1
2
3
4
const char *get_host_name()
{
  return "Board1";
}

I was able to achive results.

in dhcp.h

1
2
//#define HOST_NAME "WIZnet"
extern const char  HOST_NAME[10] ;


in dhcp.cpp

1
2
3
buffer[16] = hostName;
buffer[17] = strlen(HOST_NAME); // length of hostname 
strcpy((char*)&(buffer[18]), HOST_NAME);
Topic archived. No new replies allowed.