473,789 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1432
In article <di**********@n ews.net.uni-c.dk>, Paminu <ja******@asd.c om> 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.c om> 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_arr ay.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.g atech.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.***********@g mail.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_Keit h) 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
2203
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 code? That is fairly relative terminology. Is he saying that the use of multidimensional arrays in C++ are not desirable? That doesn't seem right to me.
20
4722
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
2741
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 I'm pretty sure the rest of the program is working just fine. The only thing I could think might be wrong is that the if statement can only hold so many values in itself? Let me show what I'm doing: if (table001]>>5]&b&0x1f] != 0 &&
188
17439
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
93
4012
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 so that it will compile in all old and broken compilers, preferably in such a fashion that it can be moved with no effort from the embedded system in the coffe machine to the 64 bit processor in your desktop.
30
2949
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
4764
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. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
4
1466
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', 'housepet2' ='cat', 'housepet3' ='bird') $zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion', 'zooanimal3' ='zebra', 'zooanimal4' ='giraffe') I'm then running through a long text file, and detecting what part of
3
2204
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 declarations work (first index closest to identifier defines the largest arrays) makes it seems like C has row-major multidimensional arrays No need to mention "row major" when discussing C if one talks about "arrays of arrays" and the way C's...
11
6798
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 section 6.5 of K&R2. K&R2 has their own version of <getwordwhich, I think, is quite different from what I need: <getwordwill have following properties: 1.) If the word contains any number like "beauty1" or "win2e" it will
0
9502
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10389
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10185
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9006
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7523
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.