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 

can any one help me...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: can any one help me... Reply with quote



hi 4nds...
if i declare
int a=10;
the value 10 will be assigned in a;
similarly if i declare
void main()
{
int a,b=10;
a=&b;
}
why can't the variable store the address the address of variable be
just like storing a int value..??
why shud a be a pointer variable??
pls answer me....
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Martin Ambuhl
Guest





PostPosted: Thu Apr 19, 2007 7:13 pm    Post subject: Re: can any one help me... Reply with quote



madhuriIST (AT) gmail (DOT) com wrote:
Quote:
hi 4nds...
if i declare
int a=10;
the value 10 will be assigned in a;
similarly if i declare
void main()
^^^^

This is an error that *no* competent C textbook or instructor teaches.

Quote:
{
int a,b=10;
a=&b;
^^^^

This is very dangerous. &b yields an address; it should be stored in
a pointer. a is an int, not a pointer. Conversion between ints and
pointers makes non-portable programs and the use of such conversions
requires a knowledge of the particular implementation that, frankly, you
are very unlikely to have.

Quote:
}
why can't the variable store the address the address of variable be
just like storing a int value..??

It can:
int main(void)
{
int *a; /* a is a pointer-to-int */
int b = 10; /* b is an int */
b = &a; /* a now points to b */
return 0;
}

Quote:
why shud a be a pointer variable??

It *should* because you are trying to store a pointer value in it.

Quote:
pls answer me....

*Please* don't whine.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
John Dallman
Guest





PostPosted: Thu Apr 19, 2007 7:13 pm    Post subject: Re: can any one help me... Reply with quote



In article <clcm-20070416-0043 (AT) plethora (DOT) net>, madhuriIST (AT) gmail (DOT) com ()
wrote:

Quote:
void main()
{
int a,b=10;
a=&b;
}
why can't the variable store the address the address of variable be
just like storing a int value..??
why shud a be a pointer variable??

Because C, while not a fully type-safe language, distinguishes clearly
between integer types and pointer types. If you want a language that
doesn't, you want assembler, or BCPL.

In general, trying to store pointers and ints in the same variables is a
bad idea: how will functions to which they are passed know what kind of
data they've received? If you really want to do this, look up C "union",
which will let you do it, but makes you spell out that this is really
what you want. It's a good idea to learn the basics of C thoroughly
before trying to get clever, though: getting exactly what you ask for,
when you don't know clearly what you want or why, leads to trouble. This
is as true in programming as it is in stories.

--
John Dallman, jgd (AT) cix (DOT) co.uk, HTML mail is treated as probable spam.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Keith Thompson
Guest





PostPosted: Thu Apr 19, 2007 7:14 pm    Post subject: Re: can any one help me... Reply with quote

madhuriIST (AT) gmail (DOT) com writes:
Quote:
hi 4nds...

I think "4nds" is supposed to be "friends". The time I and others
wasted figuring that out vastly exceeds the time it would have taken
you to spell out the word.

This is not a chat room. *Please* take the time to spell out words
rather than using silly abbreviations like "shud" and "pls". We don't
worry about minor errors in grammar and spelling, especially if
English isn't your first language, but deliberate obfuscation is not
welcome.

Quote:
if i declare
int a=10;
the value 10 will be assigned in a;

Right.

Quote:
similarly if i declare
void main()

The main returns int, not void. If you have a book that says main
returns void, get rid of it. The correct declaration is

int main(void)

Quote:
{
int a,b=10;
a=&b;
}
why can't the variable store the address the address of variable be
just like storing a int value..??

Because integers and pointers are two different things. They *might*
have similar representations on some systems; on others, there may not
even be an integer type big enough to hold a pointer value.

Quote:
why shud a be a pointer variable??

Because you want to store a pointer value (an address) in it. Why
would you think it shouldn't be a pointer variable?

Quote:
pls answer me....

I just did. (Asking the question is sufficient; requesting that we
answer it is redundant.)

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Douglas A. Gwyn
Guest





PostPosted: Thu Apr 19, 2007 7:14 pm    Post subject: Re: can any one help me... Reply with quote

madhuriIST (AT) gmail (DOT) com wrote:
Quote:
if i declare
int a=10;
the value 10 will be assigned in a;
similarly if i declare
void main()
{
int a,b=10;
a=&b;
}

Actually that is wrong and should generate a diagnostic.
I presume you meant
int *a, b = 10;

Quote:
why can't the variable store the address the address of variable be
just like storing a int value..??

There are ways to do that, such as:
uintptr_t aa;
aa = (uintptr_t)&b;
but there is rarely any good reason for doing that.

Quote:
why shud a be a pointer variable??

Because you're assigning an address to it.
Addresses in C are an abstraction, not necessarily represented
the same as integers. The "same" location may have its
address represented differently depending on the type of the
object that is presumed to be at that location. One advantage
of this scheme is that pointer arithmetic, which knows the
byte-width of the pointed-to object, automatically scales by
that width. For example:
double x[10], *p = x;
while (p < &x[10])
*p++ = 0.0;
If you were using plain integers you'd have to deal with the
element width somehow:
double x[10];
uintptr_t i = (uintptr_t)x;
for (; i < sizeof x; i += sizeof(double))
*(double *)i = 0.0;
Of course, you can also use array indexing:
double x[10];
int i = 0;
for (; i < 10; ++i)
x[i] = 0.0;
but in C that is defined in such a way as to exploit the
properties of pointer arithmetic!
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jack Klein
Guest





PostPosted: Thu Apr 19, 2007 7:15 pm    Post subject: Re: can any one help me... Reply with quote

On 16 Apr 2007 15:18:36 GMT, madhuriIST (AT) gmail (DOT) com wrote in
comp.lang.c.moderated:

Quote:
hi 4nds...

4nds doesn't post here anymore, he had a birthday recently, so now he
calls himself 5nds.

Quote:
if i declare
int a=10;
the value 10 will be assigned in a;

No, 'a' will be initialized with the value 10. This is not the same
thing as assignment.

Quote:
similarly if i declare
void main()

The only portable return type for main() in C is int, not void.

Quote:
{
int a,b=10;
a=&b;

If you are using an actual C compiler, it must issue a diagnostic at
the line above, because it is not valid. If the compiler produces an
executable after issuing the diagnostic, executing the code produces
undefined behavior.

Quote:
}
why can't the variable store the address the address of variable be
just like storing a int value..??
why shud a be a pointer variable??
pls answer me....

u shud reed a buk on see programming.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group