C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: linked list
Goto page Previous  1, 2, 3 ... 29, 30, 31, 32  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Kaz Kylheku
Guest





PostPosted: Fri Jun 08, 2012 3:16 pm    Post subject: Re: Code problem Reply with quote



On 2012-06-08, rage <hansum.rahul (AT) gmail (DOT) com> wrote:
Quote:
Can someone please help me out on this?

Your code is not a well-formed ISO C program. It contains type system
violations that require a diagnostic.

Quote:
#include<stdio.h
main()
{
int i;
int *j=10;

Here, a variable of pointer type is being initialized with an integer value.

Quote:
i=j+19;

Here, an integer object is being assigned from a pointer expression.

Quote:
printf("%d %d\n",j,i);

Here, the %d conversion specifier, which requires a matching argument of type
int, is given an argument of type "int *". This is undefined behavior.

Quote:
}

The output is a garbage value (86) on my pc and ideone. Should it not be 29?

Since the program requires diagnostics, the ISO C standard doesn't require the
implementation to translate and execute the program. If the program translates
and executes anyway, its behavior is not defined by ISO C. Of course, it has a
behavior, but implementation-specific reasoning is required to understand it.

For instance, whether or not you can print a pointer using %d will depend
on the calling conventions, and that can be sensitive to different versions
of even the same compiler. What if pointers are 64 bits wide but int is 32
bits? In that case it is conceivable that "%d %d" will just take two halves
of the 64 bit j value, and i is ignored.

This kind of program is usually not correct even according to the local
compiler and library documentation, never mind ISO C.
Back to top
io_x
Guest





PostPosted: Fri Jun 08, 2012 3:19 pm    Post subject: Re: Code problem Reply with quote



"io_x" <a (AT) b (DOT) c.invalid> ha scritto nel messaggio
news:4fd22dcb$0$1388$4fafbaef (AT) reader2 (DOT) news.tin.it...
Quote:

"rage" <hansum.rahul (AT) gmail (DOT) com> ha scritto nel messaggio
news:7081cbd2-1ff6-4591-89c0-e7680b330790 (AT) googlegroups (DOT) com...
Can someone please help me out on this?

#include<stdio.h
main()
{
int i;
int *j=10;
i=j+19;
printf("%d %d\n",j,i);
}

The output is a garbage value (86) on my pc and ideone. Should it not be 29?

no it shuould be 10+[sizeof(int*) * 19]
if it is sizeof(int*)==4
than 10+[4 * 19] = 86
because it appear in j+19 "19" mean 19*sizeof(type(j))
'+' follow the aritmetic on array of
element size sizeof(int*)

so if p is a pointer that point to something of size M
p+19 means p+M*19 as address in char obj

yes i know it is UB
but in the place is not as above "10+[sizeof(int*) * 19] "
i think it is weak, something is not based from math...
on what??

in all above speach swap("sizeof(int*)", "sizeof(int)")
swap("sizeof(type(j))", "sizeof(type(*j))")
Back to top
Eric Sosman
Guest





PostPosted: Fri Jun 08, 2012 3:20 pm    Post subject: Re: typedef struct Reply with quote



On 6/8/2012 1:02 PM, 张源 西北工业大学 wrote:
Quote:
On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
On 5/31/2012 3:10 PM, Kulin wrote:
[...]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

I don't know what you mean by "the ambiguity of." The line
actually says two things:

1) A type named `struct A' exists. The type is "incomplete,"
meaning that there is no information about the number of bytes a
`struct A' requires or what its member elements are. Perhaps the
code will provide such details later to "complete" the type, but
no details are available yet.

2) `B' is an alias for `struct A'. `B' and `struct A' can
be used interchangeably; both refer to the exact same type. Since
`struct A' is an incomplete type, `B' is also an incomplete type
(because it is, in fact, the same type).

Quote:
struct A { // "completes" the type `struct A'
int test;
B *p1;
};

These lines "complete" the `struct A' type by describing the
struct elements. Since `struct A' is now complete, its alias `B'
is also complete: As before, `struct A' and `B' mean the same thing.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Eric Sosman
Guest





PostPosted: Fri Jun 08, 2012 3:32 pm    Post subject: Re: typedef struct Reply with quote

On 6/8/2012 1:27 PM, Դ ҵѧ wrote:
Quote:
[...]
thanks a lot,sorry for my awful english,since i'm a Chinese.
can you exlain why the code below are forbidden ?
typedef struct A {
A *p1; /* not allowed */

`struct A' is the name of a type, but `A' alone is not. The compiler
has no idea at all what `p1' should point to.

Quote:
B *p2; /* not allowed */

`B' means nothing (yet), because `B' has not been declared. The
compiler has no idea what `p2' should point to.

Quote:
} B;

This gives compilation errors.

Yes.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Les Cargill
Guest





PostPosted: Fri Jun 08, 2012 3:36 pm    Post subject: Re: Code problem Reply with quote

rage wrote:
Quote:
Can someone please help me out on this?

#include<stdio.h
main()
{
int i;
int *j=10;
i=j+19;
printf("%d %d\n",j,i);
}

The output is a garbage value (86) on my pc and ideone. Should it not be 29?



Your code is broken:

int i;
int k = 10;
int *j = &k;
i=(*j)+19;
printf("%d %d\n",*j,i);

might work as you expect. "int *j;" declares j as "a pointer
to an integer". "j = 10;" make it point to address 10.

you probably don't want that.

From that point on, you're not in Kansas any more.. you're exposing...
<puts on sunglasses>
UNDEFINED BEHAVIOR!

--
Les Cargill
Back to top
Eric Sosman
Guest





PostPosted: Fri Jun 08, 2012 3:55 pm    Post subject: Re: typedef struct Reply with quote

On 6/8/2012 1:37 PM, Դ ҵѧ wrote:
Quote:
[...]
Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.

This is not "Google group;" this is Usenet. Usenet has been around since
Google's founders were three years old. Their company does many things well,
but the "Google Groups" interface to Usenet is not among their successes.

If you are new to C, get yourself a good book. "The C Programming
Language"
by Kernighan and Ritchie is a good one if you already have experience
with other
programming languages. (It will teach you C, but will not teach you how
to write
programs.) Other books exist, some good and some not. I do not know whether
there are good C books in Chinese.

Another useful source is the comp.lang.c Frequently Asked Questions
(FAQ)
page at <http://www.c-faq.com/>. The coverage is somewhat spotty since
it tries
to address question that arise frequently, and some answers are a little
dated.
Nonetheless, it's an excellent place to look before posting a question.

There's also a Usenet group called "alt.comp.lang.learn.c-c++". I know
nothing about its quality or usefulness, but the title looks helpful.
There might
be some Chinese-language C groups or forums; I don't know.

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
John Gordon
Guest





PostPosted: Fri Jun 08, 2012 4:04 pm    Post subject: Re: Code problem Reply with quote

In <7081cbd2-1ff6-4591-89c0-e7680b330790 (AT) googlegroups (DOT) com> rage <hansum.rahul (AT) gmail (DOT) com> writes:

Quote:
Can someone please help me out on this?

#include<stdio.h
main()
{
int i;
int *j=10;
i=j+19;
printf("%d %d\n",j,i);
}

The output is a garbage value (86) on my pc and ideone. Should it not be 29?

You declared j as an integer pointer, and initialized it to point at memory
location 10, which very likely contains an unknown value.

As others have said, your compiler should have issued a warning about
this, which would have given you a clue as to what the problem was.

--
John Gordon A is for Amy, who fell down the stairs
gordon (AT) panix (DOT) com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
Back to top
James Kuyper
Guest





PostPosted: Fri Jun 08, 2012 4:31 pm    Post subject: Re: typedef struct Reply with quote

On 06/08/2012 01:20 PM, 张源 西北工业大学 wrote:
Quote:
On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
Le 31/05/2012 21:10, Kulin a écrit :
What if I want to create a pointer to the struct inside the struct?
typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

Hum, why don't you first define the type incompletely ?

typedef struct A A;
struct A {
A *p1;
A *p2;
};

can you explain why we can typedef incompletely?
such as some basic implementation on computer for this incompletely typedef?

(sorry for my awful English)

Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.
Back to top
James Kuyper
Guest





PostPosted: Fri Jun 08, 2012 4:35 pm    Post subject: Re: typedef struct Reply with quote

On 06/08/2012 02:22 PM, Zhang Yuan wrote:
....
Quote:
It's a pitty that cfaq is blocked in china. - -!

What in the world? Why do they find objectionable about the cfaq? I
known that their censors are crazy, I'm just not clear on the particular
brand of craziness that produced that decision.
Back to top
Դ ҵѧ
Guest





PostPosted: Fri Jun 08, 2012 5:02 pm    Post subject: Re: typedef struct Reply with quote

On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
Quote:
On 5/31/2012 3:10 PM, Kulin wrote:
Keith Thompson<kst-u (AT) mib (DOT) org> wrote:

John McDermick<johnthedspguy (AT) gmail (DOT) com> writes:
If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer?

struct A {
struct A *p;
};


and then refer to it as "struct A". There's no real need to use a
typedef to give the type another name; it already has a perfectly good
one.

(The counterargument is that many people like to have a one-word name
for the type.)

My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

Quote:
struct A { // "completes" the type `struct A'
int test;
B *p1;
};



Quote:

--
Eric Sosman
esosman@ieee-dot-org.invalid


On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
Quote:
On 5/31/2012 3:10 PM, Kulin wrote:
Keith Thompson<kst-u (AT) mib (DOT) org> wrote:

John McDermick<johnthedspguy (AT) gmail (DOT) com> writes:
If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer?

struct A {
struct A *p;
};


and then refer to it as "struct A". There's no real need to use a
typedef to give the type another name; it already has a perfectly good
one.

(The counterargument is that many people like to have a one-word name
for the type.)

My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration
struct A { // "completes" the type `struct A'
int test;
B *p1;
};

--
Eric Sosman
esosman@ieee-dot-org.invalid



On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
Quote:
On 5/31/2012 3:10 PM, Kulin wrote:
Keith Thompson<kst-u (AT) mib (DOT) org> wrote:

John McDermick<johnthedspguy (AT) gmail (DOT) com> writes:
If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer?

struct A {
struct A *p;
};


and then refer to it as "struct A". There's no real need to use a
typedef to give the type another name; it already has a perfectly good
one.

(The counterargument is that many people like to have a one-word name
for the type.)

My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration
struct A { // "completes" the type `struct A'
int test;
B *p1;
};

--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to top
Դ ҵѧ
Guest





PostPosted: Fri Jun 08, 2012 5:20 pm    Post subject: Re: typedef struct Reply with quote

On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
Quote:
Le 31/05/2012 21:10, Kulin a crit :
What if I want to create a pointer to the struct inside the struct?
typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

Hum, why don't you first define the type incompletely ?

typedef struct A A;
struct A {
A *p1;
A *p2;
};

can you explain why we can typedef incompletely?
such as some basic implementation on computer for this incompletely typedef?

(sorry for my awful English)
Back to top
Դ ҵѧ
Guest





PostPosted: Fri Jun 08, 2012 5:27 pm    Post subject: Re: typedef struct Reply with quote

On Saturday, June 9, 2012 1:20:16 AM UTC+8, Eric Sosman wrote:
Quote:
On 6/8/2012 1:02 PM, Դ ҵѧ wrote:
On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
On 5/31/2012 3:10 PM, Kulin wrote:
[...]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

I don't know what you mean by "the ambiguity of." The line
actually says two things:

1) A type named `struct A' exists. The type is "incomplete,"
meaning that there is no information about the number of bytes a
`struct A' requires or what its member elements are. Perhaps the
code will provide such details later to "complete" the type, but
no details are available yet.

2) `B' is an alias for `struct A'. `B' and `struct A' can
be used interchangeably; both refer to the exact same type. Since
`struct A' is an incomplete type, `B' is also an incomplete type
(because it is, in fact, the same type).

struct A { // "completes" the type `struct A'
int test;
B *p1;
};

These lines "complete" the `struct A' type by describing the
struct elements. Since `struct A' is now complete, its alias `B'
is also complete: As before, `struct A' and `B' mean the same thing.

--
Eric Sosman
esosman@ieee-dot-org.invalid



On Saturday, June 9, 2012 1:20:16 AM UTC+8, Eric Sosman wrote:
Quote:
On 6/8/2012 1:02 PM, Դ ҵѧ wrote:
On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
On 5/31/2012 3:10 PM, Kulin wrote:
[...]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

I don't know what you mean by "the ambiguity of." The line
actually says two things:

1) A type named `struct A' exists. The type is "incomplete,"
meaning that there is no information about the number of bytes a
`struct A' requires or what its member elements are. Perhaps the
code will provide such details later to "complete" the type, but
no details are available yet.

2) `B' is an alias for `struct A'. `B' and `struct A' can
be used interchangeably; both refer to the exact same type. Since
`struct A' is an incomplete type, `B' is also an incomplete type
(because it is, in fact, the same type).

struct A { // "completes" the type `struct A'
int test;
B *p1;
};

These lines "complete" the `struct A' type by describing the
struct elements. Since `struct A' is now complete, its alias `B'
is also complete: As before, `struct A' and `B' mean the same thing.

--
Eric Sosman
esosman@ieee-dot-org.invalid

thanks a lot,sorry for my awful english,since i'm a Chinese.
can you exlain why the code below are forbidden ?
typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors.
Back to top
Դ ҵѧ
Guest





PostPosted: Fri Jun 08, 2012 5:37 pm    Post subject: Re: typedef struct Reply with quote

On Saturday, June 9, 2012 1:32:26 AM UTC+8, Eric Sosman wrote:
Quote:
On 6/8/2012 1:27 PM, ��Դ ������ҵ��ѧ wrote:
[...]
thanks a lot,sorry for my awful english,since i'm a Chinese.
can you exlain why the code below are forbidden ?
typedef struct A {
A *p1; /* not allowed */

`struct A' is the name of a type, but `A' alone is not. The compiler
has no idea at all what `p1' should point to.

B *p2; /* not allowed */

`B' means nothing (yet), because `B' has not been declared. The
compiler has no idea what `p2' should point to.

} B;

This gives compilation errors.

Yes.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.
Back to top
Zhang Yuan
Guest





PostPosted: Fri Jun 08, 2012 6:22 pm    Post subject: Re: typedef struct Reply with quote

On Saturday, June 9, 2012 1:55:22 AM UTC+8, Eric Sosman wrote:
Quote:
On 6/8/2012 1:37 PM, 锟斤拷源 锟斤拷锟斤拷锟斤拷业锟斤拷学 wrote:
[...]
Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.

This is not "Google group;" this is Usenet. Usenet has been around since
Google's founders were three years old. Their company does many things well,
but the "Google Groups" interface to Usenet is not among their successes.

If you are new to C, get yourself a good book. "The C Programming
Language"
by Kernighan and Ritchie is a good one if you already have experience
with other
programming languages. (It will teach you C, but will not teach you how
to write
programs.) Other books exist, some good and some not. I do not know whether
there are good C books in Chinese.

Another useful source is the comp.lang.c Frequently Asked Questions
(FAQ)
page at <http://www.c-faq.com/>. The coverage is somewhat spotty since
it tries
to address question that arise frequently, and some answers are a little
dated.
Nonetheless, it's an excellent place to look before posting a question.

There's also a Usenet group called "alt.comp.lang.learn.c-c++". I know
nothing about its quality or usefulness, but the title looks helpful.
There might
be some Chinese-language C groups or forums; I don't know.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Thank you.
the quality of this usenet is higher than those in china .
chinese students prefer to read american books rather than natives.
i'm reading c primer plus now.
thank you,
It's a pitty that cfaq is blocked in china. - -!
Back to top
Zhang Yuan
Guest





PostPosted: Fri Jun 08, 2012 6:41 pm    Post subject: Re: typedef struct Reply with quote

On Saturday, June 9, 2012 2:35:04 AM UTC+8, James Kuyper wrote:
Quote:
On 06/08/2012 02:22 PM, Zhang Yuan wrote:
...
It's a pitty that cfaq is blocked in china. - -!

What in the world? Why do they find objectionable about the cfaq? I
known that their censors are crazy, I'm just not clear on the particular
brand of craziness that produced that decision.

Me neither.use some software I can go to cfaq.
This is not difficult for students in china .
pitty.sometimes ,i even can't go to google group.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page Previous  1, 2, 3 ... 29, 30, 31, 32  Next
Page 30 of 32

 
 


Powered by phpBB © 2001, 2006 phpBB Group