Search code examples
cisspace

How to fix argument of type is incompatible with parameter of type


I used the function isspace to search through a word for white spaces. The problem is that I get an error message when the program builds: "argument of type char* is incompatible with parameter of type int"

    int const bufferSize = 256;

    newItemIDPointer = (char*)malloc(bufferSize * sizeof(char));
    if (newItemIDPointer == NULL)
    {
        printf("Didnt allocate memory!");
        exit(EXIT_SUCCESS);
    }

    printf("Enter new Item ID: ");
    scanf_s(" %[^'\n']s", newItemIDPointer, bufferSize);

    stringLength = strlen(newItemIDPointer);
    newItemIDPointer = (char*)realloc(newItemIDPointer, size_t(stringLength + 1));

    int i = 0;
    int count = 0;
    while ((newItemIDPointer + i) != '\0')
    {
        if (isspace(newItemIDPointer + i))
        {
            count++;
        }
        i++;
    }

What is wrong with the implementation of isspace in my code and How can I fix this error message?


Solution

  • newItemIDPointer + i is a pointer to the i'th character in the string. You don't want the pointer but the character it points to. You need to dereference the pointer.

    So replace this:

    while ((newItemIDPointer + i) != '\0')
    {
        if (isspace(newItemIDPointer + i))
    

    With:

    while (*(newItemIDPointer + i) != '\0')
    {
        if (isspace(*(newItemIDPointer + i)))
    

    Or equivalently:

    while (newItemIDPointer[i] != '\0')
    {
        if (isspace(newItemIDPointer[i]))