| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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; |
|
#2
|
||||
|
||||
|
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
![]() |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Which of these assignments are correct? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|