473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int pointing to char

this piece of code assigns an int pointer(evident ) to a char,
and when i try to access the ascii value of the char through the
integer pointer(p) ,

what i get is a junk value or not i don't know !
'cause it shows consistent values for diff. chars , for eg.
c = 'a' ans=>-9119

c = 'b' ans=>-9118

c = 'c' ans=>-9117

... and so on
#include <stdio.h>

void main()
{
char c = 'a';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
clrscr();
pf("\n%d", *p);
getch();
}

. . . . whats happening ?

Gautam
Nov 14 '05
23 1947
James Hu wrote:

On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote:
> this piece of code assigns an int pointer(evident ) to a char,
> and when i try to access the ascii value of the char through the
> integer pointer(p) ,
> what i get is a junk value or not i don't know !

> #include <stdio.h>
>
> void main()
> {
> char c = 'a';
> int * p;
>
> p = &c; /*suspicious pointer conversion WARNING !*/
> clrscr();
> pf("\n%d", *p);
> getch();
> }

> . . . . whats happening ?

You are lying to the compiler about what p is pointing to. Assuming
that pf() in your program is not #defined to be an empty statement,
your program results in undefined behavior, so anything could happen.


Actually, undefined behaviour is introduced by "void main()",


I generally ignore this, since it may be valid in a freestanding
environment.
and also by the line "p = &c;" (for example, it could produce
a hardware exception on a device which requires int pointers
to be correctly aligned).


What would such hardware do when presented with:

char c = 'a';
void *q = &c;
int *p = q;

?
It is UB to point a pointer to an object of an incompatible type.


I don't believe a constraint violation results in undefined behavior,
but IANACLL (xref: 6.5.16.1p1 and 4p1).


If a constraint is violated,
then you don't know what the code means,
and that's undefined behavior.

N869
3. Terms and definitions
3.6
[#1] constraints
restrictions, both syntactic and semantic, by which the
exposition of language elements is to be interpreted

--
pete
Nov 14 '05 #11
In <84************ **************@ posting.google. com> ol*****@inspire .net.nz (Old Wolf) writes:
> this piece of code assigns an int pointer(evident ) to a char,
> and when i try to access the ascii value of the char through the
> integer pointer(p) ,
> what i get is a junk value or not i don't know !
> #include <stdio.h>
>
> void main()
> {
> char c = 'a';
> int * p;
>
> p = &c; /*suspicious pointer conversion WARNING !*/
> clrscr();
> pf("\n%d", *p);
> getch();
> }

> . . . . whats happening ?


You are lying to the compiler about what p is pointing to. Assuming
that pf() in your program is not #defined to be an empty statement,
your program results in undefined behavior, so anything could happen.


Actually, undefined behaviour is introduced by "void main()",
and also by the line "p = &c;"


Nope, this is a constraint violation and requires a diagnostic. Mere
undefined behaviour doesn't.
(for example, it could produce
a hardware exception on a device which requires int pointers
to be correctly aligned). It is UB to point a pointer to an
object of an incompatible type.


Only in certain cases, when the incompatible type has looser alignment
requirements. Character pointers (of all three types) can be pointed to
any object. Pointers to unsigned char can even be safely dereferenced
after that. A pointer to int can be safely made to point to a struct
whose first member has type int. It can be even dereferenced, if the
struct member has already been initialised.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
James Hu wrote:

On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote:
> It is UB to point a pointer to an object of an incompatible type.


I don't believe a constraint violation results in undefined behavior,
but IANACLL (xref: 6.5.16.1p1 and 4p1).


If a constraint is violated,
then you don't know what the code means,
and that's undefined behavior.


This is correct. The important point is that the constraint violation
*requires* a diagnostic, while a mere invocation of undefined behaviour
doesn't.

After the diagnostic is produced, there is no difference between the
two, because the standard doesn't specify the semantics of any code
that contains syntax errors or constraint violations.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
On 2004-03-04, Dan Pop <Da*****@cern.c h> wrote:
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
James Hu wrote:

On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote:

> It is UB to point a pointer to an object of an incompatible type.

I don't believe a constraint violation results in undefined behavior,
but IANACLL (xref: 6.5.16.1p1 and 4p1).


If a constraint is violated,
then you don't know what the code means,
and that's undefined behavior.


This is correct. The important point is that the constraint violation
*requires* a diagnostic, while a mere invocation of undefined behaviour
doesn't.

After the diagnostic is produced, there is no difference between the
two, because the standard doesn't specify the semantics of any code
that contains syntax errors or constraint violations.


Like I said, IANACLL, but my understanding was that the behavior of a
constraint violation is unspecified behavior, not undefined behavior.

A program can still be correct if it contains unspecified behavior
(xref: 4p3).

-- James
Nov 14 '05 #14
On 2004-03-04, James Hu <jx*@despammed. com> wrote:
On 2004-03-04, Dan Pop <Da*****@cern.c h> wrote:
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
James Hu wrote:

On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote:

> It is UB to point a pointer to an object of an incompatible type.

I don't believe a constraint violation results in undefined behavior,
but IANACLL (xref: 6.5.16.1p1 and 4p1).
I should qualify that with "c.v. doesn't necessarily result in u'd.b.".
If a constraint is violated,
then you don't know what the code means,
and that's undefined behavior.


This is correct. The important point is that the constraint violation
*requires* a diagnostic, while a mere invocation of undefined behaviour
doesn't.

After the diagnostic is produced, there is no difference between the
two, because the standard doesn't specify the semantics of any code
that contains syntax errors or constraint violations.


Like I said, IANACLL, but my understanding was that the behavior of a
constraint violation is unspecified behavior, not undefined behavior.


Since I have only be looking at the assignment operator, this statement
should be restricted to the assignment in question:

char c = 'a';
int *p = &a;

-- James
Nov 14 '05 #15
In <N7************ ********@comcas t.com> James Hu <jx*@despammed. com> writes:
On 2004-03-04, Dan Pop <Da*****@cern.c h> wrote:
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
James Hu wrote:

On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote:

> It is UB to point a pointer to an object of an incompatible type.

I don't believe a constraint violation results in undefined behavior,
but IANACLL (xref: 6.5.16.1p1 and 4p1).

If a constraint is violated,
then you don't know what the code means,
and that's undefined behavior.
This is correct. The important point is that the constraint violation
*requires* a diagnostic, while a mere invocation of undefined behaviour
doesn't.

After the diagnostic is produced, there is no difference between the
two, because the standard doesn't specify the semantics of any code
that contains syntax errors or constraint violations.


Like I said, IANACLL, but my understanding was that the behavior of a
constraint violation is unspecified behavior, not undefined behavior.


3.4.4
1 unspecified behavior
behavior where this International Standard provides two or more
possibilities and imposes no further requirements on which is
chosen in any instance

When a constraint is violated, the standard doesn't provide *any*
possibilities the implementation can choose from, either explicitly or
implicitly.

For example:

6.5.2.1 Array subscripting

Constraints

1 One of the expressions shall have type ``pointer to object type'',
the other expression shall have integer type, and the result
has type ``type''.

What are the allowed possibilities for the type of a[b] when both a and b
have type double?
A program can still be correct if it contains unspecified behavior
(xref: 4p3).


But a program CANNOT be correct if it violates a constraint. That's why
they're called "constraint s" in the first place.

A correct C program (from the standard's POV) is a program that
doesn't require any diagnostic and doesn't invoke undefined behaviour.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #16
In <Uo************ ********@comcas t.com> James Hu <jx*@despammed. com> writes:
On 2004-03-04, James Hu <jx*@despammed. com> wrote:
On 2004-03-04, Dan Pop <Da*****@cern.c h> wrote:
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:

James Hu wrote:
>
> On 2004-03-04, Old Wolf <ol*****@inspir e.net.nz> wrote:
>
> > It is UB to point a pointer to an object of an incompatible type.
>
> I don't believe a constraint violation results in undefined behavior,
> but IANACLL (xref: 6.5.16.1p1 and 4p1).
I should qualify that with "c.v. doesn't necessarily result in u'd.b.".
If a constraint is violated,
then you don't know what the code means,
and that's undefined behavior.

This is correct. The important point is that the constraint violation
*requires* a diagnostic, while a mere invocation of undefined behaviour
doesn't.

After the diagnostic is produced, there is no difference between the
two, because the standard doesn't specify the semantics of any code
that contains syntax errors or constraint violations.


Like I said, IANACLL, but my understanding was that the behavior of a
constraint violation is unspecified behavior, not undefined behavior.


Since I have only be looking at the assignment operator, this statement
should be restricted to the assignment in question:

char c = 'a';
int *p = &c;


The C standard doesn't classify constraint violations in several
categories. ALL of them have the same effect: a diagnostic is required
after which there are no more requirements from the implementation.
An implementation aborting the translation process after issuing a
required diagnostic would be perfectly conforming.

If all C compilers failed to translate code *requiring* a diagnostic,
C would be a better known language. Far too many incompetents discard
ALL the warnings by default, without trying to understand if they signal
a real problem or not. If the program appears to work as expected, it
*must* be correct.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #17
> >> > char c = 'a';
> int * p;
>
> p = &c; /*suspicious pointer conversion WARNING !*/ [snips] > . . . . whats happening ?

You are lying to the compiler about what p is pointing to. Assuming
that pf() in your program is not #defined to be an empty statement,
your program results in undefined behavior, so anything could happen.
Actually, undefined behaviour is introduced by "void main()",


I generally ignore this, since it may be valid in a freestanding
environment.


I generally ignore freestanding environments, as you could raise
the objection "it may be valid in a f.e." to just about anything
posted on c.l.c.
FWIW, void main() may be valid in a hosted environment too.
and also by the line "p = &c;" (for example, it could produce
a hardware exception on a device which requires int pointers
to be correctly aligned).


What would such hardware do when presented with:

char c = 'a';
void *q = &c;
int *p = q;


The same thing. You may be thinking of the requirement that a pointer
to object type may be cast to (void *) and back again later _to the
original type_. That is not happening in this situation.

I program in an environment where this does generate a hardware exception
(but that does not resolve this argument, as it could just be that
this environment is non-conforming)
I don't believe a constraint violation results in undefined behavior,
but IANACLL (xref: 6.5.16.1p1 and 4p1).


It seems to me that the Standard only has something to say on topics
where its constraints are met, so this case is outside of the Standard's
scope, therefore UB. IANACLL either though :)
Nov 14 '05 #18
> >> > char c = 'a';
> int * p;
>
> p = &c; /*suspicious pointer conversion WARNING !*/
Actually, undefined behaviour is introduced by "void main()",
and also by the line "p = &c;"


Nope, this is a constraint violation and requires a diagnostic. Mere
undefined behaviour doesn't.


This sentence seems to imply that constraint violations
are all UB (otherwise, I fail to understand your use of 'mere')
(for example, it could produce
a hardware exception on a device which requires int pointers
to be correctly aligned). It is UB to point a pointer to an
object of an incompatible type.


Only in certain cases, when the incompatible type has looser alignment
requirements. Character pointers (of all three types) can be pointed to
any object. Pointers to unsigned char can even be safely dereferenced
after that. A pointer to int can be safely made to point to a struct
whose first member has type int. It can be even dereferenced, if the
struct member has already been initialised.


I agree totally, but the case in point (pointer to int being made to
point to a char) is not in your list of acceptable pointings.
Nov 14 '05 #19
On 2004-03-04, Dan Pop <Da*****@cern.c h> wrote:
In <Uo************ ********@comcas t.com> James Hu <jx*@despammed. com> writes:
On 2004-03-04, James Hu <jx*@despammed. com> wrote:
>> [snip]>> I don't believe a constraint violation results in undefined behavior,
>> but IANACLL (xref: 6.5.16.1p1 and 4p1).
I should qualify that with "c.v. doesn't necessarily result in u'd.b.".
[snip]
Like I said, IANACLL, but my understanding was that the behavior of a
constraint violation is unspecified behavior, not undefined behavior.


Since I have only be looking at the assignment operator, this statement
should be restricted to the assignment in question:

char c = 'a';
int *p = &c;


BTW, if it wasn't clear before, my xref's are against C99.

The C standard doesn't classify constraint violations in several
categories. ALL of them have the same effect: a diagnostic is required
after which there are no more requirements from the implementation.
An implementation aborting the translation process after issuing a
required diagnostic would be perfectly conforming.
I think this is true of C89. But, C99 seems to have changed things.

For one thing:

xref 4p2: If a "shall" or "shall not" requirement that appears
outside of a constraint is violated, the behavior is undefined.

And this sentence is repeated in the Appendix the summarizes
undefined behaviors. If a constraint violation implied undefined
behavior, I believe the standard would not have bothered to state
this exception.

As to the particular assignment, there is a curious example in the
standard:

xref 6.5.16.1p6: EXAMPLE3: Consider the fragment:

const char **cpp;
char *p;
const char c = 'A';

cpp = &p; // constraint violation
*cpp = &c; // valid
*p = 0; // valid

The first assignment is unsafe because it would allow the
following valid code to attempt to change the value of the
const object c.

It seems the standard is implying the statement with the constraint
violation still has valid semantics.
If all C compilers failed to translate code *requiring* a diagnostic,
C would be a better known language.
Better known? Perhaps just better...
Far too many incompetents discard
ALL the warnings by default, without trying to understand if they signal
a real problem or not.


This point cannot be made often enough.

-- James
Nov 14 '05 #20

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

Similar topics

1
2085
by: Rebecca Hoffmann | last post by:
Hi, I have a serious problem while compiling a small project (a part of the Modular Flow Scheduling Middleware: ex1): There are 3 linker errors, all from symbols that point to templates: -- verbose build output -------------------------------------------- ex1.obj : error LNK2001: unresolved external symbol "public: virtual
19
5887
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
31
2205
by: Andrej Prsa | last post by:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a) {
1
1162
by: Tran Hong Quang | last post by:
Hi, I have code like this: unsigned char *buf *buf=1234567; How I detect the the size of data buf is pointing to? (In this case is 7) Thanks
7
1924
by: william | last post by:
My question is: Specific memory block where my pointer pointing to changed strangely, seemingly that no statement changed it. Here are two examples I got: ***********1***************** I was about to read from a floppy image and build a tree for all the directories and files. My question is only about a small portion where I had debugging problem, and I marked the place below at two places using "<======================"(you can try to...
14
1497
by: jois.de.vivre | last post by:
Hello, I was wondering if the following code was ok: // ----------- start code #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char** argv) {
7
3517
by: Martin | last post by:
When referring to the conforming declaration for main, Lint displays Info 818: Pointer parameter 'argv' (line 3) could be declared as pointing to const Presumably it's saying that the definition could be: int main(int argc, char *const *argv) Why didn't C89 mandate that?
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.