473,421 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

A Stupid Question about Structure

Hi, all,

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;
typedef struct datastruct *dataTypePt;

int main()
{
dataType y;
dataTypePt p;
dataTypePt *Pt;
y.data=1;
p->data=2;

/*Here, compiler says "Segmentation falt" */
(*pt)->data=3;
printf("\n%d\n%d\n%d\n",y.data,p->data,(*pt)->data);

return 0;
}

I'm confusing about the use of structure. Anyone can help me?
Feb 1 '08 #1
7 1571
On Feb 1, 4:23*pm, ot_007_2...@hotmail.com wrote:
typedef struct datastruct dataType;
typedef struct datastruct *dataTypePt;
typedefs like these are usually purposeless obfuscations.
* * dataTypePt p;
OK, you've allocated memory for a pointer.
It's automatic, so uninitialized (i.e., garbage).
(If static it would be initialized to NULL.)
* * p->data=2;
"Dereferencing" an uninitialized pointer?
Wrong.
* * /*Here, compiler says "Segmentation falt" */
Whew, you lucked out!
(Some wise-acre will point out, the compiler
"would have been within its rights" to format
your hard disk!)
I'm confusing about the use of structure. Anyone can help me?
Not structures. You're confused about pointers,
or rather the need to give them values.

James

Feb 1 '08 #2
ot*********@hotmail.com wrote:
Hi, all,

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;
typedef struct datastruct *dataTypePt;
You'll probably find many people suggesting that you don't use
typedef for pointers, but you may chose to ignore them...

You've chosen poor names for your structures and types, in my
opinion, by the way.
int main()
{
dataType y;
so y is a structure. OK
dataTypePt p;
So p is a pointer to structure, but hasn't been initialised to point to
a structure...
dataTypePt *Pt;
Pt is a pointer to a pointer to structure and again hasn't been
initialised.
y.data=1;
That's fine.
p->data=2;
So you are trying to follow an unitialised pointer... Not a good idea.
/*Here, compiler says "Segmentation falt" */
I doubt it. I'd worry about a compiler that had a segmentation fault.

It's more likely that your program builds and then fails when you run
it.
(*pt)->data=3;
Similar problems - following an unitialized pointer, trying to use
whatever you find as a pointer to structure.
printf("\n%d\n%d\n%d\n",y.data,p->data,(*pt)->data);

return 0;
}

I'm confusing about the use of structure. Anyone can help me?
Try reading a good textbook (I like Kernighan and Ritchie) and the C FAQ
at www.c-faq.com
Feb 1 '08 #3
Thank you all:)

Now, the biggest problem seems to be the pointer pointing to nothing.

To initialise a struct pointer, should I use the malloc(), or I have
to get it point to a struct variable?

Feb 1 '08 #4
ot*********@hotmail.com wrote:
Thank you all:)

Now, the biggest problem seems to be the pointer pointing to nothing.

To initialise a struct pointer, should I use the malloc(), or I have
to get it point to a struct variable?
It entirely depends on what you are trying to achieve...

I think most of us feel that you need to spend more time with a good
textbook or tutorial (or both) rather than trying to learn by tinkering.
Feb 1 '08 #5
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"
Feb 1 '08 #6
James Dow Allen wrote:
>
On Feb 1, 4:23 pm, ot_007_2...@hotmail.com wrote:
[...]
p->data=2;

"Dereferencing" an uninitialized pointer?
Wrong.
/*Here, compiler says "Segmentation falt" */

Whew, you lucked out!
(Some wise-acre will point out, the compiler
"would have been within its rights" to format
your hard disk!)
[...]

Consider a system without memory protection (real-mode MS-DOS comes
to mind). Consider the possible ramifications of storing a "2" in
some random memory location. Reformatting your hard drive, possibly
two days later, while very unlikely, is not impossible. (And, as
far as the C standard is concerned, it's a perfectly legal outcome.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Feb 1 '08 #7
Keith Thompson <ks***@mib.orgwrites:
[...]
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.
[...]

One thing I forgot to mention: there's no ambiguity to the compiler,
but some development environments provide a command to show or jump to
the definition of an identifier when you click on it or otherwise
indicate it. Such environments might not necessarily know the
difference between a typedef and a struct tag (though they *could*
look for a preceding "struct" keyword). In such an environment, *if*
you choose to use typedefs for structs, there might be some advantage
in using distinct identifiers for the struct tag and for the typedef.
But in that case it's still a good idea to have a simple convention
for deriving one from the other. For example, since you're presumably
going to be using the typedef name much more often than the struct
tag, you might append a "_s" suffix to the tag:

typedef struct dataType_s
{
int data;
struct dataType_s *next;
} dataType;

Or even just a single underscore.

--
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"
Feb 1 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: knoak | last post by:
Hi there, I'm building a website about animals. There is a mySQL DB behind it. There are 2 tables: 1 - species 2 - animals Species could be: birds, fishes etc. Animals would be: hawk, eagle...
8
by: sebb | last post by:
I'm kind of newbie to programming, but I thought of something and I want some opinions on that. It's about a new instruction block to do some cycles. I thought about that because it's not very...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
36
by: Hoopster | last post by:
Hello, I know nothing about C++ but want to get started. Is there any good free C++ program that I can try to see if I like programming? I also need a good free compiler. I don't want to...
2
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int...
15
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
7
by: Gina_Marano | last post by:
Hey guys, I am used to text files and such but thought I better get modern. I want to store/read the following (example) in an xml file: A list of DVDs each person has: Amy On Golden Pond
1
by: mistersteve | last post by:
Alright guys, so I'm writting in c++ and I have an instance of the priority_queue class which i am filling with instances of this structure: struct app { int idTag; int date, hours,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.