Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > use of "->" on arrays?

Reply
Thread Tools

use of "->" on arrays?

 
 
Paminu
Guest
Posts: n/a
 
      10-11-2005
I thought that "->" was only used with pointers. Why is it that it is also
allowed to use it on arrays??

struct test {
int a;
int b;
};

struct test array[5];

void wierd()
{
array->b = 200;
printf("array->b: %d\n", array->b);

}

int main()
{
wierd();
return 1;
}


It is not allowed if I do something like:

array[1]->b = 200;

so how do I know which element that gets field b set to 200 when I type:

array->b = 200;
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      10-11-2005
In article <dihal1$thj$>, Paminu <> wrote:

>struct test {
> int a;
> int b;
>};
>
>struct test array[5];
>
>void wierd()
>{
> array->b = 200;
> printf("array->b: %d\n", array->b);
>
>}


A nice example... In most contexts, an array name is converted to a
pointer to its first element, so array->b is equivalent to (*array).b
which is equivalent to array[0].b.

-- Richard
 
Reply With Quote
 
 
 
 
Charles M. Reinke
Guest
Posts: n/a
 
      10-11-2005
"Paminu" <> wrote in message
news:dihal1$thj$...
> I thought that "->" was only used with pointers. Why is it that it is also
> allowed to use it on arrays??
>
> struct test {
> int a;
> int b;
> };
>
> struct test array[5];
>
> void wierd()
> {
> array->b = 200;
> printf("array->b: %d\n", array->b);
>
> }
>
> int main()
> {
> wierd();
> return 1;
> }
>
>
> It is not allowed if I do something like:
>
> array[1]->b = 200;
>
> so how do I know which element that gets field b set to 200 when I type:
>
> array->b = 200;


hologram>cat my_array.c
#include <stdio.h>

struct test
{
int a;
int b;
}; /* struct test */

struct test array[5];

void wierd(void) /* BTW, itz spelled "weird" */
{
int i;

array->b = 200;
printf("array->b: %d\narray[0].b: %d\n\n", array->b, array[0].b);
for(i=0; i<5; i++)
{
(array+i)->a = i;
(array+i)->b = i * i;
} /* for i */
} /* wierd */

int main(void)
{
int i;

wierd();
for(i=0; i<5; i++)
{
printf("(array+%d)->a: %d; (array+%d)->b: %d\n", i, (array+i)->a, i,
(array+i)->b);
printf("array[%d].a: %d; array[%d].b: %d\n\n", i, array[i].a, i,
array[i].b);
} /* for i */

return 0;
} /* main */
hologram>gcc -Wall -ansi -pedantic -o my_array.exe my_array.c
hologram>my_array.exe
array->b: 200
array[0].b: 200

(array+0)->a: 0; (array+0)->b: 0
array[0].a: 0; array[0].b: 0

(array+1)->a: 1; (array+1)->b: 1
array[1].a: 1; array[1].b: 1

(array+2)->a: 2; (array+2)->b: 4
array[2].a: 2; array[2].b: 4

(array+3)->a: 3; (array+3)->b: 9
array[3].a: 3; array[3].b: 9

(array+4)->a: 4; (array+4)->b: 16
array[4].a: 4; array[4].b: 16

Bottom line: there are a couple of different ways of coding the same thing
due to the *similar* properties of arrays and pointers in C. However you
will probably want to refer to section 6 of the C FAQ
(http://www.faqs.org/faqs/C-faq/faq/) to learn what the similarities (and
most important, the differences) really are.

-Charles


 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      10-11-2005
On 2005-10-11 18:26:05 -0400, "Charles M. Reinke"
<> said:

> void wierd(void) /* BTW, itz spelled "weird" */


[OT]
And "itz" is spelled "it's"
[/OT]

--
Clark S. Cox, III


 
Reply With Quote
 
g.kanaka.raju@gmail.com
Guest
Posts: n/a
 
      10-12-2005

Paminu wrote:
> I thought that "->" was only used with pointers. Why is it that it is also
> allowed to use it on arrays??
>
> struct test {
> int a;
> int b;
> };
>
> struct test array[5];
>
> void wierd()
> {
> array->b = 200;
> printf("array->b: %d\n", array->b);
>
> }
>
> int main()
> {
> wierd();
> return 1;
> }
>
>
> It is not allowed if I do something like:
>
> array[1]->b = 200;
>
> so how do I know which element that gets field b set to 200 when I type:
>
> array->b = 200;


You are correct that "->" is only used with pointers. In your exampale
"array" is nothing but a pointer. It's the base address of the array.
In other words, address of array[0].
Following is not allowed as the operator -> expects a pointer.
array[1]->b = 200;
But you can always right (array+1)->b = 200; Here array+1 is a pointer
to second element of the array.

Regards,
Raju

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2005
writes:
> Paminu wrote:
>> I thought that "->" was only used with pointers. Why is it that it is also
>> allowed to use it on arrays??

[...]
>> struct test array[5];

[...]
> You are correct that "->" is only used with pointers. In your
> exampale "array" is nothing but a pointer. It's the base address of
> the array. In other words, address of array[0].


That's both right and wrong. "array" itself is an array object, not a
pointer. The name "array", when used in an expression, is converted
to a pointer value.

> Following is not allowed as the operator -> expects a pointer.
> array[1]->b = 200;
> But you can always right (array+1)->b = 200; Here array+1 is a pointer
> to second element of the array.


Yes -- but of course it's much less clear than "array[1].b = 200;".

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
How do I know when to use the Viewstate and when to use the posted data? :-) Simon ASP .Net 1 11-09-2004 02:32 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57