c - What is the purpose of array[index] = 0;? -


char dev[20] = "dev_name"; char dest[32]; strncpy(dest,dev,sizeof(dev)); dest[sizeof(dev)-1] = 0; 

what dest[sizeof(dev)-1] = 0; means?

in code, assuming size analogous sizeof,

 dev_name[size(dec_name)-1] = 0; 

dev_name[size(dec_name)-1] points last element of array, remember c arrays use 0 based indexing.

then, definition, string in c exxentially char array with null-termination, if want use char array string, must have null-termination.

0 ascii value of nul or null. so, essentially, you're putting null-terminator char array.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -