int* ptr=10;.......issue.

S

sid

hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.


int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
//
.......



}


on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.
 
A

arajak

sid said:
hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.


int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
//
......



}


on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.

==>I tried this out on gcc also it gives the same warning.
Its because
int* ptr = 10;
is a one liner for the following two lines :
int *ptr; // an integer pointer
ptr = 10; //NOTE : its ptr not *ptr

(ref:(int *ptr = 10;))So ptr doesnt points to a memory location wich
stores 10 but after that assignment ptr points to memory location 10.
 
A

arajak

sid said:
hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.


int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
//
......



}


on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.

==>I tried this out on gcc also it gives the same warning.
Its because
int* ptr = 10;
is a one liner for the following two lines :
int *ptr; // an integer pointer
ptr = 10; //NOTE : its ptr not *ptr

(ref:(int *ptr = 10;))So ptr doesnt points to a memory location wich
stores 10 but after that assignment ptr points to memory location 10.
 
S

sid

==>I tried this out on gcc also it gives the same warning.
Its because
int* ptr = 10;
is a one liner for the following two lines :
int *ptr; // an integer pointer
ptr = 10; //NOTE : its ptr not *ptr

(ref:(int *ptr = 10;))So ptr doesnt points to a memory location wich
stores 10 but after that assignment ptr points to memory location 10.

thanks!
i had assumed to be same as int *ptr =new int(10);
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

sid said:
hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.


int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
No, you're trying to initialize the pointer with the value 10.
10 is not a location of an int.

int i;
int *ptr = &i; /*properly initializes an int pointer to point at an int*/
 
J

John Tsiombikas

i had assumed to be same as int *ptr =new int(10);

That's a weird thing to assume, as it's not something that can be done
with C (nor C++, which is where I imagine you got the new keyword from).
 
F

Flash Gordon

Nils said:
No, you're trying to initialize the pointer with the value 10.
10 is not a location of an int.

It is also an illegal assignment and the compiler is *required* to issue
a diagnostic. Warnings count as diagnostics, the computer slapping you
could also be a valid diagnostic, but might be a bit harder for the
compiler writter to implement you. Possibly the number of slaps being
the the error code, so watch out for serious errors with error codes in
the thousands!
int i;
int *ptr = &i; /*properly initializes an int pointer to point at an int*/

To get ptr pointing to a location containing 10 you should change that to
int i = 10;
int *ptr = &i;

The OP also said:
To which I reply that the version of gcc I have here generates a
warning. If the compiler is warning about something that generally (but
not quite always) means you have made a mistake.
 
S

shaanxxx

sid said:
hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.


int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
//
......



}


on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.

As per my understanding, in c++ everything is object.

int* ptr=10;

when you say '10', you get object of type interger. now you want to
store this object in 'int*' object container(memory location). C++
shouts for that. C doesnt give error. you may get warning for that in
c++.

correct me if i am wrong.
Thank
Shaan.
 
D

dice

shaanxxx said:
As per my understanding, in c++ everything is object.

int* ptr=10;

when you say '10', you get object of type interger. now you want to

not quite - int is a base type that comes from C (which obviously
doesn't support objects)
store this object in 'int*' object container(memory location). C++

int i=10; //create variable of type int
int* ptr; //create a pointer to type int
ptr=&i; //set ptr to be the address of the variable of type int
shouts for that. C doesnt give error. you may get warning for that in
c++.
C is assuming you want to point to memory location 10. C++ requires you
to explicitly state what you want.

note that the outcome is exactly the same in C and C++
int* ptr=(int*)10; // this is what you are actually doing. ie pointing
to memory location 10
 
F

Flash Gordon

dice said:
shaanxxx wrote:


C is assuming you want to point to memory location 10. C++ requires you
to explicitly state what you want.

No C doesn't. C requires that the line "int *ptr=10;" produces a
diagnostic message. The compiler is then allowed to either fail to
compile the program or to compile it but do anything at all when that
line is executed.
note that the outcome is exactly the same in C and C++
int* ptr=(int*)10; // this is what you are actually doing. ie pointing
to memory location 10

Or possibly memory location 10 * sizeof int, or whatever else the
implementation defines the conversion as being.
 
S

Skarmander

John said:
That's a weird thing to assume, as it's not something that can be done
with C (nor C++, which is where I imagine you got the new keyword from).
Off-topic, but this is perfectly valid C++, and will do the expected thing.

Despite C++'s hybrid type systems, some amenities of the O-O syntax are
still preserved for the primitive types. In particular, constructor syntax
is supported for initialization.

Of course, it leads to predictable complaints that one can now confuse
int* ptr = new int(10);
with
int* ptr = new int[10];
which are very different things indeed.

S.
 
S

shaanxxx

Flash said:
shaanxxx wrote:





You are wrong. If you want to talk about C++ try comp.lang.c++ that is
why the group exists.

For comparision, we can use any C or c++ group. There is no harm in
that. If same question is asked on C++ , they would say that better you
this question on C group . So, i feel, we can compare c/c++ on any
group.

Thanks
Shaan.
 
L

lovecreatesbeauty

Flash said:
It is also an illegal assignment and the compiler is *required* to issue
a diagnostic.

Is this right to make ptr address to memory unit #10:

int *ptr = (int *)10;
 
F

Frederick Gotham

Skarmander posted:
Despite C++'s hybrid type systems, some amenities of the O-O syntax are
still preserved for the primitive types. In particular, constructor syntax
is supported for initialization.


If anything, it's good for writing templates.
 
S

Stephen Sprunk

shaanxxx said:
For comparision, we can use any C or c++ group. There is no harm in
that. If same question is asked on C++ , they would say that better
you
this question on C group . So, i feel, we can compare c/c++ on any
group.

There is no language called C/C++. C and C++ are two different
languages, and the meaning of a particular construction may vary between
them even if the syntax is valid in both languages.

If you want C answers, ask in comp.lang.c. If you want C++ answers, ask
in comp.lang.c++. If the C++ gurus tell you to ask here, then you're
dealing with the subset of C++ which is also valid C and presumably has
the same meaning -- but that's not safe to assume otherwise. There's
lots of C++ that looks like C but is either invalid (as C) or has a
significantly different meaning.

S
 
W

wmaple

It's right, but meaningless. Don't do so and keep the complier doing everything, such as getting the address of a variable.
 
J

John Tsiombikas

Skarmander said:
Off-topic, but this is perfectly valid C++, and will do the expected thing.

Despite C++'s hybrid type systems, some amenities of the O-O syntax are
still preserved for the primitive types. In particular, constructor
syntax is supported for initialization.

I aplogize for spreading misinformation then.
[OT]
I knew of this syntax, but I was under the impression that it was some
sort of gcc extension to C++ just like the initialization for
dynamically allocated arrays which is similar to that:
int *ptr = new int[size](10);
Which is not standard C++
[/OT]
 
F

Flash Gordon

shaanxxx said:
For comparision, we can use any C or c++ group. There is no harm in
that. If same question is asked on C++ , they would say that better you
this question on C group . So, i feel, we can compare c/c++ on any
group.

On that message you were specifically talking about C++ not C and you
did not talk about C at all. In any case, if you want to know the rest
of what was wrong go ask in a C++ news group.
 
K

Keith Thompson

lovecreatesbeauty said:
Is this right to make ptr address to memory unit #10:

int *ptr = (int *)10;

Probably. The cast converts the value 10, of type int, to a pointer
type, namely int* (pointer-to-int).

C99 6.3.2.3p5 says:

An integer may be converted to any pointer type. Except as
previously specified, the result is implementation-defined, might
not be correctly aligned, might not point to an entity of the
referenced type, and might be a trap representation.

with a footnote:

The mapping functions for converting a pointer to an integer or an
integer to a pointer are intended to be consistent with the
addressing structure of the execution environment.

So *if* the concept of an address with the value 10 makes sense on the
underlying system, then "(int*)10" should be the way to obtain that
value.

But it's very unlikely that this is going to be useful. I'd be
surprised if any actual system had a meaningful int object
(particularly a writable one, since you didn't include "const" in your
declaration) at location 10 in memory.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top