CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Accessing Array

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Sid2286



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

Accessing Array
PostPosted: Thu Jan 02, 2014 4:49 am     Reply with quote

I want to access an array and display on the LCD but getting the following error.
ERROR: previous identifier must be a pointer

how do I change my following code:?
Code:

#include <18F4520.h>
#device ICD=TRUE ADC=10
#fuses HS,NOLVP      //,WDT128
#use delay (clock=8MHZ)
#include <flex_lcd.c>
#include <button.c>


const char* my_lib[]={"abc", "xyz", "123","ABCD"};

int8 B7 = 0; // Enter Button->B0
int8 B5 = 0;  //right shift Button->B5
int8 B6 = 0; //increment Button->B6

int my_lib()
{
   int* x;
   while(true)
   {
      if(button(PIN_B6, 0, 50, 10, B6, 1))
      break;
   }
   while(true)
   {
      printf(lcd_putc,"\n  %c ",my_lib[x]);
      delay_ms(1000);
      if(button(PIN_B6, 0, 50, 10, B6, 1))      //Increment value
      x++;
      if(x==3)
      x=0;
      if(button(PIN_B7, 0, 50, 10, B7, 1)) // Enter Pressed Return Value
      {
          return x;
          delay_ms(500);
      }
   }
}

void main()
{
   int x;
   PORT_B_PULLUPS(true);
   while(True)
   {
      if(button(PIN_B7, 0, 50, 10, B7, 1))
      {                 
        printf(lcd_putc," \fUnits\nto Display");
        delay_ms(1000);
        x =my_lib();
      }
      printf(lcd_putc,"\n  %c ",my_lib[x]);
      delay_ms(1000);
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Thu Jan 02, 2014 5:51 am     Reply with quote

Several things worthy of comment. Some CCS specific, and other just 'wrong'....

First, either get rid of the const keyword, or do a search here on 'pointer to constant'. In the PIC, ROM, and RAM are completely separate memory spaces. Hence a standard 'pointer', which points to RAM, can't be used to access a 'const' (stored in ROM) variable. There are ways of doing this (#DEVICE PASS_STRINGS=IN_RAM), or using the 'rom' construct, rather than 'const', which adds extra code to allow pointers to be constructed.

Then it is really bad programming practice to use the same name for a function and a variable. Avoid this. This is probably what actually gives the error.

Then though the basic construction is wrong. mylib[x] is a pointer to an array of characters, not a character, which '%c' expects....

Then you declare a pointer 'x', but nowhere do you initialise it. It could be pointing anywhere!. Then you declare x as a pointer, but the function returns an integer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group