how the following two code parts same

S

saipathakota

how the following two code parts same:

#include <stdio.h>

void main ()
{

char i = NULL;

char &q = i;
^^^^

printf ( "%d", i);

printf ("%d", q);
}



O/p.................. 0 0



#include <stdio.h>

void main ()
{

char i = NULL;

char q = i;
^^

printf ( "%d", i);

printf ("%d", q);
}


O/p ................ 0 0
 
F

fred.l.kleinschmidt

how the following two code parts same:

#include <stdio.h>

void main ()
{

char i = NULL;

char &q = i;
^^^^

printf ( "%d", i);

printf ("%d", q);

}

O/p.................. 0 0

#include <stdio.h>

void main ()
{

char i = NULL;

char q = i;
^^

printf ( "%d", i);

printf ("%d", q);

}

O/p ................ 0 0

They are the same except for the lines
 
L

Lew Pitcher

how the following two code parts same:

#include <stdio.h>

void main ()

Nope. In a hosted environment, main() returns int by definition. You really mean
int main(void)
here
{

char i = NULL;

You should see a diagnostic indicating that you are converting a pointer to an
integer (type) without a cast. Having said that, i should now be initialized
to zero.
char &q = i;

Syntax error. Your program should not compile.
printf ( "%d", i);

printf ("%d", q);
}



O/p.................. 0 0

I don't believe you.
#include <stdio.h>

void main ()
Again,
int main(void)
{

char i = NULL;

You should see a diagnostic indicating that you are converting a pointer to an
integer (type) without a cast. Having said that, i should now be initialized
to zero.
char q = i;

q is initialized to the contents of i. Since i is initialized to zero and not
changed before this statement, q should now also be set to zero.
printf ( "%d", i);

printf ("%d", q);
}


O/p ................ 0 0

OK

So, what's your problem?

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

saipathakota

Nope. In a hosted environment, main() returns int by definition. You really mean
int main(void)
here



You should see a diagnostic indicating that you are converting a pointer to an
integer (type) without a cast. Having said that, i should now be initialized
to zero.




Syntax error. Your program should not compile.






I don't believe you.





Again,
int main(void)



You should see a diagnostic indicating that you are converting a pointer to an
integer (type) without a cast. Having said that, i should now be initialized
to zero.


q is initialized to the contents of i. Since i is initialized to zero and not
changed before this statement, q should now also be set to zero.






OK

So, what's your problem?

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

i compiled in the vc++ .. o/p is same .. is it true with the C++
 
D

Default User

Lew Pitcher wrote:

You should see a diagnostic indicating that you are converting a
pointer to an integer (type) without a cast.

You mean you might see a diagnostic. There's an excellent chance that
NULL is not a pointer type at all.




Brian
 
D

Default User


Please trim quotes to the minimum necessary for context. Especially
trim the signatures, the parts after -- (I left part in above as an
example).
i compiled in the vc++ .. o/p is same .. is it true with the C++

VC++ can compile C programs. The first code you showed was NOT a C
program, as this:

char &q = i;

Is not a valid C construct. That looks a lot like a reference in C++.
You should probably be posting in comp.lang.c++.





Brian
 
K

Keith Thompson

how the following two code parts same:

please don't double-space posted code. It just makes it harder to read.
#include <stdio.h>
void main ()
{
char i = NULL;
char &q = i;
^^^^
printf ( "%d", i);
printf ("%d", q);
}

The above is not C. It appears to be C++. Consulting your C++
textbook or the C++ FAQ for information about "references" should
answer your question. Failing that, try comp.lang.c++.
#include <stdio.h>
void main ()

Wrong. Make this "int main(void)" (or "int main()" if you mean to
write C++).
{
char i = NULL;

NULL is a null *pointer* constant. Assigning it to a char object
might happen to work, but it makes no sense. You probably mean
char i = '\0';
or
char i = 0;
(the two are equivalent).
char q = i;
^^
printf ( "%d", i);
printf ("%d", q);
}

You claim that the output is "0 0", but neither of your printf calls
prints a space character. If your program compiles and runs at all,
I'd expect the output to be "00". Either the code you posted isn't
your actual code, or the output you posted isn't your actual output.
You should copy-and-paste both your code and your output *exactly*;
don't try to re-type or paraphrase them.

You should print a new-line ("\n") at the end of your output. You
should also return a value from main(); add "return 0;" before the
closing brace. (This isn't strictly necessary in some circumstances,
but it's always a good idea.)

If you want the output to be understandable, consider something like:

printf("i = %d, q = %d\n", i, q);
 
J

jameskuyper

Default said:
(e-mail address removed) wrote: ....

VC++ can compile C programs. The first code you showed was NOT a C
program, as this:

char &q = i;

Is not a valid C construct. That looks a lot like a reference in C++.
You should probably be posting in comp.lang.c++.

When you do post to comp.lang.c++, they will tell you that the results
you got are precisely the results you should have expected to get
(ignoring the defective declarations of main()). Save them some time,
and in your first message to that newsgroup, try to explain why it is
that you are surprised by these results. Then they will be able to
explain to you what it is that you're misunderstanding; because you
are misunderstanding something.
 
J

jameskuyper

Keith said:
(e-mail address removed) writes: ....

The above is not C. It appears to be C++. Consulting your C++
textbook or the C++ FAQ for information about "references" should
answer your question. Failing that, try comp.lang.c++.


Wrong. Make this "int main(void)" (or "int main()" if you mean to
write C++).


NULL is a null *pointer* constant. Assigning it to a char object
might happen to work, but it makes no sense. You probably mean

Given that it is C++ code, it will work. In C++, NULL has to have a
value of 0; it cannot have a pointer type.
 
K

Keith Thompson

Keith said:
(e-mail address removed) writes: [...]
{
char i = NULL;

NULL is a null *pointer* constant. Assigning it to a char object
might happen to work, but it makes no sense. You probably mean

Given that it is C++ code, it will work. In C++, NULL has to have a
value of 0; it cannot have a pointer type.

True, but using NULL for anything other than a pointer value is poor
style in any case.
 
J

James Kuyper

Keith said:
Keith said:
(e-mail address removed) writes: [...]
{
char i = NULL;
NULL is a null *pointer* constant. Assigning it to a char object
might happen to work, but it makes no sense. You probably mean
Given that it is C++ code, it will work. In C++, NULL has to have a
value of 0; it cannot have a pointer type.

True, but using NULL for anything other than a pointer value is poor
style in any case.

Agreed.
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top