473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 2431
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.comwrites:

[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);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,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)break;else default:continue;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
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
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...
18
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...
9
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...
17
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...
20
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;...
17
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...
27
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...
9
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.