Pergunta

This program counts the length of a string by subtraca warning message

-\

My stringLength function returns a size_t that is Is my mistake in the format specifier or how I return my value?

#include <stdio.h>
#include <stdlib.h>

size_t stringLength(const char *str);

int main(void) 
{


    return 0;
}

size_t stringLength(const char *str)
{
    const char *strCopy = str;

    while (*str)              
    {
        str++;                
    }

}
Foi útil?

Solução

The issue you're having is that operator-() returns a signed value, rather than unsigned. Because in your routine you know that the value will always be positive, you can add a cast to size_t and not worry about the sign being wrong.

return static_cast<size_t>(str - strCopy); //C++

or

return (size_t)(str - strCopy); //C
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top