I need to declare a function that returns a string from an array of pointers .. I am programming a PIC and I am using as an interface an LCD .. With this code I have what I want:
// CONFIGURATION BITS
#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
#define _XTAL_FREQ 8000000
// DEFINING LCD PINS
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#include <xc.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
void main()
{
TRISD = 0x00; //PORTD as output for LCD
Lcd_Init();
Lcd_Clear();
while(1)
{
Lcd_Set_Cursor(1,1);
char *keypress;
int key = 2;
char *key2[] = {"3", "4", "5"};
keypress = key2[key];
Lcd_Write_String(keypress);
__delay_ms(50);
}
}
I obtain "5" .. That is what I want .. But when I try to make a function for that..
// CONFIGURATION BITS
#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
#define _XTAL_FREQ 8000000
// DEFINING LCD PINS
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#include <xc.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
char kbd_getc();
char kbd_getc(){
int key = 2;
char *key2[] = {"3", "4", "5"};
return key2[key];
}
void main()
{
TRISD = 0x00; //PORTD as output for LCD
Lcd_Init();
Lcd_Clear();
while(1)
{
Lcd_Set_Cursor(1,1);
char *keypress;
keypress = kbd_getc();
Lcd_Write_String(keypress);
__delay_ms(50);
}
}
I don't understand why do I get mm .. I am not sure if I am declaring the function incorrectly or I am doing something wrong with the pointers, thanks


return key2[key];, which comes from accidentally usingchar kbd_getc()rather than as intended:char* kbd_getc().const char*. They will be stored in flash.void main()is not standard-compliant.main()should return anint.