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

strcpy vs strncpy

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



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

strcpy vs strncpy
PostPosted: Wed Feb 08, 2006 11:08 pm     Reply with quote

The first example doesn't work, the second does. What am I doning wrong?

Code:

char data_string[40];
char data_input[40];

data_input[0]='A';
data_input[1]='B';
data_input[2]='C';
data_input[C]=0;

strcpy(data_string,data_input);
printf(data_string);


But nothing is stored in data_string, but I found a work-around.

Code:

char data_string[40];
char data_input[40];

data_input[0]='A';
data_input[1]='B';
data_input[2]='C';
data_input[C]=0;

strncpy(data_string,data_input,40);
printf(data_string);


This works. What am I doing wrong?


Last edited by Eugeneo on Wed Feb 08, 2006 11:47 pm; edited 1 time in total
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

try
PostPosted: Wed Feb 08, 2006 11:14 pm     Reply with quote

try this...
Code:
printf("%s",data_string);
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Wed Feb 08, 2006 11:31 pm     Reply with quote

I used the printf to debug the code. There was a problem with my string copy before I inserted the printf.
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

Is this part of the string!!
PostPosted: Wed Feb 08, 2006 11:38 pm     Reply with quote

Check this below.You are using the array wrongly i guess!!check this
Code:
data_input[C]=0;

why do you have C as an index.did you define it some where else?
if not change it to 3 i.e.
Code:
data_input[3]=0;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 08, 2006 11:39 pm     Reply with quote

1. What PIC are you using ?

2. What is your compiler version ?
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

Re: Is this part of the string!!
PostPosted: Wed Feb 08, 2006 11:48 pm     Reply with quote

kel wrote:
Check this below.You are using the array wrongly i guess!!check this
Code:
data_input[C]=0;

why do you have C as an index.did you define it some where else?
if not change it to 3 i.e.
Code:
data_input[3]=0;


Sorry.. It was right in the original.

Compiler: 3.227
Chip: 16F877
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Wed Feb 08, 2006 11:53 pm     Reply with quote

I noticed there was a fix for the 18F, but I guess it was always working for the 16F?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 09, 2006 12:05 am     Reply with quote

I tested the following program with PCM vs. 3.227 and it displayed this:

ABC

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=========================
void main()
{
char data_string[40];
char data_input[40];

data_input[0]='A';
data_input[1]='B';
data_input[2]='C';
data_input[3]=0;

strcpy(data_string, data_input);
printf(data_string);

   
while(1);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 09, 2006 12:12 am     Reply with quote

Quote:
There was a problem with my string copy before I inserted the printf.

I didn't see your statement above, initially.
So I made a new test program in which printf() is commented out and
stepped through it with the simulator. The values are in there.
I saw 0x41, 0x42, 0x43 go into 'c' in the watch window, as I stepped
through the code.

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=========================
void main()
{
char data_string[40];
char data_input[40];
char c;

data_input[0]='A';
data_input[1]='B';
data_input[2]='C';
data_input[3]=0;

strcpy(data_string, data_input);
//printf(data_string);

c = data_string[0];
c = data_string[1];
c = data_string[2];
c = data_string[3];

   
while(1);
}
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Thu Feb 09, 2006 2:26 am     Reply with quote

I've tested it again, and it doesn't work. This is the code I get

Code:


008     PSP_DATA
015     CCP_1_LOW
015-016 CCP_1
016     CCP_1_HIGH
01B     CCP_2_LOW
01B-01C CCP_2
01C     CCP_2_HIGH
020     strtok.save
021-024 _Randseed
025-04C data_string
04D-074 data_input
075     rs232_errors
077     @SCRATCH
078     @SCRATCH
078     _RETURN_
079     @SCRATCH
07A     @SCRATCH
07B     @SCRATCH
07C     main.@SCRATCH
07D     @delay_ms1.P1
07D     main.@SCRATCH
07E     main.@SCRATCH

.................... data_input[0]='A';
*
002F:  MOVLW  41
0030:  MOVWF  4D
.................... data_input[1]='B';
0031:  MOVLW  42
0032:  MOVWF  4E
.................... data_input[2]='C';
0033:  MOVLW  43
0034:  MOVWF  4F
.................... data_input[3]=0;
0035:  CLRF   50
....................
.................... strcpy(data_string, data_input);
0036:  MOVLW  4D
0037:  MOVWF  10     
0038:  MOVLW  25
0039:  MOVWF  7C

003A:  MOVF   10,W     W=4D
003B:  MOVWF  04       FSR=4D
003C:  MOVF   00,W     
003D:  MOVWF  7E       7E ='A'
003E:  MOVF   7C,W     
003F:  MOVWF  04       FSR=25     
0040:  MOVF   7E,W     W='A'
0041:  MOVWF  00       REG 25='A'
0042:  MOVF   00,F     Test reg
0043:  BTFSC  03.2     Zero bit
0044:  GOTO   048      Zero END
0045:  INCF   7C,F
0046:  INCF   7D,F     Should this not be 10?     
0047:  GOTO   03A      Loop

.................... printf("%s\n\r",data_input);
0048:  MOVLW  4D
0049:  MOVWF  04
004A:  MOVLW  00
004B:  IORWF  00,W
004C:  BTFSC  03.2
004D:  GOTO   054
004E:  BTFSS  0C.4
004F:  GOTO   04E
0050:  MOVF   00,W
0051:  MOVWF  19
0052:  INCF   04,F
0053:  GOTO   04A
0054:  MOVLW  0A
0055:  BTFSS  0C.4
0056:  GOTO   055
0057:  MOVWF  19
0058:  MOVLW  0D
0059:  BTFSS  0C.4
005A:  GOTO   059
005B:  MOVWF  19
.................... printf("%s\n\r",data_string);
005C:  MOVLW  25
005D:  MOVWF  04
005E:  MOVLW  00
005F:  IORWF  00,W
0060:  BTFSC  03.2
0061:  GOTO   068
0062:  BTFSS  0C.4
0063:  GOTO   062
0064:  MOVF   00,W
0065:  MOVWF  19
0066:  INCF   04,F
0067:  GOTO   05E
0068:  MOVLW  0A
0069:  BTFSS  0C.4
006A:  GOTO   069
006B:  MOVWF  19
006C:  MOVLW  0D
006D:  BTFSS  0C.4
006E:  GOTO   06D
006F:  MOVWF  19
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 09, 2006 12:16 pm     Reply with quote

I've installed your version, and I've gone back to vs. 3.188 as well,
and I don't see the bug. The address that you're getting, which is
0x10, is not even listed in your symbol table. Can you confirm that
your version is 3.227 ? The version is given at the top of the .LST file.

Also, can you re-install your compiler ? Something is very wrong,
and I think you should re-install it.
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