|
-
February 18th, 2010, 10:43 AM
#1
memset problem
Hi,
Sorry for putting a c code.
I am trying to use memset. But it is behaving in a strange manner.
Code:
int main()
{
char arr[99];
char tt[] = "hello";
memcpy(arr,tt,5);
memset(arr+5,' ',94);
printf("%s",arr);
return 0;
}
The out put is giving hello blank space and some junk at the end.
But as per my knowledge it should only print hello.
I am using vs2005.
Please clarify
-
February 18th, 2010, 10:46 AM
#2
Re: memset problem
You need to add 0 in the end of string, and not space:
memset(arr+5,0,94);
-
February 18th, 2010, 11:05 AM
#3
Re: memset problem
Thanks Alex it worked.
But one thing i want to know when i used memcpy(arr,tt,5); tt is null terminated so does the memcpy do not copy the null here
-
February 18th, 2010, 11:05 AM
#4
Re: memset problem
 Originally Posted by Rajesh1978
Hi,
Sorry for putting a c code.
I am trying to use memset. But it is behaving in a strange manner.
Code:
int main()
{
char arr[99];
char tt[] = "hello";
memcpy(arr,tt,5);
memset(arr+5,' ',94);
printf("%s",arr);
return 0;
}
The out put is giving hello blank space and some junk at the end.
But as per my knowledge it should only print hello.
I am using vs2005.
Please clarify
What are you trying to accomplish there? Why not just use strcpy?
-
February 18th, 2010, 11:06 AM
#5
Re: memset problem
 Originally Posted by Rajesh1978
Thanks Alex it worked.
But one thing i want to know when i used memcpy(arr,tt,5); tt is null terminated so does the memcpy do not copy the null here
You told it to copy 5 bytes. The null is in the sixth position.
-
February 18th, 2010, 11:30 AM
#6
Re: memset problem
I was trying to understand the behaviour of memset and memcpy.
I had a misconception that it will not copy the null character.
thanks
-
February 18th, 2010, 03:47 PM
#7
Re: memset problem
btw if I were you I'd always initialiaze vars with something:
Code:
char arr[99] = {0};
-
February 18th, 2010, 07:03 PM
#8
Re: memset problem
 Originally Posted by GCDEF
You told it to copy 5 bytes. The null is in the sixth position.
And even if you had copied at, you would have overwritten it with a space next.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 18th, 2010, 07:51 PM
#9
Re: memset problem
 Originally Posted by Rajesh1978
I was trying to understand the behaviour of memset and memcpy.
I had a misconception that it will not copy the null character.
thanks
It copies whatever you tell it to copy. hello is 5 bytes. You told the memcpy to copy 5 bytes. If you wanted it to copy the NULL then you tell it to copy 6 bytes. Or you do as others have suggested and use strcpy. memcpy doesn't care what is in the source memory block. It copies exactly how many bytes you tell it to copy irrespective of what is contained within that source memory block.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|