Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C problem

Name: Anonymous 2012-01-18 19:35

Is the behaviour of this program well defined?

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

int main() {
    size_t num, min, max;
    if(scanf("%zu%zu%zu", &num, &min, &max) != 3) return 0;
    if(min >= num || max >= num || min > max) return 0;
    int *arr = calloc(num, sizeof(*arr));
    if(!arr) return 0;
    int *x = arr + min, *y = arr + max, *z = arr + (y - x);
    *x = 1, *y = 1, *z = 1;
    return 0;
}

Name: Anonymous 2012-01-19 16:34

>>11
Sizeof doesn't evaluate the expression, it just infers the type of the expression, and then retrieves the size of the expression's type.


And before you get your panties in a bundle, let me quote the standard...

"The sizeof operator yields the size (in bytes of its operand), which may be an expression or parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of operand is a variable length array type, the operarand is evaluated; otherwise the operand is not evaluated and the result is an integer constant."

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List