Omar Khaled
Omar Khaled

Reputation: 25

Array declaration

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.

Upvotes: 1

Views: 152

Answers (2)

smiling_nameless
smiling_nameless

Reputation: 1057

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.

Upvotes: 2

Beta
Beta

Reputation: 99172

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".

Upvotes: 4

Related Questions