Arrays

Pointer Arrays

#include <iostream>
#include <cstring>
using namespace std;

void mystrncpy(char* p, string s, unsigned int n){
    unsigned int siz;
    if ( s.size() < n)
        siz= s.size();
    else
        siz=n;

    unsigned int x;
    for (x=0; x<siz; x++)
    {
        //*(p+x)=s[x];
        p[x]=s[x];
    }
    p[x]='\0'; //null char terminated
}

int main(){
    char* c;
    c= new char[5];
    //mystrncpy(c,"abcde",4);
    strncpy(c,"abcdef",5-1);
    //strncpy(c,"abc",5-1);
    //in line above, can't use sizeof(c) as c is just a pointer
    cout <<"strlen(c): "<< strlen(c) <<endl;
    cout<<endl;
    printf("%s\n", c);
    cout << *c << endl; //first letter
    cout << c << endl; // cstring c

    delete[] c;
    return 0;
}

C code commented

#include <cstdio>  //for printf
#include <string.h> // for strncpy
//#include <stdlib.h> //for malloc and free
using namespace std;

int main() {
    //char y[6]="hello"; //need 6 for null byte
    char w[] ="world"; //6
    printf("%lu\n",sizeof(w));
    //const char* word = "hello";
    const char* fruits[] = {"banana", "apple", "pear", "orange", "kiwi"};
    char** f;
    f = new char* [5];  //(char**)malloc(5*8); //8 bytes/address (64 bit OS)
    int x;
    for (x = 0; x < 5; x++) 
    {
        f[x] = new char[8]; //(char*)malloc(8*1); // 1 byte per char
        strncpy(f[x], fruits[x], 8-1);
        printf("%s\n", f[x]);
    }

    for (x = 0; x < 5; x++) 
    {
        delete[] f[x]; //free(f[x]);
        f[x] = 0; //or in C++ nullptr
    }

    delete[] f; //free(f);
    f = 0;
    return 0;
}
#include <string.h>
#include <iostream>
//equivalence of 2D array and pointer pointers
using namespace std;
int main(){
    char m[3][6]; //stack
    char** M;
    M = new char*[3]; //heap
    for (int i=0; i<3; i++)
    {
        M[i] = new char[6];
    }
    int x,y;

    //assign data
    const char* a="cathy";
    const char* b="billy idol";
    const char* c="henry winkler";

    // pitfall of strncpy is it will not null terminate if source 
    // is bigger than dest
    strncpy(m[0], a, sizeof(m[0])-1);
    m[0][5]='\0';                    //null terminate C string
    strncpy(m[1], b, sizeof(m[1])-1);
    m[1][5]='\0';
    strncpy(m[2], c, sizeof(m[2])-1);
    m[2][5]='\0';

    // sizeof for an array and pointer are not the same

    strncpy(M[0], a, 5); //sizeof(M[0]) = 8 for 64 bit OS
    M[0][5]='\0';
    strncpy(M[1], b, 5);
    M[1][5]='\0';
    strncpy(M[2], c, 5);
    M[2][5]='\0';

    printf("%s\n",m[0]);   
    printf("%s\n",m[1]);
    printf("%s\n",m[2]);
    printf("%s\n",M[0]);
    printf("%s\n",M[1]);
    printf("%s\n",M[2]);
    
    while (true)
    {
        cout << "Enter coordinates of letter:(x=-1 to quit)" <<endl;
        cin >> x >> y;
        if (x==-1) break;

        cout << *( *(m+x)+y ) << endl; //equivalent lines
        cout << m[x][y] << endl;
        cout << *( *(M+x)+y ) << endl;
        cout << M[x][y] << endl;
    }
    for (int i=0; i<3; i++)
    {
        cout<< "Deleting: "<< M[i] <<endl;
        delete[] M[i];
    }
    delete[] M;
    return 0;
}