message "excess elements in array initializer"

Thread Starter

King2

Joined Jul 17, 2022
163
I have declared array that can store six numbers. I've one message while trying to compile program in C language. look's like I'm storing more numbers than array length. I don't understand why red mark is showing before number five

1661003281307.png
 

Thread Starter

King2

Joined Jul 17, 2022
163
How to identify cause of problems in line 5, col 68, 78, and 89?

1661008699352.png

source code
C:
#include<stdio.h>

int main()
{
   int array[4][3][2]  = { {10, 20} ,{30, 40}, {50, 60}, {70, 80}, {90, 100},{110, 120},{130, 140}, {150, 160}, {170, 180}, {190, 200}, {210,220}, {230, 240} };

  return 0;
}
 
Last edited:

MrChips

Joined Oct 2, 2009
30,821
You are trying to define a multi-dimensional array:
array[4][3][2]

Think of this as 4 tables, 3 rows, 2 columns.
Create 4 tables first:
int array[4][3][2] = { { }, { }, { }, { } };

Then add the rest. Each table has 3 rows:
{ { }, { }, { } }

Each row has 2 columns:
{ { }, { } }

You could state it in one array and see how it turns out:
int array[4][3][2] = { 1, 2, 3, 4, 5, 6, ... etc };
 

WBahn

Joined Mar 31, 2012
30,071
I have declared array that can store six numbers. I've one message while trying to compile program in C language. look's like I'm storing more numbers than array length. I don't understand why red mark is showing before number five

View attachment 274283
C (and most, but not all languages) store arrays in "row major" order. So { {row 0}, {row 1}, {row 2} }. Therefore your initializer is for an array with 3 rows and 2 columns, but your declaration is for 2 rows and 3 columns.

The general recommendation when providing an initializer is to not give explicit dimensions.

int array[][] = {{1,2}, {3,4}, {5,6}};

The initializer provides the needed information. If you also provide explicit dimensions, then your code is "over-defined", meaning that there is more than one way to determine the same value. What if those two ways don't agree? This might come about when you changed the initializer (to add another row, for instance) and forgot to change the dimensions. This can cause either an error or perhaps very-difficult-to-find run-time issues.

But a counter-argument can be made that, as is possibly the case here, if you meant the array to have the dimensions explicitly given and messed up the organization of the initializer, that you've given the compiler a means of letting you know.
 
Last edited:

Thread Starter

King2

Joined Jul 17, 2022
163
The general recommendation when providing an initializer is to not give explicit dimensions.

int array[][] = {{1,2}, {3,4}, {5,6}};
I don't understand this paragraph, can you please clarify. You have initialized the array but you have not defined the array index. My guess is that if index is not defined, the compiler will allocate default memory for the array elements. but what will be the advantage
 

WBahn

Joined Mar 31, 2012
30,071
I don't understand this paragraph, can you please clarify. You have initialized the array but you have not defined the array index. My guess is that if index is not defined, the compiler will allocate default memory for the array elements. but what will be the advantage
The advantage is that the compiler will allocate the proper amount of memory for the array initializer without you having to count up the rows and columns and then keep updating the dimensions as you make changes to the initializer.
 

Thread Starter

King2

Joined Jul 17, 2022
163
You are trying to define a multi-dimensional array:
array[4][3][2]

Think of this as 4 tables, 3 rows, 2 columns.
Create 4 tables first:
int array[4][3][2] = { { }, { }, { }, { } };
In three dimensional array we have tables, rows and columns

If I add another index,

int array[5][4][3][2];

Now it has become a 4 dimensional array, what does [5] represent?
 

nsaspook

Joined Aug 27, 2009
13,306
Last edited:

WBahn

Joined Mar 31, 2012
30,071
In three dimensional array we have tables, rows and columns

If I add another index,

int array[5][4][3][2];

Now it has become a 4 dimensional array, what does [5] represent?
There's no set terminology (even 'tables', 'rows', and 'columns' are not fixed in stone). Some common terms for higher dimensions are chapters, books, volumes, collections. This is drawing upon how large sets of textual material might be organized in a library.

But, in general, you use what makes sense for the application.

Image a 2D array containing data about the number of school age kids in a school district. Reach row is a different school while one column is used for each grade level.

Since counties can have more than one school district, you add a dimension such that the first dimension is the index of the district within a county.

But states have more than one county. So another index might be the index of the county.

But countries have more than one state. So the next index might be the index of the state.

Another index might indicate which country.

At some point, another index might indicate which planet.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,821
The words are just representative of how humans prefer to visualize objects.
1-dimensional array can be viewed as a linear string of values.
2-dimensional array is a flat spread sheet of rows and columns.
3-dimensional array is a stack of spread sheets.
How can we visualize a 4, 5 or n-dimensional array?
It gets difficult for the human mind to visualize n-dimensional space.

You use whatever model works for you. For example:

4th dimension is file folders.
5th dimension is cabinet drawers.
6th dimension is file cabinets.
7th dimension is office rooms
8th dimension is office floors
9th dimension is office buildings
10th dimension is branch offices
etc.
 

WBahn

Joined Mar 31, 2012
30,071
I don't find the reason of warning message in line 8, 12 and 16. What's wrong with that View attachment 274342
Your initializer is for a four dimensional array since you have four nested levels of curly braces.

The innermost dimension is 1 because your innermost initializer group only has one element.

Declare your array as

int array[4][3][2][1] =

and your warning will go away.

The compiler was able to figure out what you meant (or likely meant) and made the adjustment, but it threw the warning to let you know that it was doing so.

The language spec says that scalar initializers can optionally be surrounded by curly braces, so the following is perfectly legal:

int k = {42};

But when initializing something like an array, the compiler has to decide whether to interpret the initializer as being for a three-dimensional array in which the innermost dimension happens to have the individual values enclosed with braces, or whether to interpret it as a four dimensional array in which the innermost dimension is a vector of length one. The variable declaration breaks the ambiguity and the compiler is just informing you about which interpretation it chose in case that wasn't the one you meant.

To see this a bit more clearly, consider the following:

int m1[3] = { 1, 2, 3};
int m2[3] = { {1}, {2}. {3} };

Do you see that m2 is what you are basically doing?

Now consider the following:

int n[3][2] = { {1, 11}, {2, 22}, {3, 33} };

Now let's say that you wanted to get rid of the second column. You could do that as:

int n[3][1] = { {1}, {2}, {3} };

But do you see that this is exactly the same initializer that was used for m2?
 

Thread Starter

King2

Joined Jul 17, 2022
163
Sorry. It’s my error in the instructions I provided.
You don’t need braces around the values.
It's fine now, no warning

C:
#include<stdio.h>

int main()
{
   int array[4][3][2]  =
   {
      {
         { 1, 2}, {3, 4}, {5, 6}
      }, 
      
      {
         { 7, 8}, {9, 10}, {11, 12}
      },
      
      {
         { 13, 14}, {15, 16}, {17, 18}
      },
      
      {
         { 19, 20}, {21, 22}, {23, 24}
      
      }
   };

  
   return 0;
}
 

Thread Starter

King2

Joined Jul 17, 2022
163
But when initializing something like an array, the compiler has to decide whether to interpret the initializer as being for a three-dimensional array in which the innermost dimension happens to have the individual values enclosed with braces, or whether to interpret it as a four dimensional array in which the innermost dimension is a vector of length one. The variable declaration breaks the ambiguity and the compiler is just informing you about which interpretation it chose in case that wasn't the one you meant.
I think now I have learned enough to declare and initialize multidimensional array in c language

C:
#include<stdio.h>

int main()
{
   int array[4][3][2][2]  =
   {
      {
         { {1, 1}, {1, 2}}, {{1,3}, {1,4}}, {{1,5}, {1, 6}}
      }, 
      
      {
         { {1, 7}, {1, 8}}, {{1, 9}, {1, 10}}, {{1, 11}, {1, 12}}
      },
      
      {
         { {1, 13}, {1, 14}}, {{1,15}, {1, 16}}, {{1, 17}, {1, 18}}
      },
      
      {
         { {1, 19}, {1, 20}}, {{1, 21}, {1, 22}}, {{1, 23}, {1, 24}}
      
      }
   };
   return 0;
}
 

Thread Starter

King2

Joined Jul 17, 2022
163
This is slightly different question from thread title but I am curious to know the reason

My experiment show that I can't use same variable name with different data type. Can you tell us what's the reason behind this, why compiler doesn't allow same name with different data type

1661068176286.png
 

WBahn

Joined Mar 31, 2012
30,071
So if this were allowed, what would the value of 'x' be after the statement

x = x + 1;

Which x is being referred to?

At any given point in the code, a given identifier can only refer to one thing, otherwise the compiler may not be able to determine which thing it is referring to when it is used.
 

Thread Starter

King2

Joined Jul 17, 2022
163
At any given point in the code, a given identifier can only refer to one thing, otherwise the compiler may not be able to determine which thing it is referring to when it is used.
Thanks for clearing the doubts. I wrote code with two storage classes and one variable. Experiment show that we cannot have two storages with one variable.

Why we can't have two storages with one variable. What is the purpose of the C language creators behind this rule?

I think the reason is that if there are two storage classes then the compiler will get confused where to store the variable
 
Top