1

I'm trying to write a trim function, but when I try to use it the compiler is giving me a runtime error or load of null pointer of type 'char' when I try to run this code:

// Trim trailing whitespace
int len = strlen(*str);
int trimmed = 0;

while (isspace(*str[len - 1])) {
    --len;
    trimmed = 1;
}

Here is my compiler command: gcc ./main.c ./json.c -g -m64 -Wall -Wextra -fsanitize=address,undefined,leak -I ./lib/include -L ./lib -o ./json

This is the function that is causing the error:

void _JSON_Trim(char **str) {
  // Trim leading whitespace
  while (isspace(**str) || **str == '\0') {
    ++*str;
  }

  // Trim trailing whitespace
  int len = strlen(*str);
  int trimmed = 0;

  while (isspace(*str[len - 1])) {
    --len;
    trimmed = 1;
  }

  if (trimmed) {
    *str[len] = '\0';
  }
}

This code was semi-working until I added the double pointer symbol (char **str was char *str). However, char *str wasn't properly incrementing the pointer.

Note: the error specifically occurs in the line 11 where while (isspace(*str[len - 1])) { is called. I don't know where in the line it occurs as the compiler doesn't specify.

This is the string that the function is called on:

[
    "HI",
    "Bye"
]

Note: yes I know there's technically nothing to trim here, so the function wouldn't do anything, but it's there incase it does

3
  • 2
    *str[len - 1] is the same as *(str[len - 1]) which doesn't seem right. Perhaps you meant (*str)[len - 1]? Commented Nov 24, 2023 at 12:11
  • 1
    Imagine str being allocated as char[10] initialized with " \0" (ie nine spaces and a terminating \0) Then your code goes beyond the end of the allocated memory. Commented Nov 24, 2023 at 12:22
  • @Some programmer dude Yes, thank you Commented Nov 24, 2023 at 12:24

1 Answer 1

3

The postfix subscript operator has a higher priority than the unary dereferncing operator. This line

while (isspace(*str[len - 1])) {

is equivalent to

while (isspace(*( str[len - 1] ))) {

while you need to write

while (isspace( ( *str )[len - 1])) {

And it ie even better to write

while (isspace( ( unsigned char )( *str )[len - 1])) {

Pay attention to that the variable len shall have type size_t instead of the type int

int len = strlen(*str);.

size_t is the return type of the function strlen.

Sign up to request clarification or add additional context in comments.

2 Comments

It might be worth suggesting that the function should return the new start to the string, which means it could revert to taking a char *argument.
@JonathanLeffler In my opinion the function should move the string to the left overriding leading spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.