473,699 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

confused between declaration & definition

1) int i = 3;
2.) int* pi;
3.) int* pi2 = &i
4.) char* pc;
5.) char c;
6) char c2 = 'a'
7.) char* pc2 = &c2
8.) char* ps = "Stroustrou p";
9.) extern double d;

2, 4 & 9 are the only declarations here. right ?

i know that 1, 3, 5, 6, 7, 8 are definitions but can we call them
declarations too?

Is 2 a legal declaration?

i dont understand the difference between 7 & 8.

Nov 8 '06 #1
3 4585
* arnuld:
1) int i = 3;
2.) int* pi;
3.) int* pi2 = &i
4.) char* pc;
5.) char c;
6) char c2 = 'a'
7.) char* pc2 = &c2
8.) char* ps = "Stroustrou p";
9.) extern double d;

2, 4 & 9 are the only declarations here. right ?
All are declarations, but only 9 is a pure declaration (not a definition).

i know that 1, 3, 5, 6, 7, 8 are definitions but can we call them
declarations too?
Yes. Any definition is a declaration.

Is 2 a legal declaration?
Yes.

i dont understand the difference between 7 & 8.
7 declares a pointer to char and initializes it with the address of a
single char.

8 declares a pointer to char and initializes it with the address of the
first char in a zero-terminated sequence of chars (the string "Stroustrup ").

8 is bad form (because it means you can try to modify a string literal
without the compiler detecting that error). It's only allowed in order
to have backwards compatibility with old C. Except for interfacing to
old C code that requires it, you should write

char const* ps = "Stroustrup ";

or equivalently

const char* ps = "Stroustrup ";

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #2
arnuld wrote:
1) int i = 3;
2.) int* pi;
3.) int* pi2 = &i
4.) char* pc;
5.) char c;
6) char c2 = 'a'
7.) char* pc2 = &c2
8.) char* ps = "Stroustrou p";
9.) extern double d;

2, 4 & 9 are the only declarations here. right ?
2 and 4 look like definitions to me.
i know that 1, 3, 5, 6, 7, 8 are definitions but can we call them
declarations too?

Yes, the standard defines [3.1/2]:

A declaration is a definition unless it declares a function without
specifying the function?s body (8.4), it contains the extern specifier
(7.1.1) or a linkage-specification (7.5) and neither an initializer nor
a function-body, it declares a static data member in a class declaration
(9.4), it is a class name declaration (9.1), or it is a typedef
declaration (7.1.3), a using-declaration (7.3.3), or a using-directive
(7.3.4).

As you can see, every definition is a declaration.

Is 2 a legal declaration?
Looks legal to me. Did you run it by your compiler?

i dont understand the difference between 7 & 8.
8 is a special case since the right hand side is really const. Usually, when
you define a T*, you are allowed to modify the pointee; and any attempt to
define a T* so that it points to a (an array of) T char should fail. In
order to support C code, there are special provisions for char*.
Best

Kai-Uwe Bux
Nov 8 '06 #3
Alf P. Steinbach wrote:
* arnuld:
>1) int i = 3;
2.) int* pi;
3.) int* pi2 = &i
4.) char* pc;
5.) char c;
6) char c2 = 'a'
7.) char* pc2 = &c2
8.) char* ps = "Stroustrou p";
9.) extern double d;

2, 4 & 9 are the only declarations here. right ?

All are declarations, but only 9 is a pure declaration (not a
definition).
It depends on the context. 2, 4, 5, can also be declarations if
they happen to be inside a class definition.
>[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4271
by: qazmlp | last post by:
// test.h class test { public : inline void check() ; } ; // test.C inline void test::check() // Is 'inline' optional here?
2
1302
by: | last post by:
so i am using Dreamweaver; and I need to be able to have multiple pages post to a page called page6_1.asp from there; i give people the ability to either '1 - save definition as new report'; '2 - overwrite existing report definition'; or '3 - new draft for existing report' I have these 3 functions written as seperate pages.. Because Dreamweaver starts getting confused when you put too much on one page.
8
2455
by: newmans | last post by:
Perhaps one of the experts can straighten me out on this point... In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition, section 4.9, indicates that typedef complex<short> Point; is not only a declaration, but also a definition. The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2 that
34
6433
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
10
20872
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. sourcefile1.c ============== extern long globalfoo; /* declaration? */
25
2799
by: venky | last post by:
Hi main() { int x; /* it declaration or defination??*/ }
1
1341
by: wtu | last post by:
function Declaration&Definition -------------------------------------------------------------------------------- " Geroty (const vector<Point3D>& poly, const Plane& Tr)" As i'm c++ beginner the following Function Declaration and Definition couldnt understandable? so explain me what kind of Declaration this is."const vector<Point3D>& poly....." ? and where do I find study materials for the kind of function declaration" Also pleas...
15
14906
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here after a long time since people here also helped me out with my lexer. thanking in anticipation.
6
3679
by: James H. Newman | last post by:
I am playing with some code that has been automatically generated from ASN.1 data specification found in RFC 3280. One of the structures generated reads as follows: typedef struct TBSCertList { Version_t *version /* OPTIONAL */; AlgorithmIdentifier_t signature; Name_t issuer; Time_t thisUpdate; struct Time *nextUpdate /* OPTIONAL */;
0
8685
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
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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...
0
7745
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
6532
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
4374
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
3054
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
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.