Source

  • Thread starter Bill Cunningham
  • Start date
M

Mark McIntyre

What's wrong with it?

all-bits-zero might be a trap representation or other invalid value
for one of the data types within the GUID structure. For instance,
all-bits-zero might represent "Not a Number" for floating point, so
that if you later on tried to read it, you'd possibly coredump.
 
D

David Rubin

It doesn't matter what struct GUID is because guid is only a *pointer*.
You are just initializing a pointer to the value 0. This is the same as

struct GUID *guid = 0;

or

int *p = 0;

for that matter.

[snip]
I think he meant struct GUID {int x, int y};

That is still a syntax error. ITYM

struct GUID {int x; int y;};

However, this is a moot point.

/david
 
B

Bill Cunningham

I think he meant struct GUID {int x, int y};
What I'm really wanting to know I guess is how structs work and how to use
them. I used the GUID example.
For example.
typedef struct A{
int a;
double b;}/* sometimes a term */ ;
and I've seen struct A without the typedef.
What's the difference with and w/o the typedef? The structs hold data I can
see. How are they called? Is data all they hold?

This and pointers are the major sticking points for me with C. And when
there are bunches of parenthesis such as (((((*c))))))). That's confusing.
It looks like a time warp. Unions I rarely see.

Bill
 
J

Jack Klein

C defines "Object" thus.
3.14
1 object
region of data storage in the execution environment, the contents of
which can represent values

Note that this has NOTHING to do with being object-oriented which is I
suspect what you meant.

....and C++ describes it equivalently, using more words:

1.8 The C++ object model
1 The constructs in a C++ program create, destroy, refer to, access,
and manipulate objects. An object is a region of storage. [Note: A
function is not an object, regardless of whether or not it occupies
storage in the way that objects do. ] An object is created by a
definition (3.1), by a new expression
(5.3.4) or by the implementation (12.2) when needed. The properties of
an object are determined when the object is created.
An object can have a name (clause 3). An object has a storage duration
(3.7) which influences its lifetime
(3.8). An object has a type (3.9). The term object type refers to the
type with which the object is created.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Richard Heathfield

Bill said:
What I'm really wanting to know I guess is how structs work and how to use
them.

They work the same way ints work. They're just types, that's all.
I used the GUID example.
For example.
typedef struct A{
int a;
double b;}/* sometimes a term */ ;

This is a syntax error.

typedef struct A{
int a;
double b;} A;

would not be a syntax error, however. Here is the typedef again, this time
on a single line:

typedef struct A{ int a; double b;} A;

Here it is again, perhaps slightly less confusingly:

typedef struct A_tag { int a; double b;} A;


There are two parts here, which I'll separate out vertically for you:

xxxxxxx struct A_tag { int a; double b;} xx

typedef xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx A;

There's the typedef (something-as) A, and there's the struct definition.

Put them together, and you are defining A as an alias for struct A_tag.
and I've seen struct A without the typedef.
What's the difference with and w/o the typedef?

typedef creates a new synonym for an existing type. So, once the compiler
has seen the above example, you can use A wherever you would previously
have used struct A_tag - if you wish. To confuse matters, the tag is
allowed to be the same as the new type.
The structs hold data I
can see. How are they called?

You don't call a struct. It's just a bunch of data.
Is data all they hold?

Yes. That data can, in fact, include pointers to functions, but they're just
data too.
This and pointers are the major sticking points for me with C.

Get a better C book, then.
And when
there are bunches of parenthesis such as (((((*c))))))). That's confusing.

This reduces to *c)) which (on its own) is a syntax error. Don't be confused
by syntax errors. Just fix them.
 
R

Richard Heathfield

Mark said:
Are you concerned about traps? I suppose this may not be fully portable
but I admit I am guilty of doing this quite often.

That's certainly an issue if the struct contains non-integer types, but I'm
more worried about compiler diagnostics. Look at your first argument to
memset again, more closely. Then look up the definition of memset.
 
D

Dan Pop

In said:
Are you concerned about traps? I suppose this may not be fully portable
but I admit I am guilty of doing this quite often.

Not necessarily traps, but non-integer structure members may not be
initialised to zero or null pointers. Of course, this is an issue only
when portability to weird platforms is a valid concern (neither null
pointers nor floating point zero need be represented as all bits zero).

Dan
 
M

Mark A. Odell

That's certainly an issue if the struct contains non-integer types, but
I'm more worried about compiler diagnostics. Look at your first argument
to memset again, more closely. Then look up the definition of memset.

Yikes!

memset(&guid, 0, sizeof guid);

I will, from this point forth, no longer use memset() on structs even if I
know it contains no data types that can have trap values.
 
B

Bill Cunningham

So the character after the last } of a struct and before ; is for a typedef?
What about this.

struct A{
int a;
*_point;}Ab; /* is the Ab an alias? */

Bill
 
A

Arthur J. O'Dwyer

So the character after the last } of a struct and before ;
is for a typedef? What about this.

struct A{
int a;
*_point;}Ab; /* is the Ab an alias? */


Are you the same Bill Cunningham who's been posting drivel
in this newsgroup since 1996?

I hate to echo ERT, but you really should work on making
a less obvious troll; otherwise people might ignore you.

-Arthur
 
I

Irrwahn Grausewitz

Bill Cunningham said:
So the character after the last } of a struct and before ; is for a typedef?
What about this.

struct A{
int a;
*_point;}Ab; /* is the Ab an alias? */
^^^
Syntax error!Err, no: in this case 'Ab' is an object of type 'struct A';

What you probably meant is something like:

typedef
struct A
{
int a;
int *p;
}
Ab;

Here the identifier 'Ab' is defined as an alias for 'struct A', so you
can, for example, declare a variable of this type like this:

Ab bar;

instead of having to write:

struct A bar;


Of course the type you want to create an alias for does not have to be
a struct:

typedef
int
foo;

Here 'foo' is defined as type alias for int.

HTH
Regards
 
B

Bill Cunningham

What you probably meant is something like:

typedef
struct A
{
int a;
int *p;
}
Ab;

Here the identifier 'Ab' is defined as an alias for 'struct A', so you
can, for example, declare a variable of this type like this:

Ab bar;

instead of having to write:

struct A bar;


Of course the type you want to create an alias for does not have to be
a struct:

typedef
int
foo;

Here 'foo' is defined as type alias for int.

HTH
Regards
Ok great I see now thanks.

Bill
 
R

Richard Heathfield

Arthur said:
Are you the same Bill Cunningham who's been posting drivel
in this newsgroup since 1996?

The very briefest of perusals of the 1996 stuff suggests that it's written
by a different Bill Cunningham. This Bill Cunningham appears to have been
posting drivel only since 2002.
 

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

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top