char*s[20] a 1D or a 2D array and why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

char*s[20] a 1D or a 2D array and why?

3rd Feb 2019, 6:33 AM
Shivansh Mathur
Shivansh Mathur - avatar
8 Answers
+ 6
Shivansh Mathur Please don't ask a question if you think that you know the answer better than everyone who answers it. What you do with an array doesn't change how many dimensions it has. "Arrays can be single or multidimensional. The number of subscript or index determines the dimensions of the array." https://overiq.com/c-programming-101/one-dimensional-array-in-c/
3rd Feb 2019, 7:07 AM
Anna
Anna - avatar
+ 1
it's one dimensional array of size 20 and it holds character pointers
3rd Feb 2019, 6:52 AM
code learner
code learner - avatar
+ 1
arrays are essentially pointers so you can do s[0][3] but it doesn't mean its 2D array, only one dimension is on stack s[0][3] => *(*(s+0)+3) what we are doing is simple pointer arithmetic. correct me if I am wrong
3rd Feb 2019, 7:06 AM
code learner
code learner - avatar
+ 1
If we go by actual definition of array, its members are stored on consecutive memory location. then *s[20] is 1D, yes individual characters are consecutive but last char pointer of s[0] and s[1] are not consecutive. similarly **s is not array
3rd Feb 2019, 7:12 AM
code learner
code learner - avatar
3rd Feb 2019, 7:06 AM
code learner
code learner - avatar
0
you're correct code learner
3rd Feb 2019, 7:14 AM
Shivansh Mathur
Shivansh Mathur - avatar
- 1
No, it can be both 1D and 2D array depending on what you're doing with it
3rd Feb 2019, 6:50 AM
Shivansh Mathur
Shivansh Mathur - avatar
- 1
If it is being used to store individual bytes then it is 2D
3rd Feb 2019, 6:57 AM
Shivansh Mathur
Shivansh Mathur - avatar