You are here:



I have a few small questions about type initializations in C/C++:

- When I want to initalize a string that will be used later, I do it like below:

char szString[30]={'\0'};

Is there any problem with this string initialization? or What is the standart initialization method for strings?

- How can I initialize array of arrrays?
(array of Strings and array of integers)

- How can I initialize typedef types?
typedef struct{
int myint;
char mystring[30];


Hello Cem, thank you for the question.

Initializing a string like this: char szString[30]={'\0'};
is fine. You could also do the following:
char szString[30] = {0};. The way that I always initialize my objects out to 0 is with the function memset() that can be found in memory.h. It will 0 out your memory fairly
inexpensively to the processor. This function works for arrays, typedefs, structs, everything because of its parameters. Here are examples of zeroing out an array of strings, an array of ints, and a structure:

const int numObjects = 30;

// strings
char* strArr[numObjects];

memset(strArr, 0, sizeof(char*) * numObjects);

// ints
int intArr[numObjects];

memset(intArr, 0, sizeof(int) * numObjects);

// using your typedef struct above
myType x;

memset(&x, 0, sizeof(myType));

This first parameter to memset() is a pointer to the object to be set. An array is already a pointer, so the name suffices. The second parameter is what you wish to set it to, which should be 0 for proper empty initialization. The third parameter is the number of elements to set, which is the sizeof the object. Notice that with an array, we have a contiguous block of memory allocated of the specific type. So to properly set the entire array, you use the sizeof the elementes in the array, multiplied by the number of elements in the array to make sure we get the entire block of memory. Please do not hesitate to ask another question if you have

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 All rights reserved.