C Language Notes: 2D Arrays, Strings, and Functions
1. Two-Dimensional Integer Arrays
A two-dimensional array is a matrix-like data structure composed of multiple one-dimensional arrays.
It’s mainly used to store tabular or planar data.
1.1 Definition
// Define a 2D array with 2 rows and 3 columns
int a[2][3];
// Fully initialized
int a[2][3] = {{1,2,3},{4,5,6}};
int b[2][3] = {1,2,3,4,5,6};
// Partially initialized
int a1[2][3] = {1,2,3,4}; // Uninitialized elements are set to 0
int b2[2][3] = {{1,2},{3}};
int c1[2][3] = {0}; // All elements are initialized to 0
int c2[2][3] = {{0}};
Notes:
- Both row and column numbers must be integer constants.
- The data type cannot be
void. - You can omit the row number, letting the compiler infer it.
1.2 Accessing Array Elements
int a[2][3]; // a has 2 elements, each being a 1D array of length 3
// Memory layout:
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
// Type relations:
a -> int [][3]
a[0] -> int []
a[0][0]-> int
1.3 Characteristics
| Property | Description |
|---|---|
| Uniformity | All elements share the same data type |
| Continuity | Stored in contiguous memory blocks |
| Orderliness | Elements are indexed in order |
A two-dimensional array is essentially a collection of multiple one-dimensional arrays.
1.4 Size and Dimension Calculation
int array_test[2][3] = {0};
int row = sizeof(array_test) / sizeof(array_test[0]); // number of rows
int col = sizeof(array_test[0]) / sizeof(array_test[0][0]); // number of columns
int size = sizeof(array_test); // total bytes
printf("size:%d row:%d col:%d\n", size, row, col);
1.5 Traversing a 2D Array
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int arr[2][3] = {0};
int row = sizeof(arr)/sizeof(arr[0]);
int col = sizeof(arr[0])/sizeof(arr[0][0]);
srand(time(NULL));
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
arr[i][j] = rand() % 50;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
printf("row:%d, col:%d, value:%d\n", i, j, arr[i][j]);
return 0;
}
2. Two-Dimensional Character Arrays
Two-dimensional character arrays are often used to store multiple strings.
2.1 Definition and Initialization
char str[2][100] = {"hello", "world"};
char str[5][32] = {{"hello"}, {"world"}, {"1234"}, {"how"}, {"you ###"}};
char str2[5][32] = {"hello","world","1234","how","you ###"};
char str3[5][32] = {0};
char str4[5][32] = {{0}};
char str5[][32] = {"hello","world","how","are","you"};
2.2 Access and Memory Layout
str -> char [][100]
str[0] -> char [100]
str[0][0] -> 'h'
str[1][0] -> 'w'
sizeof(str) = 2 * 100 * sizeof(char) = 200 bytes
2.3 Common String Library Functions
| Function | Description |
|---|---|
strcpy(dest, src) | Copy string |
strcmp(s1, s2) | Compare strings (returns 0 if equal) |
strcat(dest, src) | Concatenate strings |
strlen(str) | Get string length |
2.4 Manual String Comparison Logic
int i = 0;
int ret = 0;
while(1) {
if(str[i] == str2[i] && str[i] != 0) {
i++;
} else {
ret = str[i] - str2[i];
break;
}
}
printf("ret %d\n", ret);
Equivalent built-in function:
int ret = strcmp(str, str2);
2.5 Input and Output Example
#include<stdio.h>
#include<string.h>
int main() {
char str_test[3][100] = {0};
for(int i=0;i<3;i++) {
printf("input str %d:\n", i+1);
gets(str_test[i]);
}
for(int i=0;i<3;i++) {
printf("str %d, content: %s\n", i+1, str_test[i]);
}
return 0;
}
3. Functions in C
Functions are the basic building blocks of modular programming, allowing large problems to be decomposed into smaller, reusable components.
3.1 Definition and Usage
Syntax:
type function_name(type param1, type param2, ...) {
// function body
return expression;
}
Example:
int add(int a, int b) {
int c = a + b;
return c;
}
3.2 Function Declaration and Invocation
int add(int, int); // Declaration
int main() {
int a = 10, b = 20;
int c = add(a, b);
printf("%d\n", c);
return 0;
}
Key Points:
- A declaration is required if the definition comes after its use.
- Return type cannot be an array.
- If not explicitly defined, the return type defaults to
int. - Any code after
returnwill not execute.
3.3 Example: Basic Function
#include<stdio.h>
int add(int a,int b){
int c = a + b;
return c;
}
int main(){
int a=10, b=20;
int c=add(a,b);
printf("%d\n", c);
return 0;
}
3.4 Example: Finding the Maximum Value
#include<stdio.h>
int getmax(int a,int b) {
return a >= b ? a : b;
}
int main() {
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n", getmax(a,b));
return 0;
}
3.5 Example: Printing Multiples of 3 (1–100)
#include<stdio.h>
int isThreeTimes(int a) {
if (a % 3 == 0) return 1;
return 0;
}
int main() {
for(int i=1;i<=100;i++)
if(isThreeTimes(i))
printf("%d ", i);
return 0;
}
806

被折叠的 条评论
为什么被折叠?



