Hi ppl,
can some one say if the following pointer initialization is legal or
no.
int *intPtr = 5;
While debugging i still see that the pointer is given some address.
However, the initialization value is not found there. But, the
following string initialization,
char *charPtr = "this is a test string";
is just fine. The charPtr is pointing to the address location of the
value, "this is a test string".
thank you,
Prakash 17 4806 pr************* **@googlemail.c om wrote:
can some one say if the following pointer initialization is legal or
no.
int *intPtr = 5;
No, it's not legal. `5` isn't a pointer value (and it hasn't been
obtained from a pointer value). The compiler should generate a
diagnostic. The behaviour of the code, if the compiler is
(un)helpful enough to generate it, isn't specified by the C
standard. (Your implementation may say what it does.)
While debugging i still see that the pointer is given some address.
However, the initialization value is not found there.
I don't understand what you mean. Be specific.
But, the following string initialization,
char *charPtr = "this is a test string";
Which is legal, and assigns to `charPtr` the address of
some static store initialised to contain the characters
of the string (with a terminating 0).
is just fine.
Good.
The charPtr is pointing to the address location of the
value, "this is a test string".
Yes.
--
Chris "electric hedgehog" Dollin
"Go not to the Elves for counsel, for they will answer both no and yes"
/The Lord of the Rings/
On Feb 27, 2:43 pm, prakashraovadd. ..@googlemail.c om wrote:
Hi ppl,
can some one say if the following pointer initialization is legal or
no.
int *intPtr = 5;
your leftside is int* , right side is int, you should get warning
telling that you should cast.
>
While debugging i still see that the pointer is given some address.
However, the initialization value is not found there. But, the
intPtr is memory location and it will point to memory location 5,
if you try to find address of intPtr it will be different value, see
this.
#include<stdio. h>
#include<stdlib .h>
int main()
{
int *intPtr = 5; <---Casting Warning
char *charPtr = "this is a test string";
/* here intPtr is NOT EQAUL to &intPtr */
printf("\n%p",& intPtr);
printf("\n%p\n" ,intPtr);
return 0;
}
output :
0xbffed8e4 <--some address
0x5
------
following string initialization,
char *charPtr = "this is a test string";
is just fine. The charPtr is pointing to the address location of the
value, "this is a test string".
thank you,
Prakash
ra************@ yahoo.co.in wrote:
On Feb 27, 2:43 pm, prakashraovadd. ..@googlemail.c om wrote:
>Hi ppl, can some one say if the following pointer initialization is legal or no.
int *intPtr = 5;
your leftside is int* , right side is int, you should get warning
telling that you should cast.
No, you should get a diagnostic [warning or not] that says /what you're
doing is wrong/. It shouldn't tell you to cast, because that's usually
/not/ the right answer to a type mismatch: the thing to do with a type
mismatch is to /fix the mismatch/.
If the OP really wants to make a pointer-to-int which points to
location 5 (wherever that is -- and /which/ location 5 would that
be, sir? Aligned how, exactly?) on his machine, and his implementation
allows him to do that, then a cast might be the right answer. But
the OP hasn't explained why they're playing typing games, so we
don't know that the premise is true, and I believe it to be false.
In any case, casting at whim is a problem, not a solution. Yes?
--
Chris "electric hedgehog" Dollin
"Go not to the Elves for counsel, for they will answer both no and yes"
/The Lord of the Rings/
On Feb 27, 4:19 pm, Chris Dollin <chris.dol...@h p.comwrote:
raxitsheth2...@ yahoo.co.in wrote:
On Feb 27, 2:43 pm, prakashraovadd. ..@googlemail.c om wrote:
Hi ppl,
can some one say if the following pointer initialization is legal or
no.
int *intPtr = 5;
your leftside is int* , right side is int, you should get warning
telling that you should cast.
No, you should get a diagnostic [warning or not] that says /what you're
doing is wrong/. It shouldn't tell you to cast, because that's usually
/not/ the right answer to a type mismatch: the thing to do with a type
mismatch is to /fix the mismatch/.
I agree, you used correct/more accurate words.on gcc i got following.
warning: initialization makes pointer from integer without a cast
>
If the OP really wants to make a pointer-to-int which points to
location 5 (wherever that is -- and /which/ location 5 would that
be, sir? Aligned how, exactly?) on his machine, and his implementation
allows him to do that, then a cast might be the right answer. But
the OP hasn't explained why they're playing typing games, so we
don't know that the premise is true, and I believe it to be false.
In any case, casting at whim is a problem, not a solution. Yes?
Not in all case, as you give the description above,
>
--
Chris "electric hedgehog" Dollin
"Go not to the Elves for counsel, for they will answer both no and yes"
/The Lord of the Rings/
--Raxit
On Feb 27, 9:43 am, prakashraovadd. ..@googlemail.c om wrote:
Hi ppl,
can some one say if the following pointer initialization is legal or
no.
int *intPtr = 5;
I can say if it's legal or not. It's not legal. If the compiler allows
it, it's almost certain not to do what I think you want.
While debugging i still see that the pointer is given some address.
However, the initialization value is not found there. But, the
following string initialization,
char *charPtr = "this is a test string";
is just fine. The charPtr is pointing to the address location of the
value, "this is a test string".
Actually, charPtr is pointing to the initial 't' of the string.
charPtr is a pointer to char, not a pointer to string. This is an
important distinction. Strings don't "really" exist in C.
If you wanted (as I suspect) intPtr to point to an int containing a
value of 5, you'd have to do it in two steps, e.g.
int theValue = 5;
int *intPtr = &intValue;
You probably need to do some more reading of a good text book on C. pr************* **@googlemail.c om wrote:
>
can some one say if the following pointer initialization is legal
or no.
int *intPtr = 5;
Converting an integer to a pointer is implementation defined. The
answer is no.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> ra************@ yahoo.co.in wrote:
On Feb 27, 4:19 pm, Chris Dollin <chris.dol...@h p.comwrote:
>If the OP really wants to make a pointer-to-int which points to location 5 (wherever that is -- and /which/ location 5 would that be, sir? Aligned how, exactly?) on his machine, and his implementation allows him to do that, then a cast might be the right answer. But the OP hasn't explained why they're playing typing games, so we don't know that the premise is true, and I believe it to be false.
>In any case, casting at whim is a problem, not a solution. Yes?
Not in all case, as you give the description above,
In that case it isn't at whim; it's because it would be the [1]
correct thing to do.
[1] Or "a".
--
Chris "electric hedgehog" Dollin
"It was the first really clever thing the King had said that day."
/Alice in Wonderland/
CBFalconer <cb********@yah oo.comwrites:
pr************* **@googlemail.c om wrote:
>> can some one say if the following pointer initialization is legal or no.
int *intPtr = 5;
Converting an integer to a pointer is implementation defined. The
answer is no.
The result of such a conversion is implementation-defined, but no such
conversion is specified here. Some compilers (including gcc) will do
the conversion implicitly, but they're still required to issue a
diagnostic for the constraint violation; gcc's warning message is
sufficient as far as the standard is concerned.
If a compiler chooses to accept the above declaration, it's doing so
as part of an extension, something beyond what the language specifies.
Such an extension will *probably* do the equivalent of:
int *intPtr = (int*)5;
and I suspect that all compilers that allow this do just that, but in
principle it could do anything. It could even allocate space for an
int object and set intPtr to point to it, resulting in (*intPtr == 5)
(I don't believe any compilers actually do that). Or it could make
demons fly out of your nose. Once you've violated a constraint, the
behavior is undefined (but an implementation can choose to define the
behavior).
If you want to initialize intPtr to the address corresponding to the
number 5, the *first* thing you should do is stop and think about why
you want to do this. Chances are you don't. If you do have a good
reason, you should use a cast (an explicit conversion).
--
Keith Thompson (The_Other_Keit h) ks***@mib.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"
Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
>pr************* **@googlemail.c om wrote:
>>> can some one say if the following pointer initialization is legal or no.
int *intPtr = 5;
Converting an integer to a pointer is implementation defined. The answer is no.
The result of such a conversion is implementation-defined, but no such
conversion is specified here. ....
Oh? I see an object intPtr, of type pointer to int, being
initialized with the value 5, which is an integer.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: DaKoadMunky |
last post by:
I was recently looking at some assembly code generated by my compiler. When
examining the assembly code associated with constructors I noticed that the
dynamic-dispatch mechanism was enabled before member initialization but after
base initialization.
To speak in implementation details the virtual function table pointer for
objects being constructed was set before member initialization but after base
initialization.
Curiousity then...
|
by: Steven T. Hatton |
last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe
it passed the moderator's veto - and I did not expect or desire anything
different. But the question remains:
ISO/IEC 14882:2003(E) §8.5 says:
To zero-initialize an object of type T means:
5
-- if T is a scalar type (3.9), the object is set to the value of 0 (zero)
converted to T;
|
by: sathyashrayan |
last post by:
The standard confirms that the following initialization of a struct
struct node
{
---
---
}
struct node var = {NULL};
|
by: tuko |
last post by:
Hello kind people.
Can someone explain please the following code?
/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage; /* Line 1*/
/* Set The Pointer To NULL */
memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/
According to my knowledge in the first line
|
by: Jeremy Beasley |
last post by:
I'm having a problem using pointers to structures. One method of declaring
point and then referencing a structure works, while the other generates an
error at compile time.
-----------------
ex1. (this method works)
struct data {
int a,b;
} myData;
| |
by: Roman Werpachowski |
last post by:
I can't understand this:
double * x = new double;
const double * p = x;
delete p;
works. Why am I allowed to delete a const pointer? On the same note:
why am I allowed to do this:
class probna {
|
by: ank |
last post by:
Hi, all.
I've come to think of the idea of
automatic initialization/deinitialization of
non-local reference count pointer.
I've made an assumption that the user of the pointer
only read pointer after acquire the reference (increment the ref count)
and when finished using it, the user will release the ref count.
|
by: ank |
last post by:
Hi, all.
I've come to think of the idea of
automatic initialization/deinitialization of
non-local reference count pointer.
I've made an assumption that the user of the pointer
only read pointer after acquire the reference (increment the ref count)
and when finished using it, the user will release the ref count.
|
by: Glen Dayton |
last post by:
While looking at some old code I ran across a snippet that
increments a pointer to access different members of a structure:
....
struct Badness {
std::string m_begin;
std::string m_param1;
std::string m_param2;
std::string m_end;
|
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...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |