C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 23rd, 2003, 01:36 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 12
Question passing structs to a function

I am having a problem accessing a struct in a function in ANSI C. I get four indentical errors that say, "request for member `abcarray' in something not a structure or union".

Can you briefly look at this code and see if I am dereferencing my pointers properly?



// struct define
struct abc
{
int abcarray[4];
};



// function define
void functionname(struct abc* xyz)
{
*xyz.abcarray[0] = 255; // this causes the errror
*xyz.abcarray[1] = 255; // this causes the errror
*xyz.abcarray[2] = 255; // this causes the errror
*xyz.abcarray[3] = 255; // this causes the errror
return;
}
__________________
Matthew Doucette / Xona.com

Last edited by Doucette : April 23rd, 2003 at 01:39 PM.

Reply With Quote
  #2  
Old April 23rd, 2003, 02:14 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 13 m 3 sec
Reputation Power: 1985
Sometimes I think that a foreign language should be required for programmers.

Think about what you are telling the computer to do:
*xyz.abcarray[0] = 255; // this causes the errror

You are telling the computer to dereference the entire expression, xyz.abcarray[0] -- specifically the integer value already contained in abcarray[0] -- and to store a value of 255 at that location being pointed to. It cannot recognize xyz as a struct because xyz was declared as a pointer to a struct, but not as a struct itself.

In order to tell it that you want to dereference xyz and access the element abcarray[0] within it, you could change that line to:
(*xyz).abcarray[0] = 255;
Now, that way it knows that you are talking about dereferencing xyz, not the entire left expression.

I mention that construction above because it can be handy at times in C++ (especially in MFC), but a far better and much more conventional way of expressing it would be:
xyz->abcarray[0] = 255;

Last edited by dwise1_aol : April 23rd, 2003 at 02:23 PM.

Reply With Quote
  #3  
Old April 23rd, 2003, 08:30 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 12
Thanks. Would you believe I already thought of that but used { } instead of ( )? You can tell that I am not use to programming in C...

Just tested it, it works. Thanks a million.

Quote:
Originally posted by dwise1_aol
Think about what you are telling the computer to do:
*xyz.abcarray[0] = 255; // this causes the errror

You are telling the computer to dereference the entire expression, xyz.abcarray[0] -- specifically the integer value already contained in abcarray[0] -- and to store a value of 255 at that location being pointed to.
Ouch. That could be deadly to my server. I ran this program about 10 times too with that bad code. [edit: nevermind, I just remembered that it did not compile with the bad code, so when I was running it it was an early copy of the program.]

Last edited by Doucette : April 24th, 2003 at 06:18 AM.

Reply With Quote
  #4  
Old April 23rd, 2003, 09:25 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 13 m 3 sec
Reputation Power: 1985
Some languages, like Ada, are written to protect the programmer from himself (actually, Ada was somewhat paranoid about that, as I recall). Then there's C, written by programmers to do what they wanted to do, that figures that you know what you're doing and if you wrote it that way, that's what you wanted to have happen.

There was a classic C book, which I unfortunately did not get, called something like "Shooting Yourself in the Foot with Your Own Rope". It spawned a serious of computer jokes which described how various languages would accomplish the same effect; eg, BASIC: you shoot your foot repeatedly with a water pistol until it gets waterlogged. One page with such a collection is at http://burks.brighton.ac.uk/burks/language/shoot.htm .

It's all part of learning and using C. And the main reason I can spot many mistakes is because I've committed them myself.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > passing structs to a function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap