Hi,
I have written the following program:
-------------------------------------------------------------------------------------------------------------
1 #include <stdio.h>
2 #include "abc.h"
3
4 void PROP_abc(double ptr[TN][TTS])
5 {
6 double ptr1[TN][TTS] = { 0.1936, 0.17273, 0.63634, 0.9429, 5.4393, 0.77277, 0.025127, 0.14202, 0.55906, 0.38984, 0.00, 0.00, 0.6214, 0.12952, 0.45833, 00.53225, 0.017949, 0.57854, 0.16608, 1.0684, 0.31321, 1.198, 0.00, 0.00, 0.00, 0.025592, 0.00, 0.44234, 0.27205, 0.24618,
7 ............
8 ............
9 ............
10 ............
11 ............
12 ............
13 ............
14 ............};
15 for(int i = 0; i < TN; i++)
16 for(int t = 0; t < TTS; t++)
17 ptr[i][t] = ptr1[i][t];
18 return;
19 }
-------------------------------------------------------------------------------------------------------------
where TN and TTS are defined in file abc.h and the numbers on left are the line numbers.
I am getting following errors:
In function void PROP_abc
1. error: expected `}' before numeric constant in line number 6
2. error: expected â,â or â;â before numeric constant in line number 6
At global scope
3. expected unqualified-id before for in line 15
4. expected constructor, destructor, or type conversion before < token in line 15
5. expected constructor, destructor, or type conversion before ++ token in line 15.
6. expected constructor, destructor, or type conversion before < token in line 16
7. expected constructor, destructor, or type conversion before ++ token in line 16
8. expected unqualified-id before return in line 18
9. expected declaration before } token in line 19
I am surprised to see so many errors but fail to see the reason, please help.
Thank you.
10 15380
You have a 2D array, so each subarray (row) needs to be defined. -
double arr[2][2] = {{2.0, 1.5}, {1.75, 18.32}};
-
Banfa 9,065
Expert Mod 8TB
TN and TTS are defined somewhere are they?
Line 17 is not going to work because ptr1 is not a 2 dimensional array of double but rather a double **. As such there is no first dimension size required for the compiler to be able to properly calculate the location you are talking about.
Line 17 is not going to work because ptr1 is not a 2 dimensional array of double but rather a double **. As such there is no first dimension size required for the compiler to be able to properly calculate the location you are talking about.
ptr1 is the address of ptr1[0]. ptr1[0] is an array of TTS doubles. Therefore, ptr1 is a pointer to an array of TTS doubles:
double (*ptr1)[TTS];
ptr1 is not a double**.
As to array dimension size, assuming TN and TTS are global constants, the compiler does have all the informaton it needs for pointer arithmetic.
The code as posted compiles OK.
The posted errors are outside the posted code.
Banfa 9,065
Expert Mod 8TB
ptr1 is the address of ptr1[0]. ptr1[0] is an array of TTS doubles. Therefore, ptr1 is a pointer to an array of TTS doubles:
double (*ptr1)[TTS];
ptr1 is not a double**.
True my mistake I was talking about ptr not ptr1 which is a double ** but is accessed as a 2d array and that highlights the problems with having your variable names too similar.
True my mistake I was talking about ptr not ptr1 which is a double **
ptr is not a double** either. This code:
void PROP_abc(double ptr[TN][TTS])
5 {
6 double ptr1[TN][TTS] = { 0.1936, 0.17273, 0.63634, 0.9429, 5.4393, 0.77277, 0.025127, 0.14202, 0.55906, 0.38984, 0.00, 0.00, 0.6214, 0.12952, 0.45833, 00.53225, 0.017949, 0.57854, 0.16608, 1.0684, 0.31321, 1.198, 0.00, 0.00, 0.00, 0.025592, 0.00, 0.44234, 0.27205, 0.24618,
etc....
shows ptr is an array of TN elements that are TTS arrays of double. That makes ptr a pointer to and array of TTS doubles just like ptr1.
Where is this double** coming from? There's no double** anywhere in the posted code.
Banfa 9,065
Expert Mod 8TB
ptr is not a double** either. This code:
shows ptr is an array of TN elements that are TTS arrays of double. That makes ptr a pointer to and array of TTS doubles just like ptr1.
Where is this double** coming from? There's no double** anywhere in the posted code.
I believe ptr is a double ** because it is declared as a function parameter. You can't pass arrays as function parameters (or is that something that has changed between C and C++?) and the notation [] used on a function parameter is equivalent to declaration of a pointer rather than declaration of an array.
It's not a double**. It's a double*[]. There's a difference because the latter implies that all of the rows are the same length (a value the compiler needs to generate valid assembly).
It's not a double**. It's a double*[]. There's a difference because the latter implies that all of the rows are the same length (a value the compiler needs to generate valid assembly).
what are the signs after double; are they for pointer?
Banfa 9,065
Expert Mod 8TB
what are the signs after double; are they for pointer?
Yes .
I believe ptr is a double ** because it is declared as a function parameter. You can't pass arrays as function parameters (or is that something that has changed between C and C++?) and the notation [] used on a function parameter is equivalent to declaration of a pointer rather than declaration of an array.
True, you cannot pass an array to a function because the name of the array is the address of element 0. Therefore, what you pass is that address.
In this case, there is an array of double [TN][TTS] that belongs to the caller. When that array is passed to a function, what is passed is the address of element 0. In this case element 0 is an array of double [TTS]. Therefore, what is passed is the address of this element. And that is a double(*)[TTS].
So the correct function argument is double ptr[TN][TTS] or double ptr[][TTS].
In the first case the function knows there are TN elements in the array. In rthe second case, that information should be passed as a second argument.
A double** is a pointer to a double* and a double* is a pointer to a single double. None of this applies to the current code.
It is true that the address of the array and the address of element 0 are the same address. So you could lie to the compiler and typecast to a double** and handle the dimensions with two addtional function arguments.
Since all arrays in C and C++ are one-dimensional arrays, you can typecast as needed to view the array in any manner desired.
Finally, try compiling this code: -
-
void MyFunc(double** arg)
-
{
-
-
}
-
int main()
-
{
-
const int TN = 5;
-
const int TTS = 10;
-
double arr[TN][TTS];
-
MyFunc(arr);
-
-
}
-
and see what your compiler says about this. If you are right, this will compile without error.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
6 posts
views
Thread by Zenon |
last post: by
|
5 posts
views
Thread by Preben |
last post: by
|
10 posts
views
Thread by Szabolcs Horvát |
last post: by
|
5 posts
views
Thread by Damien |
last post: by
| |
5 posts
views
Thread by amitmool |
last post: by
|
12 posts
views
Thread by Rahul |
last post: by
| |
11 posts
views
Thread by Peter Jansson |
last post: by
| | | | | | | | | | | |