Go Down

Topic: char to const char (Read 1 time) previous topic - next topic

hamptonni

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 :-)

MichaelMeissner

#1
Jan 03, 2013, 01:38 pm Last Edit: Jan 03, 2013, 01:41 pm by MichaelMeissner Reason: 1
What is the prototype (call signature) of the DMD function you are trying to call?

If it is something like:

Code: [Select]

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: [Select]

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.

Go Up