473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

use of "->" on arrays?

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;
Nov 15 '05 #1
5 1412
In article <di**********@news.net.uni-c.dk>, Paminu <ja******@asd.com> 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
Nov 15 '05 #2
"Paminu" <ja******@asd.com> wrote in message
news:di**********@news.net.uni-c.dk...
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
Nov 15 '05 #3
On 2005-10-11 18:26:05 -0400, "Charles M. Reinke"
<cm******@ece.gatech.edu> said:
void wierd(void) /* BTW, itz spelled "weird" */


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

--
Clark S. Cox, III
cl*******@gmail.com

Nov 15 '05 #4

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

Nov 15 '05 #5
g.***********@gmail.com 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) ks***@mib.org <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.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Graham | last post by:
On page 89 of Stroustrup's book "The C++ Programming Language" 3rd Ed. He says that multidimensional arrays are best avoided outside low-level code. What precisely does he mean by low-level...
20
by: Pavel Stehule | last post by:
Hello, Is possible merge two arrays like array + array => array select array_append(array, array); ERROR: function array_append(integer, integer) does not exist
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
93
by: jacob navia | last post by:
In this group there is a bunch of people that call themselves 'regulars' that insist in something called "portability". Portability for them means the least common denominator. Write your code...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
4
by: javelin | last post by:
I'm defining several arrays in the following format: $farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow', 'farmanimal3' ='chicken') $housepets = array('housepet1' ='dog',...
3
by: Lax | last post by:
Isn't it "technically" meaningless to call C a "row major language," since there are no such things as multidimensional arrays in C. In C you can define arrays of arrays, and the way that the...
11
by: arnuld | last post by:
C takes input character by character. I did not find any Standard Library function that can take a word as input. So I want to write one of my own to be used with "Self Referential Structures" of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.