A doubt

D

deepak

Hi,

In the following program, could someone help me to understand how y is
generated by compiler. I'm not able to map properly even after getting
output as 100.

#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int main()
{
int y = 100;
int *p;
p = malloc(sizeof(int));
*p = 10;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return 0;
}

Thanks,
Deepak
 
B

Ben Bacarisse

deepak said:
In the following program, could someone help me to understand how y is
generated by compiler. I'm not able to map properly even after getting
output as 100.

#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int main()
{
int y = 100;
int *p;
p = malloc(sizeof(int));
*p = 10;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return 0;
}

Coursework question?

The code has undefined behaviour at the point you assign p since
malloc is undeclared. Remove what you think is the comment two lines
below to get a better idea of what is happening.
 
T

Thomas Matthews

deepak said:
Hi,

In the following program, could someone help me to understand how y is
generated by compiler. I'm not able to map properly even after getting
output as 100.

#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int main()
{
int y = 100;
int *p;
p = malloc(sizeof(int));
*p = 10;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return 0;
}

Thanks,
Deepak

Per Richard's analysis, in the line
y = y/*p;
the /* is a token indicating the start of a comment.
y = y / *p;
is actually the line you want. Here is a case where
whitespace is important and could lead to different
meanings.

You could also try:
y /= *p;


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
B

bartc

Thomas Matthews said:
deepak wrote:
Per Richard's analysis, in the line
y = y/*p;
the /* is a token indicating the start of a comment.

I've always had trouble with C's /*...*/ comments and this example shows
that the choice of a divide op followed immediately by a dereference op
probably wasn't a good one.

/*...*/ comments also apparently don't nest [why not?], so what's the best
way to
comment out a block of code that may include such comments? Using // doesn't
look right as you now get lines starting with ///* or ////. It's also a lot
of typing.

What's left, using something like #if 0...#endif? That seems to nest at
least.
 
K

Keith Thompson

bartc said:
I've always had trouble with C's /*...*/ comments and this example shows
that the choice of a divide op followed immediately by a dereference op
probably wasn't a good one.

It shows that you can cause trouble with a deliberately contrived
piece of code.
/*...*/ comments also apparently don't nest [why not?], so what's the
best way to
comment out a block of code that may include such comments? Using // doesn't
look right as you now get lines starting with ///* or ////. It's also a lot
of typing.

It's not a lot of typing if you have a decent editor. And inserting
"// " at the beginning of each line avoids the //////// problem.
What's left, using something like #if 0...#endif? That seems to nest at
least.

Yes. What I sometimes do is something like this:

#if 0
* printf("This code is ...");
* printf("commented out\n");
#endif

If it's a large block of code, I can see at a glance that it's
inactive without having to scan for #if/#endif directives.
 
N

Nobody

/*...*/ comments also apparently don't nest [why not?], so what's the best
way to
comment out a block of code that may include such comments? Using // doesn't
look right as you now get lines starting with ///* or ////. It's also a lot
of typing.

What's left, using something like #if 0...#endif? That seems to nest at
least.

Comments are for commenting; use #if ... #endif to disable a block of
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top