C Tutorial
Estimated reading: 4 minutes 446 views

C Arrays & Strings โ€“ Handling Structured Data in C


Introduction โ€“ Managing Data Collections with Arrays & Strings

Arrays and strings are fundamental to managing groups of values and textual data in C programming. Arrays allow you to store multiple items of the same type, while strings help handle character sequences efficiently.

In this guide, youโ€™ll learn:

  • How to declare and use arrays (1D, 2D, VLA)
  • How to work with strings and arrays of strings
  • How to pass arrays to functions
  • How special characters enhance string formatting

Topics Covered

Topic Description
C Arrays OverviewDeclaring, accessing, and initializing arrays
C Multi-Dimensional ArraysWorking with 2D and 3D matrices
C Arrays with FunctionsPassing and returning arrays from functions
C Variable Length ArraysArrays with dynamic size at runtime
C Strings OverviewString declaration, manipulation, and I/O
C Array of StringsHandling lists of strings (char arrays)
C Special Characters in StringsNewlines, tabs, and escape characters

C Arrays Overview

Arrays in C store multiple values of the same data type under a single name.

Syntax:

data_type array_name[size];

Example:

int numbers[5] = {1, 2, 3, 4, 5};
printf("%d", numbers[2]); // Outputs 3

Arrays are zero-indexed. The first element is at index 0.


C Multi-Dimensional Arrays

Used to create matrices or tables.

Syntax (2D Array):

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

Accessing:

printf("%d", matrix[1][2]); // Outputs 6

C supports higher dimensions like int arr[2][3][4];


C Arrays with Functions (pass/return)

You can pass arrays to functions by passing their base address.

Passing an array:

void display(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
}

Return an array using pointer or dynamic memory:

int* generateArray() {
    static int arr[3] = {10, 20, 30};
    return arr;
}

๐Ÿšจ Note: Avoid returning local (non-static) arraysโ€”they lead to undefined behavior.


C Variable Length Arrays (VLAs)

Available from C99, VLAs let you define arrays with sizes determined at runtime.

Example:

int n;
scanf("%d", &n);
int arr[n];  // VLA

VLAs canโ€™t be initialized at the time of declaration and are stack-allocated.


C Strings Overview

Strings in C are arrays of characters ending with a null character (\0).

Declaration:

char name[] = "Alice";

Accessing:

printf("%c", name[1]);  // Outputs 'l'

string.h provides functions like strlen(), strcpy(), strcmp(), etc.


C Array of Strings

Useful for managing lists of strings like names or menu items.

Syntax:

char names[3][10] = {"John", "Amy", "Sam"};

Access:

printf("%s", names[1]);  // Outputs "Amy"

Alternatively, use:

char *names[] = {"John", "Amy", "Sam"};

C Special Characters in Strings

Escape sequences in strings format output or include non-printable characters.

Escape SequenceDescription
\nNew line
\tHorizontal tab
\\Backslash
\"Double quote
\0Null terminator

Example:

printf("Hello\tWorld\n");

Every string ends with \0 even if itโ€™s not shown explicitly.


Summary โ€“ Recap & Next Steps

Arrays and strings make it easier to handle structured and sequential data in C. Whether you’re creating a dynamic array, manipulating string values, or building a 2D matrixโ€”these tools are essential to master.

Key Takeaways:

  • Arrays store multiple elements of the same type under one name
  • Strings are char arrays ending with \0
  • VLAs allow runtime-defined arrays (C99+)
  • Functions can accept and return arrays
  • Use array of strings for structured lists
  • Escape sequences improve string formatting

Real-World Relevance:
Used in buffer manipulation, data tables, command parsing, file input/output, menu systems, and embedded devices.


Frequently Asked Questions (FAQ)


Can I return an array from a function in C?
Not directly, but you can return a pointer to a static or dynamically allocated array.


What happens if I exceed array bounds in C?
Youโ€™ll invoke undefined behavior. C doesnโ€™t perform automatic bounds checking.


What’s the difference between a string and a character array?
Strings are character arrays with a null terminator \0, used by string functions like strlen().


Are variable length arrays safe?
VLAs are stack-allocated. They are safe if the size is not too large and supported by the compiler.


How do I copy one string to another?
Use strcpy() from string.h:

strcpy(dest, source);

Can I use sizeof on a VLA?
No. sizeof doesnโ€™t work on VLAs at runtime.


Share Now :
Share

๐Ÿ—ƒ๏ธ C Arrays & Strings

Or Copy Link

CONTENTS
Scroll to Top