How to Create an Array in C

Arrays offer the most efficient method for storing lists of data in C. They're very easy for the programmer to create and use, and they're remarkably quick for the computer to access and update. However, they can waste a lot of memory, so they're not always the best thing to use. When not used carefully, they can also cause crashes, bugs and system instability.

Instructions

  1. Create an Array in C

    • 1

      Understand that every element of an array (or any other kind of list) will be the same kind of data. For example, a list of test scores will be an array of integers, since each test score is an integer. For more complex data, you will need to define a structure.

    • 2

      Decide what the largest size your array can ever reach will be, because arrays have a fixed length. You'll want to strike a balance between having your program able to handle unusually long data sets, and having it require (and waste) a lot of memory.

    • 3

      Create the array the same way you would create a single variable, but add the maximum size in square brackets after the name, as in these examples:

      int test_scores[50];
      char first_name[20];
      employee_record employees[1000];
    • 4

      Create a second variable that keeps track of how many elements you've added to the array so far.

    • 5

      Create an array with multiple dimensions simply making an array of arrays, like this:

      int test_answers[10][20];
      char student_names[50][20];

    Use the Array

    • 6

      Access the array's elements using the index in square brackets, like this:

      test_scores[12] = 50;
      printf("Test score: %d\n", test_scores[i]);
    • 7

      Pass arrays to functions, if you so choose. You don't need to (and should not) specify the size in the function definition. This is what the function definition and call would look like:

      function subtotal(int test_scores[]);
      test_subtotal = subtotal(test_scores);

Tips & Warnings

  • The index of an array is always of type int.

  • A character string is actually an array of characters, and all the string-handling functions built into C's standard libraries work with these arrays.

  • When passing an array to a function, changes made to the array by the function will carry over to the calling function!

  • Note that the first element of an array is element 0, not element 1. That means if your array has 50 elements, they are numbered 0 to 49.

  • Accessing elements past the last one you declared is called a "buffer overflow" and is the cause of the lion's share of bugs and crashes in C programs. C offers no protection against buffer overflows. Instead, it's up to you to avoid it through careful programming.

  • The array's size must be a constant, and it can't easily be changed while the program runs. If you need to be able to resize the list as you go, consider using a linked list instead.

Related Searches:

Comments

You May Also Like

Related Ads

Featured