Some C++ help: Character arrays, pointers, and strings, oh my.

25 Sep 2011

Ok, including my sample code below.

What I'm ultimately working on: I need to pass a string to a function, and be able to find the size of the string, and add the characters to a character array. I'm working on an I2C OLED driver... so trying to read in a string from one source, and pass it out to the display driver.

Where I'm stumbling... If I have a char * mystring then sizeof(mystring) should equal sizeof(pointer). However, shouldn't sizeof(&mystring) return the size of the contents of the string?

The code below also has a little snippet to make the led's on the header act as a binary out. ;)

Where I'm doing my testing is commented, for your pleasure.

#include "mbed.h"
DigitalOut myled1(LED1);DigitalOut myled2(LED2);DigitalOut myled3(LED3);DigitalOut myled4(LED4);

void report (int number);

int test_1(char * mypointer);
int test_2(char mystring[]);
int test_3(char mystring);

int main() {
    char * characterstringpointer = "abcdefghij";
    char characterstring[] = "abcdefghij";
    while (1) {

        // A pointer, I expect 4 (pointer = int = 4 bytes)
        report(sizeof(characterstringpointer)); 
        // I get 4.

        // Data at pointer, I expect 11 (10 + \0)
        report(sizeof(&characterstringpointer));
        // I get 4?!

        // Returns sizeof data at pointer, I expect 11 (10 + \0)
        report(test_1(characterstringpointer));
        // I get 4?!

        // Returns sizeof raw character array, I expect 11 (10 + \0)
        report(sizeof(characterstring));
        // I get 11

        // Tries to pass the array as an array then get sizeof, I expect 11 (10 + \0)
        report(test_2(characterstring));
        // I get 4?! -- This one is 'bad' but testing for completeness

        // Tries to pass as a 'char', which should act as a string, then get sizeof, I expect 11 (10 + \0)
        report(test_2(characterstring));
        // I get 4?!
        
        wait(5);
    }
}

int test_1(char * mypointer) {
    return(sizeof(&mypointer));
}
int test_2(char mystring[]) {
    return(sizeof(mystring));
}
int test_3(char mystring) {
    return(sizeof(mystring));
}
void report (int number) {
    switch (number) {
    case 0:  myled1 = 0;myled2 = 0;myled3 = 0;myled4 = 0; break;
    case 1:  myled1 = 0;myled2 = 0;myled3 = 0;myled4 = 1; break;
    case 2:  myled1 = 0;myled2 = 0;myled3 = 1;myled4 = 0; break;
    case 3:  myled1 = 0;myled2 = 0;myled3 = 1;myled4 = 1; break;
    case 4:  myled1 = 0;myled2 = 1;myled3 = 0;myled4 = 0; break;
    case 5:  myled1 = 0;myled2 = 1;myled3 = 0;myled4 = 1; break;
    case 6:  myled1 = 0;myled2 = 1;myled3 = 1;myled4 = 0; break;
    case 7:  myled1 = 0;myled2 = 1;myled3 = 1;myled4 = 1; break;
    case 8:  myled1 = 1;myled2 = 0;myled3 = 0;myled4 = 0; break;
    case 9:  myled1 = 1;myled2 = 0;myled3 = 0;myled4 = 1; break;
    case 10: myled1 = 1;myled2 = 0;myled3 = 1;myled4 = 0; break;
    case 11: myled1 = 1;myled2 = 0;myled3 = 1;myled4 = 1; break;
    case 12: myled1 = 1;myled2 = 1;myled3 = 0;myled4 = 0; break;
    case 13: myled1 = 1;myled2 = 1;myled3 = 0;myled4 = 1; break;
    case 14: myled1 = 1;myled2 = 1;myled3 = 1;myled4 = 0; break;
    case 15: myled1 = 1;myled2 = 1;myled3 = 1;myled4 = 1; break;
    default: myled1 = 1;myled2 = 1;myled3 = 1;myled4 = 1; break;
    }
    wait(2.0);myled1 = 0;myled2 = 0;myled3 = 0;myled4 = 0;
    wait(0.1);myled1 = 1;myled2 = 1;myled3 = 1;myled4 = 1;
    wait(0.1);myled1 = 0;myled2 = 0;myled3 = 0;myled4 = 0;
    wait(0.2);
}
25 Sep 2011

First off, to use size of to get the size of the string it WOULD be sizeof(*mystring)

It would be if that worked. 'sizeof' is a static macro that gives you the allocated space, NOT a dynamic amount such as the length of a variable string. For that you need the c library routine 'strlen()'. so try

strlen(mystring)