By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,783 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,783 IT Pros & Developers. It's quick & easy.

expected constructor, destructor, or type conversion before < token

P: 1
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.
Sep 5 '08 #1
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 849
You have a 2D array, so each subarray (row) needs to be defined.

Expand|Select|Wrap|Line Numbers
  1. double arr[2][2] = {{2.0, 1.5}, {1.75, 18.32}};
  2.  
Sep 5 '08 #2

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 5 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 5 '08 #4

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 6 '08 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 6 '08 #6

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 7 '08 #7

Expert 100+
P: 849
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).
Sep 7 '08 #8

Parul Bagadia
100+
P: 188
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?
Sep 7 '08 #9

Banfa
Expert Mod 5K+
P: 8,916
what are the signs after double; are they for pointer?
Yes .
Sep 7 '08 #10

weaknessforcats
Expert Mod 5K+
P: 9,197
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. void MyFunc(double** arg)
  3. {
  4.  
  5. }
  6. int main()
  7. {
  8.    const int TN = 5;
  9.    const int TTS = 10;
  10.    double arr[TN][TTS];
  11.    MyFunc(arr);
  12.  
  13. }
  14.  
and see what your compiler says about this. If you are right, this will compile without error.
Sep 7 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.