Difference between revisions of "C"

From Wiki2
Line 1: Line 1:
===strcpy strcat===
<syntaxhighlight>
<syntaxhighlight>
char* str = "Hello";
char* str = "Hello";

Revision as of 23:57, 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>

  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>

dynamic arrays