Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is the definition of declaration is contradictory?????????

Reply
Thread Tools

is the definition of declaration is contradictory?????????

 
 
sushant
Guest
Posts: n/a
 
      01-20-2005
according to the definition of declaration of a variable "the variable
is not allocated any space in the memory till it is defined".

so the code:

int main(void)
{
int x,*p;
p=&x;
printf("%p",p);
return 0;
}

should generate an error bcos x is only declared not defined... but
after executing the above code it was showing some address. the story
is not yet finished... after initialising x with some value , it was
showig the same address....
any answer for that....

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      01-20-2005
"sushant" <> wrote:

> according to the definition of declaration of a variable "the variable
> is not allocated any space in the memory till it is defined".
>
> so the code:
>
> int main(void)
> {
> int x,*p;


Yech - Google-unindented code!

> p=&x;
> printf("%p",p);
> return 0;
> }
>
> should generate an error bcos x is only declared not defined...


Wrong. That's not just a declaration of x (and p), it's a definition as
well. If you want to declare, but not define, a variable, declare it
using extern.

(BTW, for fully conforming code, you should cast that pointer to void *
before printf()ing it using "%p".)

Richard
 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      01-20-2005
On Thu, 20 Jan 2005 03:23:05 -0800, sushant wrote:

> sorry for that unindented code, but its not mentioned any where that
> for declaration we have to use extern keyword. i just want to declare a
> local variable for that matter ...... now what??


C does not permit you to just declare local variables, all such
declarations are also definitions. Why would you want to?

Note that you can declare something like:

int main(void)
{
extern int x;
...
}

Here the scope of this declaration (which is not a definition) is local
to the block but the variable x isn't, this declaration will be "linked"
with other declarations and definition of x in the entire program that
have external linkage. E.g. if the following is in the same program, not
necessarily the same source file

int foo(void)
{
extern int x;
}

then the x in this function refers to the same object as the x in main().
There needs to be a definition for x somewhere but that has to be at file
scope, it cannot be within a function.

Lawrence
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
XML jargon: declaration vs. definition Razvan XML 4 02-10-2005 07:26 AM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57