|
| siavoshkc (6) | |||||||||||||||||||||||||||||
| The difference between pointers and arrays I have seen in many places that an array is introduced as a pointer. This is technically not correct. Arrays are not pointer. So what is it? It is like any other variable in C++. Take a look at this code:
You may say: "Look there, arr is an address, who says it's not a pointer?" And I say: this code prints out an address. So var is an address?
Let me explain: All the variables can be manipulated in the memory by their addresses. CPU uses these addresses to fetch them, change them and save them. So, all variables have an address (not only those that you pointed to them). We can find out the address of a variable by (&) operator, in form of:
An array is just a sequence of variables. But there is a rule, that C++ looks arrays as if they were pointers. What does it means? It means that if you write arr, compiler takes it as &arr[0] (except in three conditions that I'll tell you soon), that is the address of the first element of the array. It has a type "pointer to T" where T is the type of array elements (that is int in our example). If you add 1 to it it will point to the second element of the array. So it takes &arr as what? It is one of the exceptions where the rule does not apply. It takes it as a pointer to array. It will again point to the first element of the array, but if you add one to this pointer, it will point to the address of the place right after the last element of the array (just like you skipped the array). It is a pointer to the whole the array. The value of &arr and arr is the same (address of the first element). But their type is different. Here &arr has type "pointer to the array of T" (compare it to the type of arr). Look at this
First cout: Prints out the "value" of arr[0]. Second cout: Prints out the "address" of arr[0]. Third cout: Again prints out the "address" of arr[0]. Forth cout: Prints out the address of the array, that is again the address of arr[0]. And this:
First cout: Prints out the "value" of arr[0] plus one. Second cout: Prints out the "address" of arr[1]. Third cout: Again prints out the "address" of arr[1] . Forth cout: Prints out the first address of memory after the array. Comparison Similarities, each with an example: 1) (*) can be used for both.
2) Subscription can be used for both
3) A pointer can be used as an array.
4) An array can have a type of pointer. That means the elements of it can be pointers.
5) All arrays will be passed to functions as a pointer, which means you can't actually send an array to a function. So function(char[]) is equal to function(char*).
Differences: 1) A pointer is a place in memory that keeps address of another place inside, while an array is a single, preallocated chunk of contiguous elements (all of the same type), fixed in size and location. 3) Array like pointers can't be initialized at definition like arrays.
4) When we allocate memory for a pointer to use it as a dynamic array. The memory can be resized or freed later. But this is not the case for arrays. For example:
3) They produce different assembly code. Look and compare:
Will have an assembly like this:
Hope to be useful... Refferences: http://67.40.109.61/torek/c/index.html http://c-faq.com/questions.html | |||||||||||||||||||||||||||||
This topic is archived - New replies not allowed.
