Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - String arrays

Node:String arrays, Next:, Previous:Initializing strings, Up:Strings



String arrays

Suppose you want to print out a screenful of text instead of a single line. You could use one long character array, interspersed with \n characters where you want the lines to break, but you might find it easier to use a string array. A string array is an array of strings, which, of course, are themselves arrays of characters; in effect, a string array is a two-dimensional character array.

Just as there are easy methods of initializing integer arrays, float arrays, strings, and so on, there is also an easy way of initializing string arrays. For example, here is a sample program which prints out a menu for a full application program. That's all it does, but you might imagine that when the user chooses 3 in the full program, the application invokes the function calculate_bill we examined earlier. (See Parameters.)

#include <stdio.h>

char *menu[] =
{
  "  -------------------------------------- ",
  " |            ++ MENU ++                |",
  " |           ~~~~~~~~~~~~               |",
  " |     (0) Edit Preferences             |",
  " |     (1) Print Charge Sheet           |",
  " |     (2) Print Log Sheet              |",
  " |     (3) Calculate Bill               |",
  " |     (q) Quit                         |",
  " |                                      |",
  " |                                      |",
  " |     Please enter choice below.       |",
  " |                                      |",
  "  -------------------------------------- "
};


int main()
{
  int line_num;

  for (line_num = 0; line_num < 13; line_num++)
    {
      printf ("%s\n", menu[line_num]);
    }

  return 0;
}

Notice that the string array menu is declared char *menu[]. This method of defining a two-dimensional string array is a combination of methods 1 and 2 for initializing strings from the last section. (See Initializing strings.) This is the most convenient method; if you try to define menu with char menu[][], the compiler will return an "unspecified bounds error". You can get around this by declaring the second subscript of menu explicitly (e.g. char menu[][80]), but that necessitates you know the maximum length of the strings you are storing in the array, which is something you may not know and that it may be tedious to find out.

The elements of menu are initialized with string constants in the same way that an integer array, for example, is initialized with integers, separating each element with a comma. (See Initializing arrays.)

 
 
  Published under free license. Design by Interspire