Difference between revisions of "C"
From Wiki2
| Line 60: | Line 60: | ||
===[http://www.cs.hofstra.edu/~cscccl/csc145/array2d.c dynamic arrays]=== | ===[http://www.cs.hofstra.edu/~cscccl/csc145/array2d.c dynamic arrays]=== | ||
===[http://rosettacode.org/wiki/Create_a_two-dimensional_array_at_runtime creating 2D arrays at runtime]=== | |||
Revision as of 23:33, 21 February 2013
strcpy strcat
<syntaxhighlight> char* str = "Hello"; char dest[12];
strcpy( dest, str ); strcat( dest, ".txt" ); </syntaxhighlight>
arrray vs pointer
c (char) string functions
multidim char arrays
<syntaxhighlight>
- include <iostream>
- include <cstring>
using namespace std;
int main(int argc, char *argv[]) {
unsigned i;
// Declaration of the two-dimensional array // as a pointer to pointer // char** array_2D; unsigned ROWS = 10; unsigned COLUMNS = 10;
// Allocate "main" array // array_2D = new char*[ROWS];
// Allocate each member of the "main" array
//
for (i = 0; i < ROWS; ++i)
array_2D[i] = new char[COLUMNS];
// Fill the 6th element with a string and // print it out // strcpy(array_2D[5], "Hey there"); cout << array_2D[5] << endl;
// Deletion is performed in reversed order.
// Pay special attention to the delete[]
// operator which must be used to delete
// arrays (instead of the "simple" delete)
//
for (i = 0; i < ROWS; ++i)
delete[] array_2D[i];
delete[] array_2D;
return 0;
} </syntaxhighlight>