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 support@ccsinfo.com

C structure don't work

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



Joined: 27 Jan 2005
Posts: 4
Location: Milano Italy

View user's profile Send private message Send e-mail

C structure don't work
PostPosted: Thu Jan 27, 2005 8:53 am     Reply with quote

Hallo
excuse me for bad english.
Why

#include <16F876.h>
#include <stdio.h>

int maxy;
void (*fzptr)(int evento);
void menuprincipale(int evento);


struct SECONDO
{
int indice;
char nome_item[20];
void (*fogliaptr)(int i);
};

Product an error in "void (*fogliaptr)(int i);"? The error is "Unknown type" and it's referred ti void word.
The same structure work correctly on borlandc 2.0
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 27, 2005 9:43 am     Reply with quote

Read here

http://www.ccsinfo.com/forum/viewtopic.php?t=20963&highlight=pointer+function

and here

http://www.ccsinfo.com/forum/viewtopic.php?t=20416&start=15
Guest








PostPosted: Fri Jan 28, 2005 7:02 am     Reply with quote

I saw the links and the 3 solution, what is the best? Naturally I must create an application menu too!
The first appear not very good, the 2th it's very shorter and what is UINT?
And in 3th solution what is "const rom struct _cmd_t"? what mean const rom char?

Excuse me for idot question, Nino
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Jan 28, 2005 7:24 am     Reply with quote

UINT is an unsigned int. Note that several of these examples were not written for the CCS compiler. "const rom" was for the C18 compiler (Microchip). CCS doesn't allow pointers to constants and there handling of pointers to functions isn't that great either. You are going to have to use a switch statement or something like this:
Code:
/*****************************************************************************
    This is an example of how to create a const array of function
    pointers and call them.  It is a bit of a trick to fool the compiler
    but seems to work for the cases that I tested. 
******************************************************************************/
#if defined(__PCM__)
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
 
#elif defined(__PCH__)
#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
 

// Here is where we define the start of menu wrappers.
// !!!! Note that it (all wrapper addresses) must be within
// 1024 bytes of the caller
#define MENU_START_ADDRESS 0x200

// This is how many bytes the wrappers take up.  You might have to
// set this value high and then determine how many the wrappers actually
// take up.  Note that a BRA is usually used for small programs.  However
// on a larger program a CALL instruction might be used which will
// require 2 additional bytes
#define FUNC_SIZE          8

// This is where we define our menu wrapper locations
#define MENU1_ADDRESS      (MENU_START_ADDRESS)
#define MENU2_ADDRESS      (MENU1_ADDRESS+FUNC_SIZE)
#define MENU3_ADDRESS      (MENU2_ADDRESS+FUNC_SIZE)
#define MENU4_ADDRESS      (MENU3_ADDRESS+FUNC_SIZE)

// Typedef for our function pointer
typedef int (*pFunc)(void);

// Typedef for our menu items
typedef struct {
   char name[16];   
   int16 ptrFunc;   // Just tricking the compiler here
} MenuItem;

// Here's that constant array for the menus
const MenuItem Menu[3]={{"String1", MENU1_ADDRESS},
                        {"String2", MENU2_ADDRESS},
                        {"String3", MENU3_ADDRESS}};
 


// These are the real Menus that we want to call
#org 0x2000,0x2100  // org'ed this just to show a CALL insteal of BRA
int RealMenuFunc1(void)
{
  int i = 1;
  return(i);
}

int RealMenuFunc2(void)
{
  int i = 2;
  return(i);
}

int RealMenuFunc3(void)
{
  int i = 3;
  return(i);
}


// These are wrapper menus to make setting the addresses easier.
// It also appears CCS is using a RCALL instruction which limits
// how far the menus can be away from the caller.
#org MENU1_ADDRESS, MENU1_ADDRESS + FUNC_SIZE - 1
int WrapperMenu1(void)
{
  return(RealMenuFunc1());
}

#org MENU2_ADDRESS, MENU2_ADDRESS + FUNC_SIZE - 1
int WrapperMenu2(void)
{
  return(RealMenuFunc2());
}

#org MENU3_ADDRESS, MENU3_ADDRESS + FUNC_SIZE - 1
int WrapperMenu3(void)
{
  return(RealMenuFunc3());
}


void main()
{
  int i;
  pFunc ptrtofunc;

  // Dummy initialization to keep the compiler from complaining
  ptrtofunc = WrapperMenu1;

  // Assign the address to the function pointer
  ptrtofunc = (pFunc)Menu[0].ptrFunc;
  // Call the function
  i=(*ptrtofunc)();

  ptrtofunc = (pFunc)Menu[1].ptrFunc;
  i=(*ptrtofunc)();
  ptrtofunc = (pFunc)Menu[2].ptrFunc;
  i=(*ptrtofunc)();


  while( TRUE )
  {
  }
}
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