pointers to an array and accesing elements of the array 
Author Message
 pointers to an array and accesing elements of the array

Problem :
1.
A 2D array is defined and I create a pointer to the array. Then pass the
pointer to a function where the array elements are to be updated.
However on compiling the error states that the pointer is not defined.
Follwoing is the code in the header file

#define volume_height 25
#define volume_width 25
#define volume_depth 25

int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

int siddon_projection( ElemsForOneLine *lor ); // error undeclared
identifier

2. Also in the main file, the header file is included and the function is
declared as

int siddon_projection( ElemsForOneLine &lor ) // error illegal left hand
operand has type [75][3] and lor is undeclared identifier
{

Quote:
}

Please would anyone help

Thanks
A.J



Sat, 19 Apr 2003 19:18:02 GMT  
 pointers to an array and accesing elements of the array

Quote:
> #define volume_height 25
> #define volume_width 25
> #define volume_depth 25

This is a matter of style, but many capitalize macros, like this:

   #define VOLUME_HEIGHT   25
   #define VOLUME_WIDTH    25
   #define VOLUME_DEPTH    25

This makes it clear that these are macros, and not variables; if they
weren't macros, the next line wouldn't compile.

Quote:
> int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

This declares an array of arrays called ElemsForOneLine, not a type.
If you wanted to make a type, look up structs or typedefs in your C
book.

(As an aside, the sum of the height, width, and depth of an object
doesn't have much meaning.  Since they are defined with respect to
volume, I suspect you wanted to multiply.)

Quote:
> int siddon_projection( ElemsForOneLine *lor ); // error undeclared

This does not work because ElemsForOneLine is not a type, but an
array of arrays.

Quote:
> 2. Also in the main file, the header file is included and the function is
> declared as
> int siddon_projection( ElemsForOneLine &lor ) // error illegal left hand

This is what pass-by-reference looks like in C++.  C does not have
any such capability.  Furthermore, your prototype and your function
definition should *not* disagree.  Change them both to something valid.

Note that // is a syntax error in C.

--



Sat, 19 Apr 2003 19:33:46 GMT  
 pointers to an array and accesing elements of the array
<snip otherwise excellent post>

Quote:

> Note that // is a syntax error in C.

Minor nit: it is a syntax error in C90, to which all current
ISO-conforming compilers conform. It is not a syntax error in the
current C Standard, but on the other hand there aren't any C99 compilers
yet.

In other words, your statement is wrong in theory but correct in
practice, at least for the time being.

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html



Sat, 19 Apr 2003 19:49:41 GMT  
 pointers to an array and accesing elements of the array

Quote:
> #define volume_height 25
> #define volume_width 25
> #define volume_depth 25

> int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

ElemsForOneLine is an int array

Quote:
> int siddon_projection( ElemsForOneLine *lor ); // error undeclared
> identifier

and so you can't use it as type identifier

Quote:
> 2. Also in the main file, the header file is included and the
> function is
> declared as

> int siddon_projection( ElemsForOneLine &lor ) // error illegal left
> hand
> operand has type [75][3] and lor is undeclared identifier
> {
> }

same as above - ElemsForOneLine is int array, and no type

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 19 Apr 2003 19:42:06 GMT  
 pointers to an array and accesing elements of the array


Quote:

> > #define volume_height 25
> > #define volume_width 25
> > #define volume_depth 25

> This is a matter of style, but many capitalize macros, like this:

>    #define VOLUME_HEIGHT   25
>    #define VOLUME_WIDTH    25
>    #define VOLUME_DEPTH    25

> This makes it clear that these are macros, and not variables; if they
> weren't macros, the next line wouldn't compile.

Thanks for the advice !

Quote:
> > int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

> This declares an array of arrays called ElemsForOneLine, not a type.
> If you wanted to make a type, look up structs or typedefs in your C
> book.

I intended to declare ElemsForOneLine  as a 2D array , I havent understood
what you meant by the declaration being an array or arrays
Would you please elaborate / direct ?

Quote:
> (As an aside, the sum of the height, width, and depth of an object
> doesn't have much meaning.  Since they are defined with respect to
> volume, I suspect you wanted to multiply.)

Agreed, it was just to fix the size of the field

Quote:
> > int siddon_projection( ElemsForOneLine *lor ); // error undeclared

> This does not work because ElemsForOneLine is not a type, but an
> array of arrays.

I didnt follow this , as I wanted ElemsForOneLine to be an array like
A[75][3] and though that the earlier declaration did the same

Quote:
> > 2. Also in the main file, the header file is included and the function
is
> > declared as

> > int siddon_projection( ElemsForOneLine &lor ) // error illegal left hand

The prototype [ i presume ] is what was defined in the .c file and the
function def in the header file. The function def. stated a pointer to a 2D
array as the input to the function and the prototype declares the address
(&lor) of the pointer. I really didnt follow the suggestion made below.

Quote:
> This is what pass-by-reference looks like in C++.  C does not have
> any such capability.  Furthermore, your prototype and your function
> definition should *not* disagree.  Change them both to something valid.

> Note that // is a syntax error in C.

> --


Please help

Thanks



Sat, 19 Apr 2003 20:05:54 GMT  
 pointers to an array and accesing elements of the array
Quote:

> ----- Original Message -----

> Newsgroups: comp.lang.c
> Sent: Tuesday, October 31, 2000 11:42 AM
> Subject: Re: pointers to an array and accesing elements of the array

> > > #define volume_height 25
> > > #define volume_width 25
> > > #define volume_depth 25

> > > int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

> > ElemsForOneLine is an int array

> that was what I had hoped

> > > int siddon_projection( ElemsForOneLine *lor ); // error undeclared
> > > identifier

> > and so you can't use it as type identifier

> what do you mean by a type identifier. By stating ElemsForOneLine *lor ,
all
> i intended was that
> lor is defined as a pointer to ElemsForOneLine . Am I wrong ? Please
> confirm.

> > > 2. Also in the main file, the header file is included and the
> > > function is
> > > declared as

> > > int siddon_projection( ElemsForOneLine &lor ) // error illegal left
> > > hand
> > > operand has type [75][3] and lor is undeclared identifier
> > > {
> > > }

> > same as above - ElemsForOneLine is int array, and no type

> woudnt &lor be the address of the first location of the array ?

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.



Sat, 19 Apr 2003 20:23:48 GMT  
 pointers to an array and accesing elements of the array

Quote:



> > > int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

> > This declares an array of arrays called ElemsForOneLine, not a type.
> > If you wanted to make a type, look up structs or typedefs in your C
> > book.

> I intended to declare ElemsForOneLine  as a 2D array , I havent understood
> what you meant by the declaration being an array or arrays

There is no such thing, strictly speaking, as a 2D array in C; it is
implemented as an array of arrays.
What naisbodo said was that you have declared ElemsForOneLine as one
instance of such an array of array, but you later use it as if it is a
type:

Quote:
> > > int siddon_projection( ElemsForOneLine *lor ); // error undeclared

> > This does not work because ElemsForOneLine is not a type, but an
> > array of arrays.

> I didnt follow this , as I wanted ElemsForOneLine to be an array like
> A[75][3] and though that the earlier declaration did the same

here. You don't use ElemsForOneLine here as an array, you use it as if
it were a type, and as if lor were a pointer to something of that type.
For example, you could also have written:

  int siddon_projection(int *lor);

In that case, lor would have been a pointer to int. As above, lor is a
pointer to (a variable of type) ElemsForOneLine; but ElemsForOneLine
isn't a type, it's a variable.

If you want ElemsForOneLine to be a type, to be precise an array of
array of int, use a typedef; you can then do this:

  ElemsForOneLine ThisLine, ThatLine;

just as you could now have done this:

  int x, y;

I could tell you exactly what typedef to use, but I think it's more
important that you learn what a typedef _is_; therefore, I advise you to
read up on typedefs in your C book, and work it out for yourself, as an
exercise.

Quote:
> > > 2. Also in the main file, the header file is included and the function
> > > is declared as

> > > int siddon_projection( ElemsForOneLine &lor ) // error illegal left hand

> The prototype [ i presume ] is what was defined in the .c file and the
> function def in the header file.

No, both are declarations, and both are prototypes; only the one in the
.c file is also a definition.
The problem is that the prototype in the header does not agree with the
prototype in the definition, and that the one in the .c file is
syntactically incorrect.

Quote:
> The function def. stated a pointer to a 2D
> array as the input to the function and the prototype declares the address
> (&lor) of the pointer.

No, it doesn't. The declaration in the header declared a pointer to (a
variable of type) ElemsForOneLine, this pointer being called lor, as the
input to the function; the declaration in the c file itself doesn't make
syntactic sense in C and should be identical to the one in the header.

Richard



Sat, 19 Apr 2003 22:30:12 GMT  
 pointers to an array and accesing elements of the array

Quote:

> Problem :
> 1.
> A 2D array is defined and I create a pointer to the array. Then pass the
> pointer to a function where the array elements are to be updated.
> However on compiling the error states that the pointer is not defined.
> Follwoing is the code in the header file

> #define volume_height 25
> #define volume_width 25
> #define volume_depth 25

> int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

> int siddon_projection( ElemsForOneLine *lor ); // error undeclared
> identifier

> 2. Also in the main file, the header file is included and the function is
> declared as

> int siddon_projection( ElemsForOneLine &lor ) // error illegal left hand
> operand has type [75][3] and lor is undeclared identifier
> {
> }

> Please would anyone help

> Thanks
> A.J

-- Hello Aby Jacob Abraham,

 May be next example will help You:

"..

#include <stdio.h>
#include <stdlib.h>

#define volume_height 25
#define volume_width 25
#define volume_depth 25

int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

int siddon_projection( int ElemsForOneLine[][3]);

int main(void)
{
 ElemsForOneLine[0][0] = 1;
 ElemsForOneLine[volume_height+volume_width+volume_depth-1][2] = 2;
 printf("siddon_projection(ElemsForOneLine) = %d\n",siddon_projection(ElemsForOneLine));

Quote:
}

int siddon_projection( int ElemsForOneLine[][3])
{
 printf("ElemsForOneLine[0][0] = %d;\nElemsForOneLine[volume_height+volume_width+volume_depth-1][2] = %d\n",ElemsForOneLine[0][
0],ElemsForOneLine[volume_height+volume_width+volume_depth-1][2]);
 return 0;

Quote:
}

.."

        Sincerely,        Alexander Fomenko



Sat, 19 Apr 2003 22:48:53 GMT  
 pointers to an array and accesing elements of the array


Quote:




> > > > int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

> > > This declares an array of arrays called ElemsForOneLine, not a type.
> > > If you wanted to make a type, look up structs or typedefs in your C
> > > book.

> > I intended to declare ElemsForOneLine  as a 2D array , I havent
understood
> > what you meant by the declaration being an array or arrays

> There is no such thing, strictly speaking, as a 2D array in C; it is
> implemented as an array of arrays.
> What naisbodo said was that you have declared ElemsForOneLine as one
> instance of such an array of array, but you later use it as if it is a
> type:

> > > > int siddon_projection( ElemsForOneLine *lor ); // error undeclared

> > > This does not work because ElemsForOneLine is not a type, but an
> > > array of arrays.

> > I didnt follow this , as I wanted ElemsForOneLine to be an array like
> > A[75][3] and though that the earlier declaration did the same

> here. You don't use ElemsForOneLine here as an array, you use it as if
> it were a type, and as if lor were a pointer to something of that type.
> For example, you could also have written:

>   int siddon_projection(int *lor);

> In that case, lor would have been a pointer to int. As above, lor is a
> pointer to (a variable of type) ElemsForOneLine; but ElemsForOneLine
> isn't a type, it's a variable.

> If you want ElemsForOneLine to be a type, to be precise an array of
> array of int, use a typedef; you can then do this:

>   ElemsForOneLine ThisLine, ThatLine;

> just as you could now have done this:

>   int x, y;

> I could tell you exactly what typedef to use, but I think it's more
> important that you learn what a typedef _is_; therefore, I advise you to
> read up on typedefs in your C book, and work it out for yourself, as an
> exercise.

> > > > 2. Also in the main file, the header file is included and the
function
> > > > is declared as

> > > > int siddon_projection( ElemsForOneLine &lor ) // error illegal left
hand

> > The prototype [ i presume ] is what was defined in the .c file and the
> > function def in the header file.

> No, both are declarations, and both are prototypes; only the one in the
> .c file is also a definition.
> The problem is that the prototype in the header does not agree with the
> prototype in the definition, and that the one in the .c file is
> syntactically incorrect.

> > The function def. stated a pointer to a 2D
> > array as the input to the function and the prototype declares the
address
> > (&lor) of the pointer.

> No, it doesn't. The declaration in the header declared a pointer to (a
> variable of type) ElemsForOneLine, this pointer being called lor, as the
> input to the function; the declaration in the c file itself doesn't make
> syntactic sense in C and should be identical to the one in the header.

> Richard

Many thanks for a comprehensive explanation. I understand my error now .


Sat, 19 Apr 2003 23:20:33 GMT  
 pointers to an array and accesing elements of the array
Aby Jacob Abraham a crit dans le message

Quote:
>> > > #define volume_height 25
>> > > #define volume_width 25
>> > > #define volume_depth 25

>> > > int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

>> > ElemsForOneLine is an int array

>> that was what I had hoped

>> > > int siddon_projection( ElemsForOneLine *lor ); // error undeclared
>> > > identifier

>> > and so you can't use it as type identifier

>> what do you mean by a type identifier. By stating ElemsForOneLine *lor ,
>all
>> i intended was that
>> lor is defined as a pointer to ElemsForOneLine . Am I wrong ?

Yes.

int siddon_projection( ElemsForOneLine *lor );

Is a prototype for the function siddon_projection() with one parameter and
returning an int.
The parameter is a pointer to an object of type ElemsForOneLine.
You never declared this type, so the compiler detects an error.

What you have declared is an object ElemsForOneLine

#define volume_height 25
#define volume_width 25
#define volume_depth 25
int ElemsForOneLine [volume_height+volume_width+volume_depth][3];

whitch (oops, Halloween effect!) is a 2D array of int.

If you want your function to accept such an array (in fact its address) you
can declare it

int siddon_projection(int lor[][3]);

Quote:
>> > > 2. Also in the main file, the header file is included and the
>> > > function is
>> > > declared as

>> > > int siddon_projection( ElemsForOneLine &lor ) // error illegal left
>> > > hand
>> > > operand has type [75][3] and lor is undeclared identifier
>> > > {
>> > > }

>> > same as above - ElemsForOneLine is int array, and no type

>> woudnt &lor be the address of the first location of the array ?

And so? A parameter must have a correct type, that's all. (see the
prototype)

int siddon_projection(int lor[][3])
{

Quote:
}

When you call the function it will be the time for passing the address of
the array:

   siddon_projection(ElemsForOneLine);

Note that an array is an L-value, so no & is required.

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Sun, 20 Apr 2003 18:51:03 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. making pointers to structure elements using array elements

2. conversion of pointer to array to pointer to its element

3. Array of pointers, pointer to array...

4. pointers to an element in an array

5. Deleting a element inside an array of pointer

6. array pointer/pointer array

7. arrays pointers array of pointers

8. Arrays of elements which are pointer to the class say myclass----------------

9. Acces the 11th element of a 10 element array

10. checkout compilers (Was: Acces the 11th element of a 10 element array)

11. Accesing array properties in COlePropPage class - VERY URGENT

12. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

 

 
Powered by phpBB® Forum Software