Difference between revisions of "C"

From Wiki2
Line 10: Line 10:


===[http://faculty.edcc.edu/paul.bladek/c_string_functions.htm c (char) string functions]===
===[http://faculty.edcc.edu/paul.bladek/c_string_functions.htm 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>

Revision as of 23:39, 21 February 2013

<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>

  1. include <iostream>
  2. 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>