Click here to Skip to main content
15,926,703 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionarray -- foo, &foo and &foo[0] Pin
George_George13-Aug-07 18:36
George_George13-Aug-07 18:36 
AnswerRe: array -- foo, &foo and &foo[0] Pin
jimfisher13-Aug-07 18:55
jimfisher13-Aug-07 18:55 
GeneralRe: array -- foo, &foo and &foo[0] Pin
George_George13-Aug-07 20:04
George_George13-Aug-07 20:04 
GeneralRe: array -- foo, &foo and &foo[0] Pin
jimfisher14-Aug-07 5:45
jimfisher14-Aug-07 5:45 
GeneralRe: array -- foo, &foo and &foo[0] Pin
George_George14-Aug-07 16:11
George_George14-Aug-07 16:11 
AnswerRe: array -- foo, &foo and &foo[0] Pin
Iain Clarke, Warrior Programmer13-Aug-07 22:46
Iain Clarke, Warrior Programmer13-Aug-07 22:46 
GeneralRe: array -- foo, &foo and &foo[0] Pin
George_George13-Aug-07 23:23
George_George13-Aug-07 23:23 
GeneralRe: array -- foo, &foo and &foo[0] Pin
Iain Clarke, Warrior Programmer13-Aug-07 23:36
Iain Clarke, Warrior Programmer13-Aug-07 23:36 
There's nothing wrong with using &foo - but not as a number. Use it as a "pointer to an array" and let the compiler worry about location details.

"Is it wrong to do..." is not something I can possibly answer in the general case.

But taking your strcpy example...

char foo [100];

strcpy (foo, "iain");   is fine
strcpy (&(foo[0]), "iain"); is fine, but ugly
strcpy (&foo, "iain"); is syntactically wrong, and I'm surprised you don't get a warning at least. It just happens to work on this compiler.


char *bar = new char [100];

strcpy (bar, "iain");   is fine
strcpy (&(bar[0]), "iain"); is fine, but ugly
strcpy (&bar, "iain"); will fail - and mostlikely crash stuff.


As you see, the only difference between "getting away with it", and "failing" is what's written a few lines up, relying on &array == array is a really bad idea, even if you will always get away with it. Imagine you come back in 3 years, your boss says "this needs to be longer - make it dynamically sized", and your code will crash with you being puzzled.

Iain.



GeneralRe: array -- foo, &foo and &foo[0] Pin
George_George13-Aug-07 23:50
George_George13-Aug-07 23:50 
QuestionDisabling shortcut in Visual C++ [modified] Pin
jimfisher13-Aug-07 18:15
jimfisher13-Aug-07 18:15 
AnswerRe: Disabling shortcut in Visual C++ Pin
Iain Clarke, Warrior Programmer13-Aug-07 22:56
Iain Clarke, Warrior Programmer13-Aug-07 22:56 
QuestionRe: Disabling shortcut in Visual C++ Pin
David Crow14-Aug-07 3:12
David Crow14-Aug-07 3:12 
AnswerRe: Disabling shortcut in Visual C++ Pin
jimfisher14-Aug-07 5:20
jimfisher14-Aug-07 5:20 
Questionfatal error C1001: INTERNAL COMPILER ERROR Pin
itkid13-Aug-07 18:11
itkid13-Aug-07 18:11 
AnswerRe: fatal error C1001: INTERNAL COMPILER ERROR Pin
jimfisher13-Aug-07 18:20
jimfisher13-Aug-07 18:20 
GeneralRe: fatal error C1001: INTERNAL COMPILER ERROR Pin
itkid15-Aug-07 20:35
itkid15-Aug-07 20:35 
Questionhow to know a double is infinite directly? Pin
includeh1013-Aug-07 18:00
includeh1013-Aug-07 18:00 
GeneralRe: how to know a double is infinite directly? Pin
Maxwell Chen13-Aug-07 19:04
Maxwell Chen13-Aug-07 19:04 
AnswerRe: how to know a double is infinite directly? Pin
Mark Salsbery14-Aug-07 6:05
Mark Salsbery14-Aug-07 6:05 
QuestionApplication that use DataGrid ActiveX Control dont work without VC++ installed Pin
xanagan13-Aug-07 17:13
xanagan13-Aug-07 17:13 
AnswerRe: Application that use DataGrid ActiveX Control dont work without VC++ installed Pin
Maxwell Chen13-Aug-07 17:46
Maxwell Chen13-Aug-07 17:46 
AnswerRe: Application that use DataGrid ActiveX Control dont work without VC++ installed Pin
Paresh Chitte13-Aug-07 18:51
Paresh Chitte13-Aug-07 18:51 
GeneralVisual Studio 2003/2005 name mangling Pin
Harald Krause13-Aug-07 15:42
Harald Krause13-Aug-07 15:42 
GeneralRe: Visual Studio 2003/2005 name mangling Pin
Maxwell Chen13-Aug-07 17:08
Maxwell Chen13-Aug-07 17:08 
QuestionDrawing Data Flow Digram Pin
Rajesh Rajan Pankapattu13-Aug-07 8:48
Rajesh Rajan Pankapattu13-Aug-07 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.