문제

I'm trying to send an array of characters in C, byte by byte to an output for a microcontroller. I'm using the following code:

int main() {
...
   LogOutput("Hello World!");
}

void LogOutput(char *msg) {

    int i;
    for (i = 0; i < sizeof(msg); i++) {
        USART0_TX(msg[i]);              // transmit byte
    }
}

However, this only sends the first two letters "He" --- does anybody know what I'm doing wrong?

도움이 되었습니까?

해결책

You're using the sizeof operator, and getting the size of the datatype, not the length of the string. Use strlen to get the length of a string (include string.h for strlen and other string manipulation functions).

다른 팁

int main() {
...
   LogOutput("Hello World!");
}

void LogOutput(char *msg) {

    int i;
    for (i = 0; i < strlen(msg); i++) {
        USART0_TX(msg[i]);              // transmit byte
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top