Converting pre-ANSI C function declarations with fixed array sizes to ANSI

StackOverflow https://stackoverflow.com/questions/19037589

  •  29-06-2022
  •  | 
  •  

سؤال

I'm new to C (well, RUSTY in C at least) and I'm working on some pre-ANSI C code in Visual Studio 2010. It compiles fine, but Intellisense is going crazy over the function declarations:

int add()
    int a, int b;
{
    return a + b;
}

instead of the ANSI

int add(int a, int b) 
{
    return a + b;
}

But I'm running into a few instances of arrays with bounds in function declarations, and I don't think that can be converted directly to ANSI C.

int add() 
    int a[5], int b[5];
{
    ...
}

The below compiles cleanly, but does this mean the exact same thing?

int add(int a[5], int b[5]) {
}
هل كانت مفيدة؟

المحلول

Yes, it means the same thing, but it doesn't (and never did) really mean what it looked like it should mean.

The parameters are really pointers. By inference, there should be 5 elements in the arrays that they point to, but that isn't going to be guaranteed. In particular, given:

extern int add(int a[5], int b[5]);

int x[4];
int y[6];

add(x, y);

This is not going to cause the compiler to throw warnings.

With C99, you could let the optimizer assume that there will be at least 5 entries using the (weird) notation:

int add(int a[static 5], int b[static 5]) { ... }

Even this doesn't necessarily cause warnings at the call site; it is information to the compiler (though whether it uses it or not is up to the compiler).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top