0

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

    }
}

enter image description here

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

    }
}

enter image description here

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

3
  • 2
    The root of the problem here is that the infamous PIC compiler "MPLAB", or whatever that crap is called nowadays, is bad. A working C compiler would have pointed out the language violation at return key2[key];, which comes from accidentally using char kbd_getc() rather than as intended: char* kbd_getc(). Commented Jul 5, 2018 at 6:30
  • As a side note, pointers to string literals should be declared as const char*. They will be stored in flash. Commented Jul 5, 2018 at 6:32
  • As another side note, void main() is not standard-compliant. main() should return an int. Commented Jul 5, 2018 at 7:43

3 Answers 3

2

Your function returns char but you return a char *.

Change the return type to char *

And up your compiler warning to catch those things.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know enough about the specifics of your application, I just told you what is wrong with your C. You (I expect) need to change the return to char *. Which did you do?
I change: char kbd_getc(){ to: char * kbd_getc(){ Now it works, thanks
There is no need to "up" compiler warnings, the code contains a constraint violation and a conforming compiler must give a diagnostic message. So the solution is rather to change to a standard C compiler.
1
    char *key2[] = {"3", "4", "5"}; 
    return key2[key];

Here, key2 is an array of char pointers and key2[key] is pointer at index key of array key2 which is pointing to first character of a string.
Now, look at the return type of function kbd_getc():

char kbd_getc(){
^^^^

Its a char type. That means, you are returning a string from function which is suppose to return a character.
If I write a simple program to check your kbd_getc(), I am getting following warning message on statement return key2[key]; -

warning: incompatible pointer to integer conversion returning 'char *' from a function with result type 'char';

If I change the return type to char *

char * kbd_getc(){

the warning message disappear and I get the desired output:

$ ./a.out 
5

Comments

-2

Just try this,

char kbd_getc(){

int key = 2;
char *key2[] = {"3", "4", "5"}; 
char a = key2[key];
return a ;

}

I think That u r returning array.

You need to return a char.

Thank you

2 Comments

Same problem, same bug. Your code returns a char * from a char function. I think the function should return char *
Ya , i think that key pressed is char.. plz change that to char* , if then change the method name to char *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.