---
name: "coap_string"
section: "3"
platform: "linux"
package: "libcoap3t64"
version: "4.3.4-1.1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/coap_string"
---

# coap\_string(3)

Work with CoAP string functions

## NAME

coap\_string, coap\_new\_string, coap\_delete\_string, coap\_new\_str\_const, coap\_delete\_str\_const, coap\_new\_binary, coap\_delete\_binary, coap\_resize\_binary, coap\_new\_bin\_const, coap\_delete\_bin\_const, coap\_make\_str\_const, coap\_string\_equal, coap\_binary\_equal - Work with CoAP string functions

## SYNOPSIS

**#include \<coap3/coap.h\>**

**coap\_string\_t \*coap\_new\_string(size\_t** *size***);**

**void coap\_delete\_string(coap\_string\_t \****string***);**

**coap\_str\_const\_t \*coap\_new\_str\_const(const uint8\_t \****data***, size\_t** *size***);**

**void coap\_delete\_str\_const(coap\_str\_const\_t \****string***);**

**coap\_str\_const\_t \*coap\_make\_str\_const(const char \****string***);**

**int coap\_string\_equal(coap\_string\_t \****string1***, coap\_string\_t \****string2***);**

**coap\_binary\_t \*coap\_new\_binary(size\_t** *size***);**

**void coap\_delete\_binary(coap\_binary\_t \****binary***);**

**coap\_binary\_t \*coap\_resize\_binary(coap\_binary\_t \****binary***, size\_t** *new\_size***);**

**coap\_bin\_const\_t \*coap\_new\_bin\_const(const uint8\_t \****data***, size\_t** *size***);**

**void coap\_delete\_bin\_const(coap\_bin\_const\_t \****binary***);**

**int coap\_binary\_equal(coap\_binary\_t \****binary1***, coap\_binary\_t \****binary2***);**

For specific (D)TLS library support, link with **-lcoap-3-notls**, **-lcoap-3-gnutls**, **-lcoap-3-openssl**, **-lcoap-3-mbedtls** or **-lcoap-3-tinydtls**. Otherwise, link with **-lcoap-3** to get the default (D)TLS library support.

## DESCRIPTION

There is support for storing strings (usually readable data) and for storing binary data. These are used by a number of functions and provide the information in some of the callbacks.

There are 4 supported string/binary types as follows

```
/*
 * Coap string data definition
 */
typedef struct coap_string_t {
  size_t length;    /* length of string */
  uint8_t *s;       /* string data */
} coap_string_t;
/*
 * Coap string data definition with const data
 */
typedef struct coap_str_const_t {
  size_t length;    /* length of string */
  const uint8_t *s; /* read-only string data */
} coap_str_const_t;
/*
 * Coap binary data definition
 */
typedef struct coap_binary_t {
  size_t length;    /* length of binary data */
  uint8_t *s;       /* binary data */
} coap_binary_t;
/*
 * Coap binary data definition with const data
 */
typedef struct coap_bin_const_t {
  size_t length;    /* length of binary data */
  const uint8_t *s; /* read-only binary data */
} coap_bin_const_t;
```

## FUNCTIONS

**Function: coap\_new\_string()**

The **coap\_new\_string**() function allocates a new coap\_string\_t of *size* where *s* points to uninitialized data of length *size* with an extra trailing NULL at *size* + 1. *length* is set to *size*.

**Function: coap\_delete\_string()**

The **coap\_delete\_string**() function is used to delete the coap\_string\_t created by **coap\_new\_string**().

**Function: coap\_new\_str\_const()**

The **coap\_new\_str\_const**() function allocates a coap\_str\_const\_t of *size* where *s* is filled in with *data* and has a trailing NULL added. *length* is set to *size*. *s* is read-only.

**Function: coap\_delete\_str\_const()**

The **coap\_delete\_str\_const**() function is used to delete the coap\_str\_const\_t created by **coap\_new\_str\_const**().

**Function: coap\_make\_str\_const()**

The **coap\_make\_str\_const**() function is used to take some read-only text and uses a static coap\_str\_const\_t for use in different function calls. There are two static entries that are cycled through so that a single function call can call **coap\_make\_str\_const**() twice.

**Function: coap\_string\_equal()**

The **coap\_string\_equal**() function is used to compare two different string objects *string1* and *string2*.

**Function: coap\_new\_binary()**

The **coap\_new\_binary**() function allocates a new coap\_binary\_t of *size* where *s* points to uninitialized data of length *size*. *length* is set to *size*.

**Function: coap\_resize\_binary()**

The **coap\_resize\_binary**() function is used resize the size of *s* to the new size of *new\_size*. The data between the old *length* and the *new\_size* is unitialized. *length* is set to *new\_size*.

**Function: coap\_delete\_binary()**

The **coap\_delete\_binary**() function is used to delete the coap\_binary\_t created by **coap\_new\_binary**().

**Function: coap\_new\_bin\_const()**

The **coap\_new\_bin\_const**() function allocates a coap\_bin\_const\_t of *size* where *s* is filled in with in with *data* and has a trailing NULL added. *length* is set to *size*. *s* is read-only.

**Function: coap\_delete\_bin\_const()**

The **coap\_delete\_bin\_const**() function is used to delete the coap\_bin\_const\_t created by **coap\_new\_bin\_const**().

**Function: coap\_binary\_equal()**

The **coap\_binary\_equal**() function is used to compare two different binary objects *binary1* and *binary2*.

## RETURN VALUES

**coap\_new\_string**() returns a pointer to an allocated coap\_string\_t or NULL if there was a failure.

**coap\_new\_str\_const**() returns a pointer to an allocated coap\_str\_const\_t or NULL if there was a failure.

**coap\_make\_str\_const**() returns a pointer to a structure in static memory that has a pointer to the provided string.

**coap\_new\_binary**() returns a pointer to an allocated coap\_binary\_t or NULL if there was a failure.

**coap\_resize\_binary**() returns a pointer to an re-allocated coap\_binary\_t or NULL if there was a failure.

**coap\_new\_bin\_const**() returns a pointer to an allocated coap\_bin\_const\_t or NULL if there was a failure.

**coap\_string\_equal**() and **coap\_binary\_equal**() return 1 on a precise match, else 0.

## SEE ALSO

[**coap\_attribute**(3)](/coap_attribute/3.md), [**coap\_context**(3)](/coap_context/3.md), [**coap\_handler**(3)](/coap_handler/3.md), [**coap\_pdu\_setup**(3)](/coap_pdu_setup/3.md) and [**coap\_resource**(3)](/coap_resource/3.md)

## FURTHER INFORMATION

See

"RFC7252: The Constrained Application Protocol (CoAP)"

for further information.

## BUGS

Please report bugs on the mailing list for libcoap: libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at https://github.com/obgm/libcoap/issues

## AUTHORS

The libcoap project \<libcoap-developers@lists.sourceforge.net\>
