Char array not initializing empty in C -


i have char array collect names, positions not being initialized empty. if value entered, rest of positions empty, if nothing entered, positions filled "weird" characters. how can fix this?

depends on how you're initializing array. statically allocated arrays (arrays outside of function not dynamically allocated):

char arr[20] = {0};  void function( void ) {     // stuff happens here array } 

for automatic arrays (arrays within scope of function not dynamically allocated):

void function( void ) {    char arr[20] = {0};    // stuff happens array , cannot     // accessed outside function } 

for dynamic arrays, use calloc() dynamically allocate , initialize 0:

size_t len = 20; char *arr = calloc ( len , sizeof(char)); 

you can use memset() clear arrays:

char arr[20]; memset (arr, 0 , sizeof(arr)); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -