473,785 Members | 2,640 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 #1
23 1943
On 2004-03-03, Gautam <ga********@yah oo.com> 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 !
'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();
}
Your compiler seems to be doing its job, according to your comment,
and is warning you that you are doing something wrong. This means
you should fix it. Change the type of c so that p can point to it.

Since you appear to be learning C, you should try to stick to the
standard language features. pf(), clrscr() and getch() are not part of
the standard C language. In standard C in a hosted environment, main
returns an int.

#include <stdio.h>

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

p = &c;
printf("%d\n", *p);
return 0;
}
. . . . 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.

-- James
Nov 14 '05 #2
In <44************ *************@p osting.google.c om> ga********@yaho o.com (Gautam) writes:
this piece of code assigns an int pointer(evident ) to a char,
Nope, it doesn't.
and when i try to access the ascii value of the char through the
integer pointer(p) ,
An operation devoid of any sense. If you want to access the value of the
char through a pointer, you NEED a pointer to char. No other pointer
type is suitable for the job.
#include <stdio.h>

void main()
So, you didn't bother to read the FAQ *before* posting...
{
char c = 'a';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
It's actually a fatal programing error, as far as your program is
concerned. NEVER ignore a compiler warning *before* understanding its
meaning.
clrscr();
What's this nonsense?
pf("\n%d", *p);
What's this nonsense?
getch();
What's this nonsense?
}

. . . . whats happening ?


You're misdefining main(), incorrectly initialising a pointer,
incorrectly using the same pointer and calling a lot functions that
neither your program nor the standard C library defines. What would
you expect to happen?!?

Compare your piece of garbage with the following program that correctly
uses a pointer to figure out the value of 'a':

#include <stdio.h>

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

printf("%d\n", *p);
return 0;
}

An equally correct way is by defining c as int and p as pointer to int.

There are cases when you want a pointer to unsigned char to point to the
address of an object of another type, but all the other object pointer
types should point to the address of an object of the corresponding type.

Only experts have a licence to ignore this rule, because they know what
they're doing.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
Gautam 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 !
'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()
Undefined behaviour. Use "int main(void)"
{
char c = 'a';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
Well? why didn't you do something about it?
clrscr();
no such function.
pf("\n%d", *p);
no such function.
getch();
no such function.
}

. . . . whats happening ?


Compare to:

#include <stdio.h>
int main(void)
{
int c = 'a';
int *p;

p = &c;
printf("%d\n", *p);
return 0;
}

and compare the resultant values with those you got. That may
give you some clue as to what is going on in your machine even
with all the undefined behavior.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #4
James Hu wrote:
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.


Is it an undefined behaviour, or result ?

I mean, since c is declared to be a char (ie 1 byte, according to ISO-C)
and p is an int* (pointer to 2, or 4 bytes), the program reaches the
memory and reads 2 (or 4) bytes instead of just one.
This is defined as far as behaviour is concerned.
The result itself being undefined (we don't know what's in second, third
and fourth byte).

I'm a bit confused with terminology here... :-(
Nov 14 '05 #5
In article <40************ ***********@new s.free.fr>,
gregg <gr******@netJU STSAYNOcourrier .com> wrote:
James Hu wrote:
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.
Is it an undefined behaviour, or result ?


It's undefined behavior. What does pf do? We don't know, it's not
defined anywhere. The only way that that line could be well defined is
if you had a line similar to the following in your code:

#define pf(x,y)

Which would replace your line with a blank one.
I mean, since c is declared to be a char (ie 1 byte, according to ISO-C)
and p is an int* (pointer to 2, or 4 bytes),
There's nothing stopping int from being 1 byte, 3 bytes or 20 bytes.
the program reaches the
memory and reads 2 (or 4) bytes instead of just one.
This is defined as far as behaviour is concerned.
No, it isn't. If sizeof(char) != sizeof(int), you're attempting to
read from memory that you haven't allocated. That's undefined behavior.
Your program might crash, it might give you a random value, or anything
else may happen.
The result itself being undefined (we don't know what's in second, third
and fourth byte).
The result *is* undefined ... because the *behavior* is undefined to
begin with.
I'm a bit confused with terminology here... :-(


Nov 14 '05 #6
Gautam 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 !
'cause it shows consistent values for diff. chars , for eg.
Every once in a while, someone decides to post the worst possible excuse
for a C program. Yours is in that group, though you forgot to leave off
the #include statement.

Here's your "code": #include <stdio.h>

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

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


And here's something closer to C:

#include <stdio.h>

int main()
{
char c = 'a';
char *p = &c;
printf("%d\n", (int) *p);
return 0;
}
Nov 14 '05 #7
Clark Cox wrote:
No, it isn't. If sizeof(char) != sizeof(int), you're attempting to
read from memory that you haven't allocated. That's undefined behavior.
Your program might crash, it might give you a random value, or anything
else may happen.


Right, i got it now !
thanks

g.
Nov 14 '05 #8
> > 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;" (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.
Nov 14 '05 #9
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).

However, even though p holds a valid character pointer value, it holds
an invalid integer pointer value, and therefore indirection yields UB
when applied on p (xref: 6.5.3.2p4).

-- James
Nov 14 '05 #10

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
5886
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
31
2204
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
1922
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
3515
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
9645
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
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
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...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
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...
1
4054
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
3
2881
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.