0

I want to reinitialize d everytime in a loop

     char d[90];   
     while(ptr != NULL)
    {
        printf("Word: %s\n",ptr);
        //int k = 0;
        strcpy(d, ptr);
        d[sizeof(d)-1] = '\0';
        //something more
        ....
        ....
     }
5
  • then do it... what stops you? Commented Oct 2, 2009 at 18:14
  • 2
    is there any reason why d variable cannot be declared inside while loop? Commented Oct 2, 2009 at 18:15
  • 1
    @coelhudo yes "SCOPE". I suppose it will give a redeclaration error. Commented Oct 2, 2009 at 18:17
  • As stackoverflowers said to you in your other thread, you should use strncpy instead of strcpy to avoid copy a string ptr larger that d Commented Oct 2, 2009 at 18:49
  • Friends don't let friends use strncpy. If you want to check buffer lengths yourself, use strcpy. If you don't, use (or roll your own) strcpy_s so that you don't have to manually nul-terminate. stncpy's only virtue is that it's standard, all it does is replace buffer overruns on write with buffer overruns on read. Commented Oct 2, 2009 at 19:08

4 Answers 4

4

There's no need to do anything "before" strcpy(). Calling strcpy() on the buffer d will overwrite whatever is in the buffer, and leave the buffer holding the string pointed at by ptr at the time of the call. There's no need for the assignment of the last character to '\0'.

Of course, if you're doing the explicit termination because you're not sure if the strcpy() will overwrite d, then you have a problem. You should use strlen() on ptr before the copy to make sure it fits, or use snprintf() if you have it.

Sign up to request clarification or add additional context in comments.

3 Comments

@Alex: glad it helped ... And feel free to accept the answer, if you're satisfied.
strncpy is the safe version of strcpy rather than snprintf
@Pete: I disagree. The semantics of strncpy() are deeply strange, and I consider it best avoided. It has two major faults: it doesn't terminate the output string if the input is larger than the buffer, and it pads the output to its maximum size if the input is smaller. Very rarely what you want.
0

memset(d, 0, 90)?

Comments

0

unwind is exactly right. You don't need to do anything before strcpy(). However, if you find yourself later needing to initialize some memory to a particular pattern you can use this:

void* pointerToMemory;          // Initialize this appropriately
unsigned char initValue = 0;    // Or whatever 8-bit value you want
size_t numBytesToInitialize;    // Set this appropriately

memset(pointerToMemory, initValue, numBytesToInitialize);

Comments

0

Simply do strcpy(d, ptr). strcpy will add the NULL terminator at the end for you, of course assuming d can hold at least as many characters as ptr.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.