0

I have a character pointer which points to the character array. I want to clear the character array and then string copy some other array in it.The memset doesn't work on char pointer. Is there some other way around to do that ?

int main(){

        char a[100] = "coepismycollege";
        char *p;
        p  = a;
        test(p);
        printf("After function : %s", a);
}
void test(char *text){
        char res[120] = "stackisjustgreat";
        printf("text = %s\nres =  %s\n", text , res);
        memset(&text, 0 , sizeof(*text));
        strcpy(text, res);
}

output should be : stackisjustgreat
Thanks in advance.

4
  • 2
    memset(&text, 0 , sizeof(*text));--> memset(text, 0 , strlen(text)); But in this case memset is not necessary. Commented Sep 10, 2017 at 3:17
  • doesn't help either ! Commented Sep 10, 2017 at 3:19
  • 2
    @Savan BLUEPIXY's comment is correct. If it didn't work for you, then you need to post a minimal, complete, and verifiable example, because your code doesn't compile. To get more information about what's wrong with your code, compile with -Wall on gcc or clang. Commented Sep 10, 2017 at 3:27
  • You were right @BLUEPIXY Commented Sep 10, 2017 at 3:36

3 Answers 3

1

sizeof(*text) will always be 1 (it is same as to sizeof(char)). It is, however, sufficient, if you intended to null only the first byte of string.

memset's first argument is pointer to start of memory block, and text is already a pointer, so memset(text, 0 , strlen(text)); is correct (without the &)

However, memset is pointless, as the following strcopy will overwrite it anyway.

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

Comments

1

You can change test() function like this:

void test(char* text) {
    char res[120] = "stackisjustgreat";
    const size_t len = strlen(res); // NOT sizeof(res)
    memset(text, 0, len); // This is actually not necesary!
    strncpy(text, res, len); // text will already be properly null-terminated
}

Even shorter version could be:

void test(char* test) {
    char res[120] = "stackisjustgreat";
    strcpy(test, res, strlen(res));
}

Just find out the length of the string to which res points by strlen(res) and then use str(n)cpy() to copy the string. Because str(n)cpy() copy the null character as well, there is no more necessary to be done.

3 Comments

Your first snippet will actually cause undefined behavior. original pointer a points to char[100], and you are setting 120 bytes of it to 0.
@TomášZahradníček No, because I am using strlen(res), which actually goes through the res string byte after byte and stops when first null byte occurs, which is right after the "stackisjustgreat string (17th byte will be NULL, if I count correctly). The problem you point out would appear if I used sizeof(res) instead of strlen(res).
I stand corrected, didn't realise you used strlen that treats argument as null terminated string
0

On the line with the memset, argument 1 should simply be text, text is already a pointer. Putting &text will put a pointer to that pointer.

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.