LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2006, 03:20 AM   #1
syseeker
Member
 
Registered: Aug 2003
Posts: 66

Rep: Reputation: 15
memset vs char arr initialization


just a bit curious to ask...

given uint8_t block[25];

is

memset(block, '\0', 25);

equivalent to

for (i=0; i<25; i++)
block[i] = '\0';

?
 
Old 03-29-2006, 03:29 AM   #2
stephenwalter
Member
 
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Rep: Reputation: 15
Hello,
There is one mistake though in what u hav typed. memset takes second argument as an integer so typically that has to be
memset(block,0,25);
Having said that , the behaviour is the same in both cases.

Regards,
S.Suresh Stephen.
 
Old 03-29-2006, 04:07 AM   #3
syseeker
Member
 
Registered: Aug 2003
Posts: 66

Original Poster
Rep: Reputation: 15
Arh.. ok, thanks for pointing that out, but why does glibc spec describes

void * memset (void *block, int c, size_t size) Function
This function copies the value of c (converted to an unsigned char) into each of the first size bytes of the object beginning at block. It returns the value of block.
 
Old 03-29-2006, 04:31 AM   #4
stephenwalter
Member
 
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Rep: Reputation: 15
Hi,
The memset function will behave in such a manner after setting the value it would return a void pointer to that block which can be type casted into a char* , but take both arrays and check it up , both will have the same contents.

Regards,
S.Suresh Stephen
 
Old 03-29-2006, 09:01 AM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by stephenwalter
Hello,
There is one mistake though in what u hav typed. memset takes second argument as an integer so typically that has to be
memset(block,0,25);
In C, it doesn't matter whether you write
memset(block,0,25);
or
memset(block,'\0',25);

memset vs. loop: The result is the same, but typically memset is highly tuned, so that it executes much faster for large buffers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
int arr[2]={23,12,45}; aditya Programming 1 03-03-2003 09:32 AM
arr not sure whats going on chens_83 Linux - General 7 02-12-2002 08:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration