473,805 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How is this trivial strncpy implementation

Do I need to repeat inserting '\0' n - strlen(src) times in case
strlen(str) less than n. "C: A reference Manual, 5th" mentions this in
sec 13.3. "K&RC, 2nd" says that insert one '\0' under the same
condition in sec B.3.

Thank you for your time.
/
*************** *************** *************** *************** *************** ****
* copy n characters in string src to dst.

*************** *************** *************** *************** *************** ***/

char *strncpy(char *dst, const char *src, size_t n)
{
char *p = dst;

while (n-- && (*dst++ = *src++))
;
return p;
}

Oct 23 '07 #1
2 2454
lovecreatesbea. ..@gmail.com wrote:
Do I need to repeat inserting '\0' n - strlen(src) times in case
strlen(str) less than n.
The Standard strncpy does. You can do whatever you want with your
private variations, but do avoid nameclashes with the Standard library,
and give a better more descriptive name. The Standard has a lot of
constraints, not least of which are historical ones, but you can do
better than cryptic six and seven letter names.

snip

Oct 23 '07 #2
"lovecreatesbea ...@gmail.com" <lo************ ***@gmail.comwr ites:

[about strncpy]
Do I need to repeat inserting '\0' n - strlen(src) times in case
strlen(str) less than n.
Yes, strncpy always writes to every byte in the output buffer.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Oct 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
6051
by: Vijay Kumar R Zanvar | last post by:
Hi, Is the following strncpy implementation according to C99? char * strncpy ( char *s, const char *t, size_t n ) { char *p = s; size_t i = 0;
12
15699
by: ­m½Z | last post by:
I am a C programming beginner... I wonder, why strncpy(s, t, n) does not put '\0' at the end of the string. Because when I output the copied string, it output more than what I want, until I put '\0' at the end by myself. But, sometime I don't need put '\0' and it work well?? Like strncpy(s, t, n); strcat(s, t1); .....
18
1595
by: Chris R. | last post by:
Hi everyone. I am trying to finish my homework, but I seem not to figure out how to fix this structure that seems to make wrong output. What this program should do is use structure to save the word and line number on which it was in array pointer using malloc and print them out alphabetized (no duplicates). Most of it works, except the words seem to be corrupted. Here is a code, if anyone can help me it would help so much since I've...
9
2041
by: fybar | last post by:
Hi, I am in the progress of writing a program that will read in the contents of a directory and then do some stuff to some of the files. So far I am getting the contents of the directory but I get a strange result from one file. Here is my program, feel free to critique: #include <sys/types.h> #include <dirent.h> #include <stdio.h>
17
2810
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t j = 0; j < 4; ++j) Why did you declare j as type size_t ?
20
1522
by: ghyott | last post by:
hello, In my opinion the following code should crash when run with *(argv+1)="1234567890" and *(argv+2)="1234567890" . int main(int argc,char **argv) { char buf1; char buf2; char buf3; strncpy(buf2,*(argv+1),sizeof(buf2));
17
3060
by: lovecreatesbeauty | last post by:
1. In the following code, is the code (line 11) legal? Is there a notice in the document to tell callers that the parameter s1 should receive an array variable, i.e. type char, but not a variable of char *? p1 and p2 point to the same things but they must be declared as different types? Is it nature? char p1 = "hello123456"; char *p2 = "world"; strncpy(p1, p2, strlen(p2));
27
2299
by: smnoff | last post by:
How does( or should user use) strncpy() to allocate enough memory space for the destination string, s1? I guess I am having trouble using strncpy as it seems to be giving me errors. And just as well, I don't know the value of n until the program runs, so it needs to be dynamically allocated. Then again I hard coded n to be number like3 just to even see if that works.
9
3172
by: Ron | last post by:
#define MAX_SIZE 512 char mybuffer; void myfunction( const char* src ) { if( src == null ) { return; } /* A core dump is occuring here */ strncpy( mybuffer, src, MAX_SIZE );
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.