I was copying a c++ program and i came tosee something like this: char a[10][10]= {....}
What does a[10][10] means?? what does the symbol [] do? Pls help me...

Recommended Answers

All 3 Replies

[] means how many spots you will get in the char veriable. this is called an array. what you just saw was a 2d array.

char username[10]

that line of code means you can now store 10 chars in the username veriable.
this is useful when you want to store more then one char.

a 2d array is pretty much the same but now it stores one in the first slot and one in the second slot. so you could use it to keep track of a tic tac toe game for instance.

here is an full example using the comand line on how to use a single array
this example makes an array with int. you can make an array with any legal syntax for storing veriables.

#include <iostream>

using namespace std;

int main(){
	int Array[4]; //declares a veriable called Array with 4 slots

	cout << "Enter a number: ";
	cin >> Array[0]; // you must always remeber that it starts at 0 and not 1

	cout << "Enter a second number: ";
	cin >> Array[1];

	cout << "Enter a third number: ";
	cin >> Array [2];

	Array[3] = Array[0]+Array[1]+Array[2]; // this means that you will only have to Array[3] since it starts at 0

	cout << "The sum is: " << Array[3] << "\n";//display whats stored in Array[3]. that is what was just done above.
	cin.ignore(); //ignores the enter so i can use the cin.get()

	cin.get(); //waites for user to press enter to close the programe
}

and here is an example i found on http://www.cprogramming.com/tutorial/lesson8.html on a 2d array

#include <iostream>

using namespace std;

int main()
{
  int x;
  int y;
  int array[8][8]; // Declares an array like a chessboard
  
  for ( x = 0; x < 8; x++ ) {
    for ( y = 0; y < 8; y++ )
      array[x][y] = x * y; // Set each element to a value
  }
  cout<<"Array Indices:\n";
  for ( x = 0; x < 8;x++ ) {
    for ( y = 0; y < 8; y++ )
      cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
    cout<<"\n";
  }
  cin.get();
}

This [] type of symbol is used in arrays and using it twice ([][]) called two dimensional arrays and using thrice ([][][]) called three dimensional array.
by using arrays one can save his unnecessary efforts by declaring extra variables.....
for eg: you can add 10 numbers by declaring 10 different variables
(just an eg: for a very beginner)
(int v1,v2,v3.....,v9,v10) but it will takes your lots of time and extra effort you can also do it by using a simple and reliable declaration int v[10]. This declaration means now you can store 10 integers in memory. you can use loops for easy initialization for eg:

for(i=0;i<=9;i++)
scanf("%d",&v[i]);

now again look up at your problem....

char a[10][10]= {....}

this means you declare a two dimensional array having 10 columns and 10 rows which will be assign at run time (may possible at compile time)

= {....}

you can initialize any array at compile time as well as run time depends on your program.
i hope you understand this topic little bit by reading this......
for more details you must go though deep study of books and spend time on your system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.