From 9dcae3ac1607fd531ed49d3a299a99e64495e376 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Sun, 8 Feb 2026 18:39:09 +0000 Subject: [PATCH] more C pointer confusion --- zk/Char_data_type_in_C.md | 14 ++++++++++ zk/Impedence.md | 0 zk/Pointers_in_C.md | 51 +++++++++++++++++++++++++++++++++++++ zk/mosquitto_mqtt_client.md | 14 ++++++++++ 4 files changed, 79 insertions(+) create mode 100644 zk/Impedence.md diff --git a/zk/Char_data_type_in_C.md b/zk/Char_data_type_in_C.md index 6dfbe67..0ee3f07 100644 --- a/zk/Char_data_type_in_C.md +++ b/zk/Char_data_type_in_C.md @@ -80,3 +80,17 @@ The more formal way of doing this would be: ```c char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'} ``` + +However, in practice, if you just want quick access to a string, where you don't +need to modify the individual characters later, you can just use a direct +pointer definition: + +```c +char *name = "Thomas"; +``` + +And then to use it later just use `name` (no `*`). In this scenario, the string +is a string literal and is stored as read-only. It can be redefined (the pointer +can point to something else) but it's not mutable itself. + +More info in [Pointers in C](./Pointers_in_C.md). diff --git a/zk/Impedence.md b/zk/Impedence.md new file mode 100644 index 0000000..e69de29 diff --git a/zk/Pointers_in_C.md b/zk/Pointers_in_C.md index 0cd5bed..9f5a79b 100644 --- a/zk/Pointers_in_C.md +++ b/zk/Pointers_in_C.md @@ -37,3 +37,54 @@ becomes equal to `27`. Pointers are necessary because C uses a [call by value](./C_is_call_by_value.md) system for function arguments. + +## To use a pointer you don't have to declare the value first (confusing) + +It is totally legitimate to declare a pointer in one go, especially with +strings: + +```c +char *name = "Thomas"; +``` + +Then to reference the value (_not_ the address, the actual value) you just use +`name`. + +This is confusing but I just have to accept for now. + +Note that if you do this, it creates the string in read-only memory. You can't +modify the individual characters of `"Thomas"`. To do this you would need to +define it as: + +```c +char name[] = "Thomas"; +name[0] = "t"; +``` + +Here is a real example of me doing this: + +```c +void mqtt_publish(esp_mqtt_client_handle_t client, char *topic, + const char *payload) +{ + if (MQTT_CONNECTED) { + esp_mqtt_client_publish(client, topic, payload, strlen(payload), 0, 0); + } +} +``` + +And then when calling the function: + +```c +mqtt_publish(mqtt_client, "test_topic", "Test message"); +``` + +Or, using a variable: + +```c +static char *topic = "test_topic"; +mqtt_publish(mqtt_client, topic, "Test message"); +``` + +> Note in the above the `*` is part of the type definition, _not_ the variable. +> Even more confusing! diff --git a/zk/mosquitto_mqtt_client.md b/zk/mosquitto_mqtt_client.md index ef463e4..6d8b557 100644 --- a/zk/mosquitto_mqtt_client.md +++ b/zk/mosquitto_mqtt_client.md @@ -5,6 +5,20 @@ tags: - mqtt --- +## Install + +```sh +sudo apt install mosquitto +``` + +This just installs the broker and you can start publishing straight away but if +you want to view the incoming requests to the given topic you need to install +the client package: + +```sh +sudo apt install mosquitto-clients +``` + ## Config At `/etc/mosquitto/mosquitto.conf`.