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

Help with strcpy

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







Help with strcpy
PostPosted: Wed Mar 26, 2003 9:34 pm     Reply with quote

Hello,

I must be having a brain malfunction. I can't see why the following sample program does not work (PCH 3.148):

#include <18F452.h>

#fuses XT,NOWDT,PUT,NOLVP,NOPROTECT
#device *=16
#device ADC=10
#use delay(clock=4000000, RESTART_WDT) // 4 MHz clock
#use rs232(baud=19200, parity=N, bits=8, xmit=PIN_C6, rcv=PIN_C7, errors)

#include <string.h>

void rom_fmtstr_to_ram(char *destStr)
{
printf("rom_fmtstr_to_ram start: \%s\n\r", destStr);
strcpy(destStr, "Carp");
printf("rom_fmtstr_to_ram end: \%s\n\r", destStr);
}

// The test.
main()
{
char bString[10];

// Put something into the string
strcpy(bString, "Hello");

// Re-write the string
rom_fmtstr_to_ram(bString);

// Display the results (should be different)
printf("Main Done: \%s\n\r", bString);

// Don't fall off into the deep SLEEP
while(1);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13115
Ron
Guest







Re: Help with strcpy
PostPosted: Thu Mar 27, 2003 6:13 am     Reply with quote

Code looks ok to me.

What exactly is not working?

:=Hello,
:=
:=I must be having a brain malfunction. I can't see why the following sample program does not work (PCH 3.148):
:=
:=#include <18F452.h>
:=
:=#fuses XT,NOWDT,PUT,NOLVP,NOPROTECT
:=#device *=16
:=#device ADC=10
:=#use delay(clock=4000000, RESTART_WDT) // 4 MHz clock
:=#use rs232(baud=19200, parity=N, bits=8, xmit=PIN_C6, rcv=PIN_C7, errors)
:=
:=#include <string.h>
:=
:=void rom_fmtstr_to_ram(char *destStr)
:={
:= printf("rom_fmtstr_to_ram start: \%s\n\r", destStr);
:= strcpy(destStr, "Carp");
:= printf("rom_fmtstr_to_ram end: \%s\n\r", destStr);
:=}
:=
:=// The test.
:=main()
:={
:= char bString[10];
:=
:= // Put something into the string
:= strcpy(bString, "Hello");
:=
:= // Re-write the string
:= rom_fmtstr_to_ram(bString);
:=
:= // Display the results (should be different)
:= printf("Main Done: \%s\n\r", bString);
:=
:= // Don't fall off into the deep SLEEP
:= while(1);
:=}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13132
Burt Poppenga
Guest







Re: Help with strcpy
PostPosted: Thu Mar 27, 2003 8:42 am     Reply with quote

The output of the routine is:
rom_fmtstr_to_ram start: Hello
rom_fmtstr_to_ram end:
Main Done: Hello

I expected something more like:
rom_fmtstr_to_ram start: Hello
rom_fmtstr_to_ram end: Carp
Main Done: Carp
___________________________
This message was ported from CCS's old forum
Original Post ID: 13134
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Help with strcpy
PostPosted: Thu Mar 27, 2003 12:59 pm     Reply with quote

:=I must be having a brain malfunction. I can't see why the following sample program does not work (PCH 3.148):
:=
:=void rom_fmtstr_to_ram(char *destStr)
:={
:= printf("rom_fmtstr_to_ram start: \%s\n\r", destStr);
:= strcpy(destStr, "Carp");
:= printf("rom_fmtstr_to_ram end: \%s\n\r", destStr);
:=}
---------------------------------------------------------

I don't have PCH, but PCM 3.149 with a 16F877 shows the same
behavior. I think it's a bug in strcpy(). It doesn't seem
to be able handle a passed array pointer. It uses ram
locations for temporary counters, that are part of the
destination array. If you change the code so that the
array is in the local routine, then strcpy works OK.

void rom_fmtstr_to_ram(char *destStr)
{
char tempStr[10];
printf("rom_fmtstr_to_ram start: \%s\n\r", destStr);
strcpy(tempStr, "Carp");
printf("rom_fmtstr_to_ram end: \%s\n\r", tempStr);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13155
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