Is char buf[30] = {0} necessary ? 
Author Message
 Is char buf[30] = {0} necessary ?

char buf[30] = {0 };

Should I initialize buf with null character above ? Isn't default
initialization assured by the standard ?



Sat, 28 Feb 2004 16:41:40 GMT  
 Is char buf[30] = {0} necessary ?

Quote:
> char buf[30] = {0 };
> Should I initialize buf with null character above ? Isn't default
> initialization assured by the standard ?

Only if it's a static variable. If it is a normal automatic variable,
no default initialisation exists. In that case you do need the null
character if you intend to use the value before assigning to it.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"This is a personnel commuter."
   - Train driver in Scientific American



Sat, 28 Feb 2004 17:07:40 GMT  
 Is char buf[30] = {0} necessary ?

Quote:

> char buf[30] = {0 };
> Should I initialize buf with null character above ? Isn't default
> initialization assured by the standard ?

Only if it's "global" (or rather, has file scope) or it's an object of
static storage.

For local variables, this initialisation is correct. (Assuming you want
an array of '\0' characters.)

If this array is for a string, you might get away with just assigning
the first element. It's up to you.

Bill, 'ip 'ip,



Sat, 28 Feb 2004 17:22:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Reg.allocating memory for double char pointer(char ** buf)

2. char buf[20] to char*

3. void *buf in ssize_t read(int fildes, void *buf, size_t nbyte)

4. Why is extern int * buf != extern int buf[] ?

5. 12 30 1899 - Why?

6. MD5 hash length 30 versus 32 under VB?

7. Where is the 30 day demo maker?

8. SEPTEMBER 30, 1999...

9. Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

10. pow(e, -30)

11. Upload of files of more than 30 Kbytes

12. Pre-reg Deadline May 30 - 3rd Conference on Object-Oriented Technologies and Systems (COOTS) - Conference Program

 

 
Powered by phpBB® Forum Software