C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 6th, 2006, 11:01 AM
jandali jandali is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 18 jandali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 6 m 23 sec
Reputation Power: 0
Which of these assignments are correct?

1) Which of these assignments are correct? If they are wrong, why?

int a[5];
int b[6];

a = b;
*a = b;
*a = *b;
a = *b;

2) Which of these assignments are correct? If they are wrong, why?

int a[5];
int *b;

b = a;
*b = a;
b = *a;
*b = *a;
a = b;
*a = *b;
a = *b;
*a = b;

Reply With Quote
  #2  
Old March 6th, 2006, 11:09 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
They're all correct, though very few will do anything useful...
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #3  
Old March 6th, 2006, 03:12 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
some of these are OK (a few), some require casts, and some are wrong. I did check my answers with a compiler (gcc):

1) Which of these assignments are correct? If they are wrong, why?

int a[5];
int b[6];

a = b; //WRONG: array assignment is not allowed
*a = b; //(requires cast) assigns the first element of b to the first element of a
*a = *b; //same as above
a = *b; //WRONG: array names are constant pointers

2) Which of these assignments are correct? If they are wrong, why?

int a[5];
int *b;

b = a; //b points to the first element of a
*b = a; //WRONG: a is an int*, b is an int
b = *a; //(requires cast) b points to the value at the memory address contained in the first element of a
*b = *a; //assigns first element of a to pointed-to-by-b (note that without any code to assign b to point to anything, this will most likely cause a segmentation fault)
a = b; //WRONG: array names are constant pointers
*a = *b; //assigns first element of a to pointed-to-by-b (see note at *b=*a)
a = *b; //WRONG: array names are constant pointers
*a = b; //(requires cast) assigns first element of a to memory address contained in b

Reply With Quote
  #4  
Old March 6th, 2006, 07:06 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Hmmm, your results amaze me.

If you assign an array b[5], b is NOT the value of the first member. It's the ADDRESS of the first member, as if you'd declared it as a pointer (in fact, it is a pointer, you can even increment it, IIRC)

Assignments of the type *a = b are also legal, even if both are pointers, as memory addresses have the same size as 'native' integers (in most cases, 32 bits, or nowadays 64 bits)

Like I said, all statements are legal, though it might be that some compilers don't like it if you reassign the address of an array you did not assign though calloc() or malloc(). (the '//WRONG: array names are constant pointers' error), I've worked on compilers that let you get away with it though.

Reply With Quote
  #5  
Old March 6th, 2006, 07:16 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
you're right Itscan about the address of the first member, my bad
I didn't say those were wrong, but on gcc you get the error "incompatible types int and int*" unless you cast

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Which of these assignments are correct?


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT