0

I can't figure this one out. Plain C compiled with MSVC Compiler on the command-line.

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

With the if (NULL == string) { return NULL; } block, I get a syntax error.

..\src\drift_charbuffer.c(78) : error C2143: syntax error : missing ';' before 'type'
..\src\drift_charbuffer.c(79) : error C2065: 'index' : undeclared identifier
..\src\drift_charbuffer.c(79) : error C2065: 'index' : undeclared identifier
..\src\drift_charbuffer.c(79) : error C2065: 'index' : undeclared identifier
..\src\drift_charbuffer.c(81) : error C2065: 'index' : undeclared identifier
..\src\drift_charbuffer.c(85) : error C2065: 'index' : undeclared identifier
..\src\drift_charbuffer.c(87) : error C2065: 'index' : undeclared identifier    

But it compiles fine without the if-block. I just can't see what's wrong here.

char*
drift_charbuffer_tostring(const drift_charbuffer* buffer)
{
    // todo: UTF-8 encoding for characters outside the ASCII-range.
    char* string = drift_alloc(buffer->count + 1);
    if (NULL == string)
    {
        return NULL;
    }

    int index;     // Line: 78
    for (index = 0; index < buffer->count; ++index)
    {
        int value = *drift_charbuffer_get(buffer, index);
        if (value > 127)
            value = '?';

        string[index] = value;
    }
    string[index] = 0;
    return string;
}
Niklas R
  • 16,299
  • 28
  • 108
  • 203

1 Answers1

2

Well, your "plain C" is actually C99 or post-C99 C. Meanwhile, MSVC compiler only supports the "classic good old plain C" AKA C89/90. In classic C it is illegal to mix statements and declarations. All declarations have to be done at the very top of the block.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765