Search code examples
c++pointersarduino

How do these pointers point to more than just a char or a byte?


I'm a high school student, recently started my final exam project using Arduino, and I'm trying to understand this piece of code I found in an example sketch for a radio transmitter using VirtualWire.

const char *msg = "hello";
vw_send((byte *)msg, strlen(msg));

I was never taught pointers or even C at all, so I'm having some issues understanding how this works.

*msg is a pointer to char, right ? At that point, it should be null, so why can you assign a value to it ?

Also, what the holy hell is (byte *)msg ? What do these parentheses mean ? Is it a byte equal to the value at pointer msg ? How does that work considering it's a char? And why is it necessary ?

Full code for context:

// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley ([email protected])
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);   // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);  // Bits per sec
}

void loop()
{
    const char *msg = "hello";

    digitalWrite(13, true); // Flash a light to show transmitting
    vw_send((byte *)msg, strlen(msg));
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);
    delay(200);
}

Solution

  • At that point, it should be null, so why can you assign a value to it ?

    No, it's not null.

    Consider this:

    int x = 42;
    

    Would you expect x to have the value 0 or the value 42 ? I assume that you would answer 42

    So why assume that

    const char *msg = "hello";
    

    will cause msg to be NULL? It won't.

    Instead it is initialized so that it points to the string "hello". Or more precisely - it points to the char h in the string "hello".

    Also notice that you can assign the pointer to point to some other char later on.

    Example:

    #include <stdio.h>
    
    int main(void) {
        const char* p = "hello";  // Now p points to the 'h' in "hello"
        printf("%s\n", p);
        p = "world";              // Now p points to the 'w' in "world"
        printf("%s\n", p);
        return 0;
    }
    

    Output:

    hello
    world
    

    In other words - you can give the pointer a new value as many times as you like.

    Here is another example:

    #include <stdio.h>
    
    int main(void) {
        const char* p = "hello"; // Make p point to the first char in the string
        do
        {
            printf("%s\n", p);
            p = p + 1;         // Make p point to the next char in the string
        } while (*p);
    
        return 0;
    }
    

    Output:

    hello
    ello
    llo
    lo
    o