simple question about a pointer to struct initialization

M

mcheema

struct mystruct *my=malloc(sizeof *my);
Is the reason this works: that the line is actually two statements the first creates the pointer my and the second initializes it?
 
E

Eric Sosman

struct mystruct *my=malloc(sizeof *my);
Is the reason this works: that the line is actually two statements the first creates the pointer my and the second initializes it?

Technically the line is not a "statement" but a "declaration."
A "statement" is something like `while' or `return' or `x = 42'.
A "declaration" introduces and describes an identifier.

Your declaration can be read in two parts. First comes the
`struct mystruct *my' piece, which introduces the identifier `my'
as a variable and describes its type. The `=malloc(sizeof *my)'
piece is an "initializer" that assigns the new variable a starting
value. The `sizeof *my' piece is well-defined because the type
of `my' has already been established by the first part of the
declaration, before the initializer needs to be evaluated.
 
B

Ben Bacarisse

mcheema said:
struct mystruct *my=malloc(sizeof *my);
Is the reason this works: that the line is actually two statements the
first creates the pointer my and the second initializes it?

Technically, it's zero statements, because C distinguishes between
declarations (like this) and statements (like an assignment expression
on its own).

It certainly has two parts: the bit before the = could stand on its own
as a declaration (with a ; of course), and the part after the = (an
initialiser) gives the initial value for the pointer object being
declared. The value could be provided by writing a separate assignment.
 
M

mcheema

struct mystruct *my=malloc(sizeof *my);

Is the reason this works: that the line is actually two statements the first creates the pointer my and the second initializes it?

Thanks Eric and Ben for the responses. So firstly it is a declaration not a statement and secondly the type of my is defined at the point sizeof is applied to *my.
 
E

Eric Sosman

Thanks Eric and Ben for the responses. So firstly it is a declaration not a statement and secondly the type of my is defined at the point sizeof is applied to *my.

Right.

For completeness, there are a few situations where `sizeof' could
not be evaluated. One is when `struct mystruct' is an "incomplete"
type, that is, when there has been no

struct mystruct {
double trouble;
short shrift;
char broiled;
...
};

declaration earlier giving the details of the `struct mystruct' type.
(Your example didn't show such a prior declaration, but it would need
to have been there for your example to work at all, so both Ben and I
"took it as read.") You can form pointers to objects of incomplete
type, but the size (and details) of the pointed-to object is unknown.

Another is when declaring an array of unspecified size, where
the initializers determine the array size. For example,

int array[] = { 1, 2, 3, 4 }; // four elements
char string[] = "Hello, world!"; // fourteen (13+1) elements

are perfectly all right. But in these cases, the initializers
govern the array sizes so `sizeof array' and `sizeof string' aren't
known until after the initialization. Something like

int wrong[] = { 1, 2, 3, sizeof wrong, 42 };

would not work.
 
K

Keith Thompson

Eric Sosman said:
Technically the line is not a "statement" but a "declaration."
A "statement" is something like `while' or `return' or `x = 42'.
A "declaration" introduces and describes an identifier.

Your declaration can be read in two parts. First comes the
`struct mystruct *my' piece, which introduces the identifier `my'
as a variable and describes its type. The `=malloc(sizeof *my)'
piece is an "initializer" that assigns the new variable a starting
value. The `sizeof *my' piece is well-defined because the type
of `my' has already been established by the first part of the
declaration, before the initializer needs to be evaluated.

And because the operand of sizeof is not evaluated, but merely analyzed
to determine its type (unless it's a variable-length array, which
doesn't apply here).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top