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 programming pointer explanation needed

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



Joined: 18 Oct 2004
Posts: 21

View user's profile Send private message

C programming pointer explanation needed
PostPosted: Fri Aug 05, 2005 5:48 am     Reply with quote

I encountered this prblem in the book C How to program, and too bad, there is no clear explanation how this happened.

Here is the code:

Code:
#include <stdio.h>


int main()
{

char sentence[80];
void reverse(const char * const);

printf("Enter a line of text:\n");
gets(sentence);

printf("\nThe line printed backwards is:\n");
reverse(sentence);

return 0;

}

void reverse(const char * const sPtr)
{

if(sPtr[0] == '\0')
return;
else
{

reverse(&sPtr[1]);
putchar(sPtr[0]);

}

}



Can someone explain me how the code works, by dissecting it, thanks.
Mark



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

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

PostPosted: Fri Aug 05, 2005 6:12 am     Reply with quote

no pointers to constants allow in CCS. Read the manual and you will see this. Use a char* instead. The function also uses recursion. Since CCS doesn't put vars on a stack, this probably isn't possible either and I believe that's in the manual too. Furthermore, even if they did support recursion, function calls (depth) are limited by the size of the hardware stack which is 8 for PIC16's and 32 for PIC18's.
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

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

PostPosted: Fri Aug 05, 2005 6:15 am     Reply with quote

I don't know if you have the complete code, but it appears that the routine is trying to attempt recursion, which CCS says you shouldn't do.

This example doesn't use recursion, i got it from a website:

Code:

int Reverse(char* str)
{

if (NULL==str)return -1; //no string
int l=strlen(str)-1; //get the string length
if (1==l)return 1;

for(int x=0;x<l;x++,l--)
{
str[x]^=str[l];  //triple XOR Trick
str[l]^=str[x];  //for not using a temp
str[x]^=str[l];
}

return 0;

}


You see, doing an XOR three times swaps the contents, without the need for a temp variable.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Aug 05, 2005 11:09 am     Reply with quote

I would like to point out the at least for the PIC16's these restrictions are due to the chip hardware more than the compiler. Many PIC16's have a stack only 2 levels deep. That makes recursion really tough. Also the PICs have a Harvard architecture, seperate code and data spaces, which makes pointers to constants hard to impliment. Some compilers for PICs support pointers to constants, but it takes lots of code space to do it.
_________________
The search for better is endless. Instead simply find very good and get the job done.
vinith



Joined: 07 Aug 2005
Posts: 1

View user's profile Send private message

bluetooth
PostPosted: Sun Aug 07, 2005 10:43 pm     Reply with quote

hi all iam new to this grp
i have no great idea about this picmcu .
iam on the pressure to built a bluetooth chip onto picmcu .I am really confused on what way shud i proceed and what are the basics to get proceeded.

my aim is to make a bluetooth chip talk to this mcu.
waiting for replies.
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