One Dimensional Array Definition In C



A one-dimensional array is a list of variables with the same datatype, whereas the two-Dimensional array is 'array of arrays' having similar data types. 'C' do not have bound checking on arrays whereas, 'Java' have strict bound checking on arrays. The general syntax for declaring a one-dimensional array is. Type arrNamesize; where type could be any built-in type or user-defined types such as structures, arrName is a user-defined identifier, and size is an integer constant. Declaring an array (an array of 10 int variables in this case) is done like this: int array10.

¤ Home » Programming » C Tutorial » Arrays in C Part 1 of 2 - Basic array declaration and manipulation

An array is a data structure composed of a fixed number of components of the same type which are organized in a linear sequence. A component of an array is selected by assigning an integer value to its index (or subscript) which identifies the position of the component in the sequence. In C, arrays are intimately related to pointers. All sorts of array manipulations can also be performed by using pointers, which will be discussed later.


Array Declaration

The syntax of array declaration is as follows,

In the above, curly braces indicate optional parts of the declaration.

Example:int arr[10];

The above declaration defines an array called arr of size 10, i.e., arr consists of 10 elements of same data type int. The elements occupy consecutive cells in the memory (each cell houses one element) and forms an ordered set of elements. Each element can be identified by its position in the array, and is also referred as the subscript of the array. The first element is at position 0 and nth element can be found in the (n -1)th position.

The name of the array is arr, which contains the address of the first element (i.e. &arr[0]) of the array. However, an array name such as arr differs from an ordinary pointer variable (likeint *p;), because, it is static in nature and cannot point to a new memory location other than what it is pointing to by virtue of the declaration.


More examples of array declaration:

Array Manipulation

The most convenient way of performing array manipulation is to use the for repetitive construct (for loop) for accessing elements of the array.

Two

The following example illustrates the usage of an array in implementing addition of two vectors:


How to initialize array elements during declaration?

Array initialization can be performed in the following way:

Note, however, that this definition-cum-initialization is permitted only in case of external variable definition. To include such initialization statement inside a function, storage class clause static has to be used as a prefix. See examples below.


Describe the output generated by each of the following programs.

One Dimensional Array Definition In C

Exercises

One Dimensional Array Definition In C

Identify the errors in the following C program (if any) which initializes an array such that each of its ten elements is assigned with 0 value.

One Dimensional Array Definition In C

One Dimensional Array Definition In Chemistry

Identify the array defined in each of the following statements. Indicate what values are assigned to the individual array elements.


Write an appropriate array definition for each of the following cases:

  1. Define a one dimensional, integer array called A with 10 elements and initialize the array elements with 2, 5, 8, 11, ... , 29.
  2. Create a one dimensional, four element character array called object and assign the characters 'C', 'I', 'R', 'C', 'L' and 'E' to the array elements.
  3. Define a one dimensional, six element floating point array called flt_const having following initials values - 2.005, -3.05452, -1e-4, 340.179, 0.3e8, 0.023415

Lab work

Write a C program that will accept a line of text as input, store it in an array, and then print it backwards. Assume that the length of the string cannot exceed 80 characters, and while entering the data, the string will be terminated by a carriage return. Test the program by entering a suitable message.


Representing a string as a character array

A string constant, enclosed within a pair of double quotes, consists of 0 (empty string) or more characters terminated by a null ('0') character, which indicates the end of the string.

The following statement in C defines a string variable:

The above declaration allocates a 21 character space in memory which can store 20 characters each of one byte (with name[0] as the starting element). One extra byte (the last element) is used to store the null character. The null character, represented by 0 serves the purpose of indicating the end of string. The string name can be initialized during declaration, by a string constant.


Example:

The above program displays the content of the variable name i.e. Ratindra Nath Bhaskar and allows the user to accept a new name in the same variable, and finally comes out of the program displaying the new name on the screen. Notice the absence of the & operator as a prefix of the variable name in the scanf statement, which requires the address of the variable. This is because the variable name itself stores the address of the string variable. This will be more clear from the following figure which shows the representation of the array name in the memory.


The variable name is a static pointer which stores the address of the first element of the name array i.e &name[0]. A static pointer means it cannot be reassigned with a new value.

From the above discussion it is obvious that a string variable requires only the starting address of the memory location where the string constant is located, because the end of the string is always indicated by the null character (0).

To appreciate how this fact is utilized, look at the following example, where characters are read one by one from the console and stored in a characer array.


Passing array to a function

Malloc Two Dimensional Array C

An array name can be used as an argument to a function, thus permitting the entire array to be passed to the invoked function. The array name must appear by itself (without brackets or subscript) as an actual argument during the function call. The function's formal parameter is also written in the same manner, though it must be declared as an array within the formal parameter declaration. When a one dimensional array is used as a formal parameter, in the function declaration array name is written with a pair of empty square brackets. The size of an array is not specified within the formal parameter declaration. Since array name identifies the address of the first element, specifying array name as a parameter to a function essentially implements call by reference mode of parameter transfer.

Example:

A C program to read a set of numbers and sort them. It sorts a one dimensional integer array in ascending order.



Two Dimensional Array Example


Two Dimensional Array C Argument

Please enable JavaScript to view the comments powered by Disqus.comments powered by

C Language Two Dimensional Array

Disqus

C Foundation

What is C?C Compiler InstallationC ExtensionsC CompilerC InterpreterC Program Structure

C Basics

C KeywordsC Data TypesC IdentifiersC VariablesC ConstantC Escape SequencesC Constant and VolatileC Typecast

Operators

What is OperatorC Comma OperatorC Arithmetic OperatorsC Relational OperatorsC Logical OperatorsC Bitwise OperatorsC Conditional OperatorsC : : OperatorC Operator Priority

Basic IO's

Basic IO'sC Formatted FunctionsC Unformatted FunctionsC Common Functions

Control Statements

What is Control StatementC if StatementC if else StatementC Nested if StatementC Else if StatementC Break StatementC Continue StatementC Switch StatementC Goto Statement

Looping

What is Control LoopC for LoopC Nested for LoopC while LoopC Nested while LoopC do while LoopC Nested do while loop

Functions

What is FunctionC User Defined FunctionsC RecursionC Passing Parameters

Scope

ScopeC Local ScopeC Global Scope

Storage Classes

What is Storage ClassC AutoC ExternC StaticC Register

Array

What is ArrayC One Dimensional ArrayC Two Dimensional ArrayC Multi Dimensional ArrayC Arrays Of Strings

String

What is StringC String Functions

Pointer

What is PointerC Pointers ArithmeticC Pointer to PointerC Pointers and ArraysC Pointers and StringsC Pointer to FunctionsVoid PointersNull PointersC Null and Void Pointer

Structure

What is StructureC Struct within StructC Array within StructureC Pointer to StructureC Structure and FunctionC EnumC Bitfield StructureC Type def

Union

What is Union

Files

What is FileC read a fileC write a fileC File HandlingC Error HandlingC Low Level Disk I/OC Other file functions

Memory Allocation

What is Memory AllocationC Malloc()C Calloc()C Free()C Realloc()C Coreleft()

C Reference

All ASCII CodeBasic C Questions

C Interview

C Interview SetsAll Star PatternsAll Number PatternsAll Alphabet PatternsAll Series Patterns