Source

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

What does the following code mean?

struct GUID *guid ={0};

Is it similar to a C++ virtual function?

Bill
 
J

Joona I Palaste

Bill Cunningham said:
What does the following code mean?
struct GUID *guid ={0};

It initialises a pointer to struct GUID, whatever struct GUID is. The
pointer is currently not pointing at any struct GUID.
Is it similar to a C++ virtual function?

No.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"To doo bee doo bee doo."
- Frank Sinatra
 
M

Mark A. Odell

What does the following code mean?

struct GUID *guid ={0};

Is it similar to a C++ virtual function?

No. It means, create a pointer to a struct GUID and initialize this new
pointer to 0. The {} are not necessary here. I'd write this as:

struct GUID *pGuid = NULL;

But if I actually wanted a GUID object, and not a pointer to one,
initialzed to zero I'd need to write:

struct GUID guid = { 0 };

or

struct GUID guid;

memset(guid, 0, sizeof guid);
 
M

Mike Wahler

Bill Cunningham said:
What does the following code mean?

struct GUID *guid ={0};

Create an object of type 'pointer to struct GUID',
and initialize it to NULL. (the braces are optional).
Is it similar to a C++ virtual function?

Not at all. Nothing to do with a function of any kind.

-Mike
 
B

Bill Cunningham

Mark A. Odell said:
No. It means, create a pointer to a struct GUID and initialize this new
pointer to 0. The {} are not necessary here. I'd write this as:

struct GUID *pGuid = NULL;

But if I actually wanted a GUID object, and not a pointer to one,
initialzed to zero I'd need to write:

struct GUID guid = { 0 };

or

struct GUID guid;

memset(guid, 0, sizeof guid);
Then C is an object language.

Bill
 
B

Bill Cunningham

Joona I Palaste said:
It initialises a pointer to struct GUID, whatever struct GUID is. The
pointer is currently not pointing at any struct GUID.
A struct GUID then, how would one write it. The GUID comes from MS's COM.

struct GUID (int x,int y);
The GUID struct is already defined in COM but that doesn't help me learn the
concept of struct. What I've seen looks like data type declarations and no
functions.

struct A{
int a;
char b;
double c;};

Bill
 
J

Joona I Palaste

Bill Cunningham said:
A struct GUID then, how would one write it. The GUID comes from MS's COM.
struct GUID (int x,int y);

This is a syntax error in C. Would you be wanting comp.lang.c++?
The GUID struct is already defined in COM but that doesn't help me learn the
concept of struct. What I've seen looks like data type declarations and no
functions.
struct A{
int a;
char b;
double c;};

Structs are different things in C and C++. Pick a language. If it's
really C then reply again and I will explain what structs are in C.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush
 
B

Bill Cunningham

Joona I Palaste said:
COM.

This is a syntax error in C. Would you be wanting comp.lang.c++?



Structs are different things in C and C++. Pick a language. If it's
really C then reply again and I will explain what structs are in C.

C of course. This is clc. What I was saying was that I never see functions
in structs. Just declared data types.

Bill
 
J

Joona I Palaste

Did you read this bit?
C of course. This is clc. What I was saying was that I never see functions
in structs. Just declared data types.

If it's C then your compiler will choke on:
struct GUID (int x, int y);
The fact that there aren't any functions declared inside that struct
GUID doesn't help. It won't magically make the C compiler accept syntax
errors.
Therefore I still suspect your code is really C++.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr
 
S

Sheldon Simms

But if I actually wanted a GUID object, and not a pointer to one,
initialzed to zero I'd need to write:

struct GUID guid = { 0 };
Ok.

or

struct GUID guid;
memset(guid, 0, sizeof guid);

This isn't a very good idea.
 
M

Mark A. Odell

This isn't a very good idea.

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

Mark A. Odell

Then C is an object language.

I should have used the word struct instead of object but I meant object in
a generic term, not in the C++ Object Oriented manner. C is not an object
oriented langauge "out of the box".
 
M

Mark A. Odell

C of course. This is clc. What I was saying was that I never see
functions in structs. Just declared data types.

Correct. Problem with that? You can "fake" it by putting pointers to
functions in your structs and then filling in the struct, e.g.

#include <stdio.h>

enum AppleTypes { GRANNY_SMITH, DELICIOUS, MACINTOSH };

struct Apple
{
int count;
enum AppleTypes type;
void (*init)(struct Apple *pApple);
};

static void appleInit(struct Apple *pApple)
{
pApple->count = 1234;
pApple->type = GRANNY_SMITH;
}

int main(void)
{
struct Apple apple;

apple.init = appleInit;
apple.init(&apple);

printf("Apple count is %d\n", apple.count);

return 0;
}
 
M

Matt Gregory

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.

What's wrong with it?
 
D

Default User

Bill said:
Then C is an object language.


C has objects. It, with some pain, can do object-oriented programming.
OO is not a natural idiom for C programming. If you want to do OO, there
are better languages.



Brian Rodenborn
 
A

Ashish

Joona I Palaste said:
Did you read this bit?



If it's C then your compiler will choke on:
struct GUID (int x, int y);

I think he meant struct GUID {int x, int y};
 
A

Arthur J. O'Dwyer

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

That's still a syntax error in C.
But in C++, one can legally write

struct GUID
{
GUID (int x, int y); // <-- this line
};

which is very close to what Bill wrote.
In C, one can legally write

struct GUID { int x; int y; };

which is also similar (but of course
means something completely different).

-Arthur
 
S

Severian

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

I used to do that too (back in the day using an old VAX C compiler),
but I always use the first form now. It's simpler, cleaner and works
just fine.

- Sev
 
M

Mark Gordon

What's wrong with it?

It sets all bits zero. Unfortunately all bits set to zero could be a
trap representation for some of the elements of struct GUID depending
on that type they are.
 
M

Mark McIntyre

Then C is an object language.

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.
 

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