Skip to content

Instantly share code, notes, and snippets.

@larry-oates
Last active October 19, 2025 13:38
Show Gist options
  • Select an option

  • Save larry-oates/0c5404cb62606f25f5d15647a039cbe7 to your computer and use it in GitHub Desktop.

Select an option

Save larry-oates/0c5404cb62606f25f5d15647a039cbe7 to your computer and use it in GitHub Desktop.
The C array-pointer myth
// Contary to the popular myth, an array's name isn't always the same as a pointer to it's first element!
#include <stdio.h>
int main() {
// You can reassign a pointer...
int *ptr;
ptr = (int *) 0xDEADBEEF;
// But as an array ISN't a pointer...
int array[1];
array = (int *) 0xDEADBEEF;
// so you can't run this! - An array is an unmodifiable lvalue (a value that can be on the left of an assignment oparation). In other word's it can be addressed but can't be modified
// ...but you CAN run this!
ptr = array;
/// When used in an lvalue expression as above, it's implicitly converted to a pointer to the first element, which is what you where probaly thinking of!
return 0;
}
// For more reading see the "Assignment" section of this page:
// https://ezn.cppreference.com/w/c/language/array.html
@CavHaunch
Copy link

So arrays are const pointers? I assume that array = ptr; doesn't work either.

@larry-oates
Copy link
Author

They are their own type but yes, the array name acts as a constant pointer in essence and can't be assigned a different address

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment