1

Consider the following declaration:

char *name[]={"John","Beckham"}; 

Is this considered as 2D Array or not?

Because my professor told me that it's not 2D array but something else,

and if yes ..we can say that we can declare 2D array without specifying dimensions.

2 Answers 2

4

It is an array of char*. It's length is 2. The first element is a pointer which points to the string literal "John", and the second is a pointer which points to the string literal "Beckham".

The entire structure is two-dimensional, in the sense that two numbers specify the location of one of the char elements (for example, the character 'k' is located at (1,3)). But it is not rectangular, so it does not have a simple size like [2, 7] (since there is no (0,4) element). It is not what people usually refer to when they talk about a "two-dimensional array".

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer so far. The declaration has some characteristics of a 2D array, e.g. it's valid to write char c=name[0][2];, but it's not what most C programmers think of as the canonical 2D array. I would refer to it as a ragged 2D array, or more accurately as a 1D array of pointers to strings.
2

Of course your professor is right. An array named Arr of type T might look like

T Arr [] = { ... };

In this case, T is the type char *, and the brackets [] denote an array of char *s.

The difference relies on the fact that a pointer is not an array.

See the C FAQ for information on the differences.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.