'why do I have compile time error in my code?

I have error on code and i dont understand why, someone can help me? the error in void printArray(char* p, int len) function on the printf.


#include <stdio.h>
#include <string.h>

void printArray(char* p, int len)
{
    for (p; p < p + len; p++)
    {
        printf("%c", *p);
    }
    printf("\n");
}

int main(void)
{
    char* msg = "hi jyegq meet me at 2 :)";
    printArray(msg, strlen(msg));
    getchar();
    return 0;
}



Solution 1:[1]

There is no compile time error in GCC at least. However this line:

for (p; p < p + len; p++)

can with appropriate settings elicit a warning in GCC at kleast:

main.c:6:5: error: statement with no effect [-Werror=unused-value]

However that refers to p in the initial expression which does nothing and is benign. The real (runtime not compile time) issue is that the expression p < p + len can never be true unless len == 0 - in which case it is always true.

    for( char* pstart = p; p < pstart + len; p++)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Clifford