0

I would like to initialize an array of strings with \0. Is it right to do it like this?

char first[1024][1024] = {'\0'};
5
  • in this way, your array will be totally zeroed. Commented Aug 29, 2014 at 16:23
  • You can make it simpler and just use {0}. Commented Aug 29, 2014 at 16:24
  • @Arpit: I'm not seeing a static, so I'm not sure what you're talking about. Commented Aug 29, 2014 at 16:25
  • it was full of rubbish before initialization :) Commented Aug 29, 2014 at 16:26
  • possible duplicate of Initialize a 2D-array at declarationtime in the C programming language Commented Aug 29, 2014 at 16:31

2 Answers 2

2

For a 2d array is better to use:

char first[1024][1024] = {{'\0'},{'\0'}};

or better yet (as suggested by @haccks):

char first[1024][1024] = {{'\0'}};

in order to avoid warnings.

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

3 Comments

@haccks, sorry for rollback your edit, but gcc gives a warning using {'\0'}
@AlterMann; Interesting!
@haccks, edited, you are right, compiles without warning and I was trying char first[1024][1024] = {'\0'};
0

If it is a static array, say, a global array, you do not need to do any initialization and the values of the array are set to 0 by default.

1 Comment

It cannot be judged the array in the question is static or not from the expression of the question, so why you are sure that it is not a static array? @lgor

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.