User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,648 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,872 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:

Copy argv to string in C(newbie question)

Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Copy argv to string in C(newbie question)

  #10  
Nov 1st, 2007
I never said memcpy() wasn't standard. Only that strncpy() was also.
The difference in choice is this: do you own/did you allocate the memory you are accessing? If not, you may very well be playing with fire to try reading (or writing) past the end of memory you have not allocated.

The strncpy() function always pads the target out to n bytes. However, it stops reading the source when it hits the end of the string, which the C specification guarantees to be there in argv[].

You are correct about watching the end of string, as strncpy() does not guarantee that the target is null-terminated.

I will agree that the use of strncat() is the best choice so far, since it addresses both issues nicely.

The strtol() has the same validation problems as stroi(). Just because it returns something different... But you are right in that it is a little more convenient for catching incomplete conversions...

Cheers.
Reply With Quote  
All times are GMT -4. The time now is 1:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC