Pages: [1]   Go Down
Author Topic: char to const char  (Read 1392 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i'm trying to use a Real Time Clock (RTC) and a dmd. I am resieving that data from the RTC and trying to put in into a variable that the DMD will understand. it only allows const chars and when i try to make one it comes up with an error (uninitalized const 'abc' (that is the name of the variable). how do i fix this error thanks :-)
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1832
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the prototype (call signature) of the DMD function you are trying to call?

If it is something like:

Code:
void do_something (const char *);

you would just pass in a normal array or pointer to a character, and the compiler will automatically convert from char */char [] to const char *.  What a const pointer means in terms of a function prototype, is the compiler can assume that the memory that the pointer points to is not modified in the function.

For example:

Code:
extern void do_something (const char *);
extern char array[10];

void test (char *p)
{
  do_something (p);
  do_something ("string");
  do_something (array);
}

When you have a const declaration of a scalar or array, you must initialize it at that point, and the compiler is free for instance to put the variable into read-only memory.

Now, going the other way (from a const pointer to normal pointer) is a type error, and you will need a suitable cast.  Even if you do convert the pointer, it is illegal to actually modify what the pointer points to.
« Last Edit: January 03, 2013, 07:41:18 am by MichaelMeissner » Logged

Pages: [1]   Go Up
Jump to: