Hi All
I have a very basic idea decaying of array. Can anyone clearly explain what it actually does? Or any good URL could be of great use.
Thanks & Regards
Sathish Kumar
6 1935
Hi All
I have a very basic idea decaying of array. Can anyone clearly explain what it actually does? Or any good URL could be of great use.
Thanks & Regards
Sathish Kumar
If you are taking pointer array like - int * arr = new int[];
-
.....
-
delete [] arr;
Then after the usage you need to delete this pointer array. It is called decaying the array.
regards
If you are taking pointer array like - int * arr = new int[];
-
.....
-
delete [] arr;
Then after the usage you need to delete this pointer array. It is called decaying the array.
regards
Not quite zodilla.
Decay means loss of type information.
e.g
if you have an array
You can do
which is essentially the same as doing
p loses the fact that array has length 5.
Decay means loss of type information.
.
Not quite r035198x.
Arrays don't have a type. They are composed of a type but have no type. That's why you can't return an array using the function return type. You can return only a type or a pointer to a type. Arrays are a pointer to a type.
The decay of array means that when you represent the array as a pointer you have lost the "array-ness" of the thing. You are left with a pointer to type, which is a pointer to a single instance of that type. The number or elements has disappeared. Hence the array has decayed to a pointer.
Hi
Can you explain this sentence clearly? "Arrays are composed of a type but have no type". I am not that much clear.
Thanks & Regards
Sathish Kumar
.
Not quite r035198x.
Arrays don't have a type. They are composed of a type but have no type. That's why you can't return an array using the function return type. You can return only a type or a pointer to a type. Arrays are a pointer to a type.
The decay of array means that when you represent the array as a pointer you have lost the "array-ness" of the thing. You are left with a pointer to type, which is a pointer to a single instance of that type. The number or elements has disappeared. Hence the array has decayed to a pointer.
.
Not quite r035198x.
Arrays don't have a type. They are composed of a type but have no type. That's why you can't return an array using the function return type. You can return only a type or a pointer to a type. Arrays are a pointer to a type.
The decay of array means that when you represent the array as a pointer you have lost the "array-ness" of the thing. You are left with a pointer to type, which is a pointer to a single instance of that type. The number or elements has disappeared. Hence the array has decayed to a pointer.
Fair enough.
Can you explain this sentence clearly? "Arrays are composed of a type but have no type". I am not that much clear.
That's true. Arrays have no type.
For example, a type is something like an int. Now you can have an int variable:
but there is no type for an array of integers. Instead you have to code:
to get an array of 5 integers.
Read this. It's more info on arrays in C and C++.
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
In each case value is the number of elements. Any other brackets only describe the elements.
Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Kinda the same, right?
So if your disc file contains
0 1 2 3 4 5 6 7 8 9
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sherifffruitfly |
last post by:
Hi,
I'm just learning cpp, and the exercise I'm working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of...
|
by: dis_is_eagle |
last post by:
Hi.I know that when an array name is used with the sizeof() operator,it
does not decay into a pointer.Is there any other situation when the
decaying to a pointer thing does not happen.Thanks for...
|
by: Mercy |
last post by:
I guess my C++ is pretty darn rusty. I was just looking over sample
C++ code for practice... and I'm kind of confused about this code
fragment:
int sector2;
int i = 3;
memset(sector2,...
|
by: Tubular Technician |
last post by:
Hello, World!
Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,
* size_t is the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |