[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C strings strncpy and memcpy

Posted on 2006-04-02
2
Medium Priority
?
728 Views
Last Modified: 2012-06-27
I'm having a problem with C strings I can't understand it..

I'm trying to get a section out of a string..

char section[5];

char tempString[32] = "123456ABCDEFGHIJKLMNOPQRSTUVWXYZ";

memcpy(section, tempString + 16, 5);

printf("%s", section);

I get KLMNOÖÿ¿`X@
0
Comment
Question by:cfans
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 400 total points
ID: 16357013
You copy five characters into section, but section now lacks a NUL terminating byte.  C strings are terminated by byte of 0.  When you call printf(), printf runs off the end of section[] looking for the NUL byte and stops when it eventually finds one somewhere in memory.

Try this:

char section[6];

char tempString[32] = "123456ABCDEFGHIJKLMNOPQRSTUVWXYZ";

memcpy(section, tempString + 16, 5);
section[5] = '\0';
0
 
LVL 11

Assisted Solution

by:x4u
x4u earned 400 total points
ID: 16357029
You forgot to terminate your string with a 0 byte. Because of this printf treated all memory after the end of your string as part of it until it found a 0 byte.

In C the end of strings needs to be terminated this way. This is already done automatically for literal strings like "abc" which actually consists of 4 bytes: 'a', 'b', 'c', '\0'.

This is your code with correct termination:

char section[6]; // need one byte more

char tempString[32] = "123456ABCDEFGHIJKLMNOPQRSTUVWXYZ";

memcpy(section, tempString + 16, 5);

section[5] = '\0'; // last byte

printf("%s", section);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question