ot*********@hotmail.com writes:
For pricticing defination of structure in C, I wrote a small code as
follow:
#include<stdio.h>
struct datastruct
{
int data;
struct datastruct *next;
};
typedef struct datastruct dataType;
As you've seen in this thread, typedefs for structs are controversial.
Personally, I prefer not to use them; it's simpler to refer to the
type as "struct datastruct" than to invent a new name for it.
Remember that a typedef merely creates an alias for an existing type;
your type "struct datastruct" already has a perfectly good name.
On the other hand, plenty of intelligent people do like typedefs for
structs, so that the type has a simple one-word name and you don't
have to repeat the "struct" keyword every time you refer to it.
But in either case, there's no good reason to use a substantially
different name for the struct tag than for the typedef. You're using
two different names, "datastruct" and "dataType", for the same thing;
you *could* use exactly the same name for both:
struct dataType
{
int data;
struct dataType *next;
};
typedef struct dataType dataType;
or even:
typedef struct dataType
{
int data;
struct dataType *next;
} dataType;
The latter is a very common idiom. There's no ambiguity; the name
"datastruct" is interpreted as a struct tag if and only if it
immediately follows the "struct" keyword.
typedef struct datastruct *dataTypePt;
Typedefs for pointer types are less controversial; they're widely
considered to be a bad idea. For any use of the name dataTypePt, it's
important to know that it's a pointer type, so don't hide that
information behind a typedef (even if it ends in "Pt"). Just refer to
the type as "struct datastruct*" or "dataType*".
int main()
Better: "int main(void)".
{
dataType y;
dataTypePt p;
dataTypePt *Pt;
y.data=1;
p->data=2;
A bit of whitespace around the "=" operator would make this easier to
read:
y.data = 1;
p->data = 2;
/*Here, compiler says "Segmentation falt" */
No, the compiler doesn't say that. (At least I *hope* it doesn't.)
The compiler is the component that translates your C program into
executable code; once you have an executable file, the compiler's job
is done. You got that message when you ran your program. The message
came from your program, or from the runtime environment, or from the
operating system.
And as you know by now, it happened because "p" had not been
initialized. In this particular case, that means the value of "p" is
arbitrary garbage. You're lucky that the error was detected; if "p"
had happened, by chance, to point to some valid chunk of memory, you
could have silently stored the value 2 anywhere. It's not uncommon for
programs with errors like this to *appear* to work correctly, or to
fail in ways that don't obviously relate to the actual cause.
(*pt)->data=3;
printf("\n%d\n%d\n%d\n",y.data,p->data,(*pt)->data);
Spaces after the commas would make this more readable.
There's a delicate balance between too much whitespace and too little.
Consider running your code through a source code formatter; you'll
need to play with the settings to get results that you like. Look at
some existing well-written code, such as the examples in K&R2.
return 0;
Thank you for remembering to return a value from main; too many people
neglect this. Strictly speaking, this may or may not be necessary,
depending on some details I won't go into here, but it's never wrong
to return 0. [Numerous nit-picking examples where it would be wrong
to return 0 deleted.]
}
I'm confusing about the use of structure. Anyone can help me?
I've probably created more confusion. The more you know, the more you
know you don't know.
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"