strncpy not that easy to use

S

smnoff

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.


470 char *
471 strncpy(char *s1, const char *s2, size_t n)
472 {
473 char *os1 = s1;
474
475 n++;
476 while (--n != 0 && (*s1++ = *s2++) != '\0')
477 ;
478 if (n != 0)
479 while (--n != 0)
480 *s1++ = '\0';
481 return (os1);
482 }
 
R

Richard Heathfield

smnoff said:
How does( or should user use) strncpy() to allocate enough memory space
for the destination string, s1?

strncpy doesn't allocate any space. It just copies and pads. If you are
(rightly) concerned not to overflow your target buffer, strncpy is not
sufficient. You must know how many bytes of data you need from the source
string, and provide at least that many bytes (plus one for a null
terminator, if relevant, which it nearly always is) for strncpy to use as a
destination. If your real requirement is to copy the entire string, which
it nearly always is, at that point you might just as well use strcpy.
I guess I am having trouble using strncpy as it seems to be giving me
errors.

It is far more likely that /you/ are giving /it/ errors. If it really is
necessary for you to use strncpy, which it hardly ever is, I suggest you
show us your calling code, rather than your attempt at an implementation.
 
K

Keith Thompson

smnoff said:
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.


470 char *
471 strncpy(char *s1, const char *s2, size_t n)
472 {
473 char *os1 = s1;
474
475 n++;
476 while (--n != 0 && (*s1++ = *s2++) != '\0')
477 ;
478 if (n != 0)
479 while (--n != 0)
480 *s1++ = '\0';
481 return (os1);
482 }

You've quoted a particular implementation of strncpy(), but if you're
going to use it you should base your usage on the documented semantics
of the function, not on how it happens to be written in one particular
implementation.

You say it seems to be givin you errors, but you don't tell us what
kind of errors, so there's not much we can do to help.

In general, though, strncpy() isn't likely to be what you want to use.
If the target array is too small, it may not be terminated with a '\0'
character (i.e., it may not be a string); if it's larger than it needs
to be, strncpy() pads it with additional '\0' characters, which is
usually wasteful.

It's usually better to use strcpy(), or perhaps memcpy() *after*
making absolutely sure that the target array is big enough.

If you have specific questions, post your actual code and tell us how
it fails.
 
S

smnoff

This code is part of a DLL that was created in Visual Studio and hence VS
doesn't always give me the error message. This DLL is being called by an
external program totally outside of Visual Studio.

Actually, I really want to know how to make this dll code run entirely in
Visual Studio so I can get the debugger to work fully as this is Visual
Studio DLL project.

Would I have to create a totally separate project to get all this debugging
to work OR would I just get same unknown error messages I have been getting
when I use the external program?
 
K

Keith Thompson

smnoff said:
This code is part of a DLL that was created in Visual Studio and hence VS
doesn't always give me the error message. This DLL is being called by an
external program totally outside of Visual Studio.

Actually, I really want to know how to make this dll code run entirely in
Visual Studio so I can get the debugger to work fully as this is Visual
Studio DLL project.

Would I have to create a totally separate project to get all this debugging
to work OR would I just get same unknown error messages I have been getting
when I use the external program?

Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

If your IDE is hiding error messages from you, either find a way to
get it to show them to you or get a better IDE. (I don't use IDEs
myself, so perhaps I'm misunderstanding your situation.)

We can help you with C programming language issues here. We can't
help you with any problems you might be having with your IDE; you'll
have to find a different forum for that.
 
A

Andrew Poelstra

Hmm. I'll just explicit-AOL Keith's post, and hope you get the message:

Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

If your IDE is hiding error messages from you, either find a way to
get it to show them to you or get a better IDE. (I don't use IDEs
myself, so perhaps I'm misunderstanding your situation.)

We can help you with C programming language issues here. We can't
help you with any problems you might be having with your IDE; you'll
have to find a different forum for that.

Mee too!
 
C

CBFalconer

*** Rude top-posting fixed ***
smnoff said:
What IDE do the users at this forum "generally" use?

Are you illiterate? Why do you top-post when you have just been
politely asked to refrain. Why do you persist in asking about IDEs
when you have just been told they are off-topic here? Why do you
assume people generally use any IDE whatsoever, when they are
basically just a nuisance?

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
J

J. J. Farrell

smnoff said:
What IDE do the users at this forum "generally" use?

As Keith said, please don't top-post.

I can't speak for other people here, but I use the same IDEs as Keith.
 
S

smnoff

Are you illiterate? Why do you top-post when you have just been
politely asked to refrain.


Cause I didn't know what top-posting meant and all those who annoyed by it
didn't
provide a link to what "top-posting" was. That's WHY!

If they wanted something to stop they should take the time to at least copy
and paste a link to what they wanted to stop. Nobody knows what
"top-posting" is as this is the ONLY group that has mentioned it. And by the
way, that top-posting link is wrong, not a single newsgroup
person/user/frequent poster has ever, repeat ever said that was a problem,
It's just this group.
 
S

smnoff

As Keith said, please don't top-post.
I can't speak for other people here, but I use the same IDEs as Keith.


And by the way, lots of users of e-mail top-post.
Was it easy to read other people's e-mail when I got started? NO it wasn't
and they didn't give it a name either.
But I had to get used to it.
 
C

CBFalconer

smnoff said:
Cause I didn't know what top-posting meant and all those who
annoyed by it didn't provide a link to what "top-posting" was.
That's WHY!

If they wanted something to stop they should take the time to at
least copy and paste a link to what they wanted to stop. Nobody
knows what "top-posting" is as this is the ONLY group that has
mentioned it. And by the way, that top-posting link is wrong, not
a single newsgroup person/user/frequent poster has ever, repeat
ever said that was a problem, It's just this group.

Both Keith Thompson and myself gave you the following link:

<http://www.caliburn.nl/topposting.html>

so that excuse is a bald faced lie. Some groups (mistakenly) will
tolerate top-posting, but it is never correct. Also, don't remove
attributions for material you quote. Attributions are the initial
lines that say: "Joe Blow wrote:"
 
K

Keith Thompson

smnoff said:
Cause I didn't know what top-posting meant and all those who annoyed by it
didn't
provide a link to what "top-posting" was. That's WHY!

I did.
 
S

smnoff

CBFalconer said:
Both Keith Thompson and myself gave you the following link:

<http://www.caliburn.nl/topposting.html>

so that excuse is a bald faced lie. Some groups (mistakenly) will
tolerate top-posting, but it is never correct. Also, don't remove
attributions for material you quote. Attributions are the initial
lines that say: "Joe Blow wrote:"


It took you forever to give the link when it should have been done weeks
ago. and if I didn't see it, you should have kept redrawing attention to it
to begin with, period. So it's your fault, period.
 
R

Richard Heathfield

[Top-posting again]

smnoff said:
It took you forever to give the link when it should have been done weeks
ago.

No, it should never be necessary to give such a link, and it should never be
necessary to explain to people how to use this group, because they're
supposed to spend a few weeks reading it, so that they can learn by
example.
and if I didn't see it, you should have kept redrawing attention to
it to begin with, period.

No, it's not his job to teach you good manners and common sense. That's your
responsibility, not his.
So it's your fault, period.

No, it's yours.

And now you have two choices - continue to be antagonistic towards the very
people from whom you are seeking expert help, or wise up fast.
 
R

Richard Heathfield

smnoff said:
And by the way, lots of users of e-mail top-post.

So what? The stupidity of other people does not mean that contributors to
comp.lang.c have to be stupid too.
Was it easy to read other people's e-mail when I got started? NO it wasn't

Then you should have complained at the time.
and they didn't give it a name either.
But I had to get used to it.

No, you didn't. And you still don't.
 
K

Keith Thompson

smnoff said:
It took you forever to give the link as well. But until you did, you didn't
get what you wanted either.

What are you talking about? You posted Wednesday at 11:31:50 Pacific
time. I posted a followup at 12:27:45, less than an hour later; in that
followup, I wrote:

| Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

In any case, you now know what top-posting is, and you know that it's
discouraged in this newsgroup (as it is in most newsgroups).

Shall we get back to discussing C?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top