Showing posts with label cpp_naive. Show all posts
Showing posts with label cpp_naive. Show all posts

Jan 22, 2014

[C][C++] Pointer vs. Array

Pointer == Array ?? Not really



void bar(int arr[][3], int m, int n)
//same
void bar(int (*arr)[3], int m, int n)
//-------
// int arr[][3][4] => int (*arr)[3][4], et cetera. same
== quote from Keith Thompson ==
Arrays are not pointers, and pointers are not arrays.
Certain features of the language seem to conspire to make you think they’re equivalent. They are not.

An expression of array type, in most contexts, is implicitly converted to a pointer to the first element of the array object.

There are three cases where this conversion doesn’t occur:
1. When the array is the operand of a unary “&” operator
(so &arr yields the address of the array, not the address of its first element; same address, different type).
2. When the array is the operand of a unary “sizeof” operator (so sizeof arr yields the size of the array, not the size of a pointer).
3. When the array is a string literal in an initializer used to initialize an array object (so char arr[6] = “hello” works).

Another rule: When you declare a function parameter with array type, it’s really of pointer type.
void foo(int arr[]);
really means this:
void foo(int *arr);
This isn’t a conversion, it’s a compile-time translation.

Finally, the indexing operator [] doesn’t take an array operand.
It takes two operands, a pointer and an integer.
p[i] is, by definition, equivalent to *(p+i).
(And it’s commutative, so arr[42] is equivalent to 42[arr]. Now that you know that, please don’t use it.)

So when you write arr[i], where arr is declared as an array object, the indexing operator gets a pointer to the first element of arr.
And when you pass an array to a function:
int arr[10];
func(arr);
you’re really passing an int*, not an array.
This isn’t because it’s a function call, it’s because the expression is converted *before* the call.
The same conversion would happen in any context other than the three that I mentioned.

So when you say that “Arrays passed to functions are converted to pointers”,
that’s really just one case of a more general rule.

Reference:
Pointers to arrays in C
Row-major_order
making-sense-of-pointers
reading-c-type-declarations
non-constant-global-initialization-in-c-and-c

Dec 4, 2013

[C++][NOTE][BEGINNER] Parsing issue

Y y(X());

The compilation error is on the line y.f(), but the source of the problem is Y y(X());

This could be interpreted as a a variable definition (which was the intention of the

programmer in this example), or as a definition of a function y, returning an object

of typeY, taking a function (with no arguments, returning an object of type X) as its argument.

The compiler is required by the standard to choose the second interpretation, which means that

y.f() does not compile (since y is now a function, not an object of type Y).

Wikipedia has a concise explanation: http://en.wikipedia.org/wiki/Most_vexing_parse,

and the standard has more in §6.8.

To fix the problem, change Y y(X()) to either Y y{X{}} (modern C++) or Y y((X())) (pre-C++11)

Feb 21, 2012

[c++] Appearing and Disappearing consts in C++

original post:

  • in lambda, when capture by value, the constentness is also captured!!



Appearing and Disappearing consts in C++

C++Next is happy to republish the following article by Scott Meyers, with Scott’s permission, of course. Thanks, Scott!