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

memcpy want to copy some value

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



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

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

memcpy want to copy some value
PostPosted: Tue Nov 05, 2013 5:22 am     Reply with quote

Hi i try to figure how to send only some data from reveived string...not succesfull...want to extract like: D8 FE 52 F7 in the string...

Code:

(data in received buffer :FF FF FF FF FF 06 80 00 0E 00 D8 FE 52 F7 05 05 03 1D 18 00 00 06 28 23 00)

 char* Received;
 char new[26];
 Received=fgetc(COM1);
 fprintf(COM2,"%c",Received); // working see all value

 memcpy(new,&Received[10],1);
 new[10] = 0; 
 fprintf(COM2,"%c",new); // not working

can somebody show me how to do it???
Thanks...
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Tue Nov 05, 2013 5:38 am     Reply with quote

For the example data, show us what you expect to see as the output.
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

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

PostPosted: Tue Nov 05, 2013 5:41 am     Reply with quote

Hi ....send the result value in hex by com2
Regards
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Nov 05, 2013 5:57 am     Reply with quote

total confusion!

- fgetc() result is char not pointer type
- Received[10] is neither in declared variable space nor loaded with a value
- fprintf(COM2,"%c",new); is not printing new[10] or new[0]

Suggestion: Learn quite a bit about C data types, characters, strings, pointers and string functions
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

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

PostPosted: Tue Nov 05, 2013 6:02 am     Reply with quote

i know it's no good ...it's not working Smile
and how i can extract some value for my received buffer and send it on com2???

use substring?? memcpy??

regards...
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Tue Nov 05, 2013 8:41 am     Reply with quote

Fvm has pointed out some of the problems. It is just not correct C. Comments inline:
Code:


 char* Received;
//received is now a _pointer_ to a char, but as yet points nowhere.
//No memory saved for it to point to, and the initial value is
//indeterminate...
 char new[26];
//new is now a pointer to a 26 character memory area.
 Received=fgetc(COM1);
//You now write a single received byte into the _pointer_. Garbage.
//_Single byte_.
 fprintf(COM2,"%c",Received); // working see all value
//Here the incorrectly stored pointer, will be automatically cast 'back'
//to a character. It'll sort of work, but it is wrong in multiple ways.
 memcpy(new,&Received[10],1);
//'Received[10], will be 10 greater than the value received. Could be
//pointing anywhere in memory. You could retrieve anything.

 new[10] = 0;
 fprintf(COM2,"%c",new); // not working
//fprintf, expects to receive a character. You are giving it the address
//of the first byte of 'new'. Of course it won't work.


You need to get a C primer, and read the section on pointers and arrays, then try some simple exercises on a PC C. Because of the error trapping on this, you will get errors when you try to talk to memory areas you shouldn't which may help you to understand just how wrong everything is.
There is only about one line of the code that is not a glaring error...
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