View previous topic :: View next topic |
Author |
Message |
glenjoy
Joined: 18 Oct 2004 Posts: 21
|
C programming pointer explanation needed |
Posted: Fri Aug 05, 2005 5:48 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Aug 05, 2005 6:12 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Fri Aug 05, 2005 6:15 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Aug 05, 2005 11:09 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
vinith
Joined: 07 Aug 2005 Posts: 1
|
bluetooth |
Posted: Sun Aug 07, 2005 10:43 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|