Click here to Skip to main content
15,886,362 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: C declarations are half backward Pin
honey the codewitch29-Nov-20 2:32
mvahoney the codewitch29-Nov-20 2:32 
GeneralRe: C declarations are half backward Pin
Vikram A Punathambekar29-Nov-20 2:39
Vikram A Punathambekar29-Nov-20 2:39 
GeneralRe: C declarations are half backward Pin
Richard MacCutchan29-Nov-20 2:57
mveRichard MacCutchan29-Nov-20 2:57 
GeneralRe: C declarations are half backward Pin
Sander Rossel29-Nov-20 1:00
professionalSander Rossel29-Nov-20 1:00 
GeneralShameless plug Pin
Vikram A Punathambekar29-Nov-20 2:35
Vikram A Punathambekar29-Nov-20 2:35 
GeneralRe: C declarations are half backward Pin
PIEBALDconsult29-Nov-20 4:13
mvePIEBALDconsult29-Nov-20 4:13 
GeneralRe: C declarations are half backward Pin
theoldfool29-Nov-20 12:20
professionaltheoldfool29-Nov-20 12:20 
GeneralRe: C declarations are half backward Pin
Mike Winiberg29-Nov-20 21:18
professionalMike Winiberg29-Nov-20 21:18 
Without wishing to start a major language debate, I think its important to understand that C was designed to be efficient at using the underlying hardware of the machine, and the way things are declared reflects this:

char *sz declares a single variable that points to a place in memory, supposedly holding a char value. This pointer (and here's where dragons lie!) can be made to point to anywhere and to anything regardless of its declaration, you are just telling the compiler that at the place where that pointer points, YOU consider there to be a char value (whether it is or not in reality).

char sz[1024] says that at the address represented by sz is a reserved block of memory that holds (in theory) 1024 char items.

In your code you can (with some severe caveats!) use both variables in the same way and it's up to you to remember and manage whatever you think you are looking at in that location - this is both the beauty and danger of a language like C - it allows you to manipulate things in much the same way as in assembler and with as few restrictions, but it provides no protection against you doing something stupid.

However, even the early compilers would detect if you attempted to treat these two variables as exactly equivalent, but would often only warn rather than prevent it, allowing you to create absolute havoc.

Much code rot is caused by the developer incorrectly assuming that, by default, either of these variables is initialised (which is why most compilers these days will attempt to put something sensible in newly declared variables to protect the innocent). Before initialisation char *sz may contain a random address and even attempting to look at it might cause a total system crash if it accidentally points into hardware protected memory. With char sz[1024] though you can be sure that doing char x = *sz is safe because sz already holds a valid address and points into memory allocated to your program, what you don't know is what value you will actually get back from that place before the array is initialised.

Why have I bored you all with this stuff? Because so many developers these days seem to know absolutely nothing at all about how the hardware they are driving works - that's fine if you are writing in a high-level domain specific language where everything like that is hidden - no help at all if you are writing a device driver for some complex piece of hardware that is integrated into an operating system of some sort.
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 4:00
mvahoney the codewitch30-Nov-20 4:00 
GeneralRe: C declarations are half backward Pin
Mike Winiberg30-Nov-20 4:33
professionalMike Winiberg30-Nov-20 4:33 
GeneralRe: C declarations are half backward Pin
W Balboos, GHB30-Nov-20 5:20
W Balboos, GHB30-Nov-20 5:20 
GeneralRe: C declarations are half backward Pin
Joop Eggen29-Nov-20 23:25
Joop Eggen29-Nov-20 23:25 
GeneralRe: C declarations are half backward Pin
Rusty Bullet30-Nov-20 3:52
Rusty Bullet30-Nov-20 3:52 
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 3:55
mvahoney the codewitch30-Nov-20 3:55 
GeneralRe: C declarations are half backward Pin
Kirk 1038982130-Nov-20 4:18
Kirk 1038982130-Nov-20 4:18 
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 4:26
mvahoney the codewitch30-Nov-20 4:26 
GeneralRe: C declarations are half backward Pin
Kirk 1038982130-Nov-20 4:37
Kirk 1038982130-Nov-20 4:37 
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 4:46
mvahoney the codewitch30-Nov-20 4:46 
GeneralRe: C declarations are half backward Pin
Kirk 1038982130-Nov-20 6:37
Kirk 1038982130-Nov-20 6:37 
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 6:39
mvahoney the codewitch30-Nov-20 6:39 
GeneralRe: C declarations are half backward Pin
BernardIE531730-Nov-20 5:50
BernardIE531730-Nov-20 5:50 
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 5:51
mvahoney the codewitch30-Nov-20 5:51 
GeneralRe: C declarations are half backward Pin
BernardIE531730-Nov-20 6:02
BernardIE531730-Nov-20 6:02 
GeneralRe: C declarations are half backward Pin
honey the codewitch30-Nov-20 6:04
mvahoney the codewitch30-Nov-20 6:04 
GeneralRe: C declarations are half backward Pin
BernardIE531730-Nov-20 6:22
BernardIE531730-Nov-20 6:22 

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.