Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not to be confused with:

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  int main() {
      int *x = malloc(sizeof(int) * 5);
      memset(x, 0, sizeof(int) * 5);
  
      printf("%d\n", *x);
      printf("%d\n", *(x+1));
      printf("%p\n", x);
      printf("%p\n", x+1);
      printf("%p\n", &x);
      printf("%p\n", &x+1);
  
      return 0;
  }


Or, not to be confused with:

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  void foo(int x[5]) {
      printf("%p\n", x);
      printf("%p\n", x+1);
      printf("%p\n", &x);
      printf("%p\n", &x+1);
  }
  int main() {
      int x[5];
      foo(x);
  
      return 0;
  }


To be confused with:

    #include <stdio.h>
    
    void foo(int (*x)[5]) {
        printf("%p\n", *x);
        printf("%p\n", *x+1);
        printf("%p\n", x);
        printf("%p\n", x+1);
    }

    int main(void) {
        int x[5];
        foo(&x);
    
        return 0;
    }


Can you elaborate on this?


In C, the array as a function parameter is different from an array as a variable/struct member. So, as a parameter, sizeof(x) is sizeof(int*). As a variable, sizeof(x) is sizeof(int[5]).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: