Hey guys, simple question that I don't see anything on in the String.h library and I can't remember from my C class 3 years ago. I have the following declaration:

char something[100] = "whatever";

And later in the program I call strncpy. After calling strncpy, at some later point I want to reset something to "whatever" again. Is this the best way to go about it? (If this even works, which I'll test in a second - but either way is there a better way?)

something[0] = '\0';
stncpy(something, "whatever", 7);

Recommended Answers

All 8 Replies

(If this even works, which I'll test in a second

You posted this without even showing the ambition to try it first?

Like I said, I looked through the Strings.h header file and documentation but I am sure that there is a better way to do it. I wanted to know what that better way is. So it doesn't really matter whether or not it works, but what the best way to reset the string is. Anyway, I give help here far more than I ask for it. Do you think I don't do any programming myself, and just pop in and ask questions once every 3 months when I actually program something? No, I program constantly, I just seldom ask questions because I can usually figure out on my own, so why not give me the benefit of the doubt and just either A) answer the question or B) don't respond to the thread.

Thanks :)

P.S. I'm currently attempting to figure out how to issue system commands on my server such as pwd and ls, then return the results to my client, but I cannot find any useful references, so if you want to help with that, please do.

From what you posted strncpy looks fine

stncpy(something, "whatever", 7);

note the string "whatever" is nine characters....or maybe you should use

stncpy(something, "whatever", strlen("whatever") + 1);

DESCRIPTION
The strcpy() function copies the string pointed to by src, including
the terminating null byte ('\0'), to the buffer pointed to by dest.
The strings may not overlap, and the destination string dest must be
large enough to receive the copy.

The strncpy() function is similar, except that at most n bytes of src
are copied. Warning: If there is no null byte among the first n bytes
of src, the string placed in dest will not be null terminated.

Nevermind my last question, I just realized that I can just redirect the output to the socket (assuming this is possible which I think it is) and that should work.

From what you posted strncpy looks fine

stncpy(something, "whatever", 7);

note the string "whatever" is nine characters....or maybe you should use

stncpy(something, "whatever", strlen("whatever"));

Ok, it just seems strange compared to how easy it is in Java. Anyway, did you mean 8 characters, because from the docs it seems like the null terminator is not included (and whatever by itself is 8 chars).

Nevermind my last question, I just realized that I can just redirect the output to the socket (assuming this is possible which I think it is) and that should work.

You really should take the time and post something organized, its would save everyone a lot of time and effort.

Ok, it just seems strange compared to how easy it is in Java.

It just seems strange that girls have to sit down compared to how boys can just stand. Maybe it's the nature of the beast...
Wondering why an orange and a cumquat aren't the same is a waste of time -- you need to learn and accept the differences. There's always a reason.

Anyway, did you mean 8 characters, because from the docs it seems like the null terminator is not included (and whatever by itself is 8 chars).

No, nine characters. The null terminator is part of the string and must be included in the count.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.