BrainDump
  Home arrow BrainDump arrow Page 4 - C: More Operators and Arrays
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

C: More Operators and Arrays
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-19

    Table of Contents:
  • C: More Operators and Arrays
  • Bitwise Operators
  • Arrays
  • Two-Dimensional Arrays
  • Commenting on Your Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C: More Operators and Arrays - Two-Dimensional Arrays


    (Page 4 of 5 )

    Like most programming languages, C allows you to create two-dimensional and multi-dimensional arrays. A two-dimensional array works like a grid, or a spreadsheet, creating rows and columns to store data in. In the example below, the first number represents the number of rows, while the second number represents the number of columns:


    char name[2] [4];

    This creates a two-dimensional array with two rows and four columns. If we wanted to assign values to our array, we could do this:


    char name[2] [4] = {{'B','i','l','l'},

    {'D','i','n','g'}};

    This creates an array that contains the name Bill Ding. If I wanted to create two variables to hold the initials of Mr. Bill Ding, I could do so like this:


    char firstInitial;

    firstInitial = name[0][0];

    char lastInitial;

    lastInitial = name[1][0];

    Remember that arrays start at 0, so row 0, column 0 would equal "B" and row 1, column 0 would equal "D". We could also change a value in the array. Let's say we wanted to change the name to Bill Dung. We would have to access the "i" in Ding to do so:


    name[1] [1] = 'u';

    Note that the "i" in Ding is located in the second row and the second column. The n is located in the second row, third column, etc.

    Also note that we don't always need to set the size of an array, but it is good practice to do so.

    And finally, we can also set the values of a multidimensional array like this:


    char name[2] [4];

    name [0] [0] = 'B';

    name [0] [1] = 'i';

    name [0] [2] = 'l;

    name [0] [3] = 'l';

    name [1] [0] = 'D';

    name [1] [1] = 'i';

    name [1] [2] = 'n';

    name [1] [3] = 'g';

    There are also ways to Loop through arrays and ways to create dimensional arrays larger than two dimensions, but those are beyond our scope (we will cover looping through arrays in a future article, however, when we discuss loops).

    More BrainDump Articles
    More By James Payne


       · Thanks for dropping by to read my article on C Operators and Arrays. In this episode...
     

    BRAINDUMP ARTICLES

    - XAML Brushes and Silverlight
    - Silverlight and XAML Basics
    - Immortal XP
    - XAML Basics
    - Microsoft Surface
    - Making Your First Active X Control in Visual...
    - Codes and Packages in Microsoft Project 2007
    - Windows 7: Rumors and Demos
    - XP SP3 Why Me?
    - Breaking Up Your Work in Microsoft Project
    - Breaking Work into Task-Sized Chunks
    - Putting Microsoft`s Worldwide Telescope Unde...
    - Handling Multiple Contracts with Indigo
    - Cleaning Out Your Data in XP
    - Multiple Service Contracts and Indigo





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway