0% found this document useful (0 votes)
42 views2 pages

Arrays and Strings in C Notes

The document provides an overview of arrays and strings in C programming, covering topics such as declaring arrays, accessing elements, using loops for processing, passing arrays to functions, and performing searching and sorting operations. It also discusses multidimensional arrays, string basics, string library functions, and arrays of pointers. Examples are included to illustrate each concept effectively.

Uploaded by

Sateesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Arrays and Strings in C Notes

The document provides an overview of arrays and strings in C programming, covering topics such as declaring arrays, accessing elements, using loops for processing, passing arrays to functions, and performing searching and sorting operations. It also discusses multidimensional arrays, string basics, string library functions, and arrays of pointers. Examples are included to illustrate each concept effectively.

Uploaded by

Sateesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Arrays and Strings in C

1. Declaring and Referencing Arrays


An array is a collection of elements of the same data type stored in contiguous memory
locations. Each element can be accessed using its index number (subscript).
Syntax: data_type array_name[size]; Example: int marks[5]; // Declares an array of 5
integers
2. Array Subscripts
Array elements are accessed using subscripts (index numbers). The first element starts at
index 0.
Example: marks[0] = 90; marks[1] = 85; printf("%d", marks[0]); // Prints 90
3. Using for Loops for Sequential Access
Arrays are often processed using for loops to access each element sequentially.
Example Program: #include <stdio.h> int main() { int marks[5] = {80, 85, 90, 95, 100};
for(int i = 0; i < 5; i++) { printf("%d\n", marks[i]); } return 0; }
4. Using Array Elements as Function Arguments
Individual array elements can be passed to functions like normal variables.
Example: void display(int x) { printf("%d", x); } int main() { int a[3] = {10, 20, 30};
display(a[1]); // Passes the value 20 return 0; }
5. Array Arguments
Entire arrays can be passed to functions by passing the array name (which is the base
address).
Example: void display(int arr[], int n) { for(int i = 0; i < n; i++) printf("%d ", arr[i]); } int main()
{ int a[3] = {10, 20, 30}; display(a, 3); return 0; }
6. Searching and Sorting an Array
Searching and sorting are common array operations. Linear search and Bubble sort are
simple examples.
Example (Linear Search): #include <stdio.h> int main() { int a[5] = {10, 20, 30, 40, 50}, key
= 30, found = 0; for(int i = 0; i < 5; i++) { if(a[i] == key) { found = 1; printf("Element found at
position %d", i+1); break; } } if(!found) printf("Element not found"); return 0; }
7. Parallel Arrays and Enumerated Types
Parallel arrays are multiple arrays of the same length, where corresponding elements are
related.
Example: char names[3][10] = {"Ram", "Raj", "Ravi"}; int marks[3] = {85, 90, 95}; for(int i=0;
i<3; i++) printf("%s scored %d\n", names[i], marks[i]);
8. Multidimensional Arrays
Multidimensional arrays store data in table-like (matrix) form. Most common is the 2D
array.
Example: #include <stdio.h> int main() { int a[2][3] = {{1,2,3},{4,5,6}}; for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) { printf("%d ", a[i][j]); } printf("\n"); } return 0; }
9. String Basics
A string is an array of characters ending with a null character ('\0').
Example: char name[10] = "Hello";
10. String Library Functions: Assignment and Substrings
Strings cannot be assigned directly using '='. Instead, use library functions like strcpy() and
strncpy() from .
Example: #include <string.h> strcpy(dest, src); // Copies src to dest strncpy(dest, src, n); //
Copies first n characters
11. Longer Strings: Concatenation and Whole-Line Input
Use strcat() for concatenation and gets()/fgets() for input.
Example: #include <stdio.h> #include <string.h> int main() { char s1[20] = "Hello "; char
s2[10] = "World"; strcat(s1, s2); printf("%s", s1); return 0; }
12. String Comparison
strcmp() function compares two strings lexicographically.
Example: #include <string.h> int result = strcmp("abc", "abd"); // Returns negative since 'c'
< 'd'
13. Arrays of Pointers
An array of pointers can store addresses of strings efficiently.
Example: #include <stdio.h> int main() { char *names[] = {"C", "C++", "Python", "Java"};
for(int i=0; i<4; i++) printf("%s\n", names[i]); return 0; }

— End of Notes —

You might also like