c++ - I have big confusion to understand the difference between `char *var[3]`, `char var[3][15]` -


as in title.

i'm confused between them.

char var[3][15]= {"hello_world!", "good", "bad"}; // known 2d array.   char *var[3]= {"hello_world!", "good", "bad"}; // , think 2d array, how. 

what's difference between them?
what cases use or that?

i'm sorry have big confusion between them.

there's 2 types of 2 dimensional arrays, , have both types there.

with first one, it's array of 5 char[15] objects, layed out sequentially in memory. unused bytes in end of each "row" (in particular case not always) filled zeros. people think of when "two dimensional array", people call "square" array distinguish other type. when initialize it, string literals copied array directly.

[0][ 0] = 'h' [0][ 1] = 'e' ... [0][14] = '\0' (end of each row filled zeros) [1][ 0] = 'g' [1][ 1] = 'o' ... [3][13] = '\0' (end of each row filled zeros) [3][14] = '\0' (end of each row filled zeros) 

the second 1 array of 5 char* (pointers) usually refers array of char objects not have same size. if point @ arrays of char objects, can accessed 2 dimensional array. since length of each array may different size, referred "jagged array". in code, there's 3 "rows", first 13 chars long, second 5 chars long, , third 4 chars long. first index of array sequential in memory, arrays forming "inner" index can anywhere in memory. when initialize it, forms one-dimensional array points @ actual string literals. together, form two-dimensional array.

[0] -> "hello_world!" [1] --------------------------------->"good" [2] ---------------------->"bad" 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -