c_cpp C阵列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C阵列相关的知识,希望对你有一定的参考价值。

void process_2d_array_pointer(int (*array)[5][10]) {
  // ...
}
process_2d_array_pointer(&a);  // Use &a. a would decay to ptr to first elem.
// Definition and declaration
char dir[] = "/home/anni";
char http_request[BUFFER_SIZE];

// Length
int len = strlen(third);

// Split part of a string
char* first = strtok(string, " ");    // ' ' = delimiter
char* second = strtok(NULL, " ");
char* third = strtok(NULL, "\r"); 

// Extern global char array
extern char arr[];
extern char* arr;   // DOES NOT WORK!
void process_array(int (&array)[3]) {
  // ...
}

int a[3] = {1,2,3};
process_array(a);
template <size_t rows, size_t cols>
void process_2d_array_template(int (&array)[rows][cols]) {
  // ...
}

int a[2][3] = {{1,2,3}, {4,5,6}};
process_2d_array_template(a);  // OR: process_2d_array_template<2,3>(a);
void process_pointer_2_pointer(int **array, size_t rows, size_t cols) {
  // ...
}

int a[5][10] = { { } };
int *b[5];  // surrogate
for (size_t i = 0; i < 5; ++i) { 
  b[i] = a[i];
}
process_pointer_2_pointer(b, 5, 10);
void process_2d_array(int (*array)[10], size_t rows) {
  // ...
}
// OR
void process_2d_array(int array[][10], size_t rows) {
  // ...
}

process_2d_array(a, 5);
char arr[10];
printf( "%s\n", buffer );   // decayed into "pointer to char"
printf( "%p\n", &buffer );  // Not automatically decayed. Ptr to 10-element array of char
int *p = new int;
int *pa = new int[4];
int **pp = new int*[4];

delete p;
delete [] pa;
delete [] pp;

以上是关于c_cpp C阵列的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 阵列

c_cpp 384.随机播放阵列

c_cpp 53.最大子阵列

c_cpp 53.最大子阵列

c_cpp 最大乘积子阵列

c_cpp 反转阵列