#define array of pointers

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
daylosh
Posts: 10
Joined: 17 Jul 2008 16:43

#define array of pointers

#1 Post by daylosh » 08 Oct 2009 20:27

Hi all, im having trouble defining an array of pointers. I want to create a header file template for holding text in an array and then simply place an #include in my main file. Something like below;

Code: Select all


//HEADER FILE

#define _MSG_1   "Message 1"
#define _MSG_2   "Message 2"
#define _MSG_3   "Message 3"

#define messages  char*[] = {_MSG_1,_MSG_2_MSG_3}


Code: Select all


//MAIN FILE

#include "messages.h"

void WriteMessage(char *msg){

   while(*msg) TXREG = *msg++;

}


WriteMessage(messages[1]);


My test program uses routines similar to above for outputting text and does work it is only the

#define messages char*[] = {_MSG_1,_MSG_2_MSG_3}

from the header file which i seem to have a problem with (nothing is sent).

If i use

char *messages[] = {_MSG_1,_MSG_2_MSG_3};

within the main file i have no problems.

Any help would be most appriciated.

thank you

day

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#2 Post by anikolic » 09 Oct 2009 15:13

Hi,
The best way to check if your #define statements are going to be correctly processed prior to compilation, you should always refer to generated .cp file in your project folder. Regarding your code:

Code: Select all

#define _MSG_1   "Message 1"
#define _MSG_2   "Message 2"
#define _MSG_3   "Message 3"
#define messages  char*[] = {_MSG_1,_MSG_2, _MSG_3} 

WriteMessage(messages[1]); 
This last command line creates this equivalent output:

Code: Select all

WriteMessage( char*[] = { "Message 1" , "Message 2" , "Message 3" } [1]);
As you can see, this is clearly a faulty syntax and cannot be compiled, and therefore cannot work as intended. Always have in mind that preprocessor directive such as #define A B simply replaces any instances of A with B, and nothing more. So do some more adjustments and try again. If you have problems with this, I suggest you should use your alternative way with variable, which already works fine:

Code: Select all

char *messages[] = {_MSG_1, _MSG_2, _MSG_3}; 
Best regards,
Aleksandar
Web Department Manager

daylosh
Posts: 10
Joined: 17 Jul 2008 16:43

SOLVED

#3 Post by daylosh » 09 Oct 2009 22:07

Thanks Aleksandar for your quick response, i have since altered my code to declare my array of pointers in a seperate c file along with a function i call to manage the strings in the manner i want.

Thanks again

Day

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”