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

Why is C not consistent

Name: Anonymous 2012-05-10 1:43

Take the following two declarations "int arr[3]" and "int *ptr".

Both require different storage, but in usage both are identical. This behaviour  is known, and it is not the consistency I am talking about.

Take the following two declarations:

void funcarr(int a, int arr[3], int c)
{ printf("%d\n", &c-&a); }

void funcptr(int a, int *ptr, int c)
{ printf("%d\n", &c-&a); }

For funcarr, all three arguments are intended to be call by value. So I would expect the stack to contain
a,arr[0],arr[1],arr[2],c
but it does not. The stack contains
a,&arr[0],c

This is *NOT* consistent, and gives a false impression that the elements of arr[] will not change within the lifetime of the function.

Who failed, so I knows whose ass to kick.

Name: Anonymous 2012-05-11 9:00

>>25
an address is an address
Someone hasn't read the C Standard and thinks x86 is all that's out there.

tl;dr: array declaration is pointer declaration with explicit space allocation
That makes as much sense as saying that int declaration is pointer declaration with explicit space allocation and automatic dereferencing. It's only partially correct and not meaningful.

Here's the most obvious demonstration of the difference between arrays and pointers:

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

int main(void)
{
    int ar[10];
    int *pt = malloc(10 * sizeof(int));

    printf("sizeof(ar) = %lu\n", sizeof(ar));
    printf("sizeof(pt) = %lu\n", sizeof(pt));

    return 0;
}

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