Obviously a hack… but a way of squaring a number without using the *
operator (this was a coding contest requirement).
(&a)[n]
is equivalent to a pointer to int
at location
(a + sizeof(a[n])*n)
and thus the entire expression is
(&a)[n] -a
= (a + sizeof(a[n])*n -a) /sizeof(int)
= sizeof(a[n])*n / sizeof(int)
= sizeof(int) * n * n / sizeof(int)
= n * n
Related Contents:
- How does the compiler allocate memory without knowing the size at compile time?
- With arrays, why is it the case that a[5] == 5[a]?
- C pointer to array/array of pointers disambiguation
- How to find the ‘sizeof’ (a pointer pointing to an array)?
- What is the difference between char array and char pointer in C?
- Is an array name a pointer?
- How come an array’s address is equal to its value in C?
- Returning an array using C
- C: differences between char pointer and array [duplicate]
- Create a pointer to two-dimensional array
- Should I use char** argv or char* argv[]?
- Correctly allocating multi-dimensional arrays
- How to find the size of an array (from a pointer pointing to the first element array)?
- Exceptions to array decaying into a pointer?
- “int *nums = {5, 2, 1, 4}” causes a segmentation fault
- Difference between array type and array allocated with malloc
- Efficiency: arrays vs pointers
- In C, are arrays pointers or used as pointers?
- Array-syntax vs pointer-syntax and code generation?
- How to include a dynamic array INSIDE a struct in C?
- Smart pointers/safe memory management for C?
- For loop with pointer in C
- My char pointer points to invalid value after being cast from int*
- Is there any overhead for using variable-length arrays?
- Pointer to 2D arrays in C
- What’s the proper use of printf to display pointers padded with 0s
- Differences when using ** in C
- How does free know how much to free?
- What is array to pointer decay?
- Why is the asterisk before the variable name, rather than after the type?
- Catch Ctrl-C in C
- C compile error: “Variable-sized object may not be initialized”
- Fastest way to zero out a 2d array in C?
- Initializing entire 2D array with one value
- Does free(ptr) where ptr is NULL corrupt memory?
- What is the default C -std standard version for the current GCC (especially on Ubuntu)?
- Difference between passing array, fixed-sized array and base address of array as a function parameter
- Why can a string be assigned to a char* pointer, but not to a char[] array?
- What’s the difference between a null pointer and a void pointer?
- Pointer Arithmetic [closed]
- In C, how would I choose whether to return a struct or a pointer to a struct?
- Determine the size of a C++ array programmatically?
- What is the maximum size of an array in C?
- What is the correct type for array indexes in C?
- Function pointers and address of a function
- Dereferencing a pointer to 0 in C
- Anonymous union within struct not in c99?
- Getting a stack overflow exception when declaring a large array
- What happens in OS when we dereference a NULL pointer in C?
- Assign multiple values to array in C