Thursday, 15 May 2014

c - Is the value of a struct variable just an address(like in the case of array)? -



c - Is the value of a struct variable just an address(like in the case of array)? -

in c next legal:

int a[] = {27,2}; int *b; int c; b = a; c = *b; /* c == 27 */

the point is address of array can assign pointer. why same not true struct, assume value of struct variable address , hence should able assign pointer in:

struct foo bar; struct foo *doo; bar.x = 0; doo = bar; //is legal? doo.x = 0; //why can't utilize dot?

in other words if value of struct variable address of first component(and suppose case array) above code should legal.

as others have pointed out, semantics arrays different structs. why that's case, have remember c derived before languages (bcpl , b), both of "typeless" languages saw memory linear array of fixed-length "words" or "cells". in older languages, when declared array like

auto v[10];

11 memory cells set aside; 1 object named v, , 10 more array elements; address of first element of array stored in v.

from dennis ritchie's paper the development of c language:

problems became evident when tried extend type notation, add together structured (record) types. structures, seemed, should map in intuitive way onto memory in machine, in construction containing array, there no place stash pointer containing base of operations of array, nor convenient way arrange initialized. example, directory entries of unix systems might described in c as struct { int inumber; char name[14]; }; i wanted construction not simply characterize abstract object describe collection of bits might read directory. compiler hide pointer name semantics demanded? if structures thought of more abstractly, , space pointers hidden somehow, how handle technical problem of initializing these pointers when allocating complicated object, perhaps 1 specified structures containing arrays containing structures arbitrary depth? solution constituted crucial jump in evolutionary chain between typeless bcpl , typed c. it eliminated materialization of pointer in storage, , instead caused creation of pointer when array name mentioned in expression. rule, survives in today's c, values of array type converted, when appear in expressions, pointers first of objects making array.

emphasis mine. why array expressions in c treated differently other look types, including struct types.

c pointers

No comments:

Post a Comment