Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > incompatible pointer type

Reply
Thread Tools

incompatible pointer type

 
 
william
Guest
Posts: n/a
 
      03-07-2007
code segment:

long int * size;
char entry[4][16];
.............
size=&entry[row][col];

*************************************
Gcc reported 'assignment of incompatible pointer type'.

MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying? Why there can be different pointer types. If
so, what is the difference between an integer pointer type and a float
pointer type, for example?

Thank you!

Ji

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      03-07-2007

"william" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> code segment:
>
> long int * size;
> char entry[4][16];
> ............
> size=&entry[row][col];
>
> *************************************
> Gcc reported 'assignment of incompatible pointer type'.


The report is correct.
You should change 'size' to type 'char *'

>
> MY QUESTION IS:
> I understand that pointer is just a memory unit that contains the
> address(or starting address)of other data object(i.e. int, float,
> array, struct, etc.). So it is just an address. How does C implement
> the pointer underlying?


That's specific to the implementation.

>Why there can be different pointer types.


Because the language defines them as different.

> If
> so, what is the difference between an integer pointer type and a float
> pointer type, for example?


The difference is that they're two different types.

-Mike


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-07-2007
"william" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> code segment:
>
> long int * size;
> char entry[4][16];
> ............
> size=&entry[row][col];
>
> *************************************
> Gcc reported 'assignment of incompatible pointer type'.
>
> MY QUESTION IS:
> I understand that pointer is just a memory unit that contains the
> address(or starting address)of other data object(i.e. int, float,
> array, struct, etc.). So it is just an address. How does C implement
> the pointer underlying? Why there can be different pointer types. If
> so, what is the difference between an integer pointer type and a float
> pointer type, for example?
>
> Thank you!
>
> Ji


And int pointer type and a float pointer type point to different types of
data. int* points to data that contains an integer. float* points to data
that contains a float. There may be some circumstances where you may wish
to convert between pointer types, but you better know what you're doing.

In your example, you are making size a pointer to an int, but then trying to
get it to point to a character. The compiler rightfully says, wait a second
buddy, those are two different types. If you really wish to do this, the
you could use reinterpret_cast to change the type of the pointer. It would
compile but depending on hwo you are use it could cause all kinds of
problems at run time. I.E.

size = reinterpret_cast<int*>( &entry[row][col] );

Again, this is very dangerous. Especially since a char is only 1 character
and an interger is more (4 on my system, 8 on some, who knows how many on
others).


 
Reply With Quote
 
S S
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 8:13 pm, "william" <(E-Mail Removed)> wrote:
> code segment:
>
> long int * size;
> char entry[4][16];
> ............
> size=&entry[row][col];
>
> *************************************
> Gcc reported 'assignment of incompatible pointer type'.
>
> MY QUESTION IS:
> I understand that pointer is just a memory unit that contains the
> address(or starting address)of other data object(i.e. int, float,
> array, struct, etc.). So it is just an address. How does C implement
> the pointer underlying? Why there can be different pointer types. If
> so, what is the difference between an integer pointer type and a float
> pointer type, for example?
>
> Thank you!
>
> Ji


Say, how would you increment the pointer then? You need to do several
operations on pointer and if compiler does not know what type it is,
you cannt do even p++. As simple as that.

 
Reply With Quote
 
william
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 11:32 am, "S S" <(E-Mail Removed)> wrote:
> On Mar 7, 8:13 pm, "william" <(E-Mail Removed)> wrote:
>
>
>
> > code segment:

>
> > long int * size;
> > char entry[4][16];
> > ............
> > size=&entry[row][col];

>
> > *************************************
> > Gcc reported 'assignment of incompatible pointer type'.

>
> > MY QUESTION IS:
> > I understand that pointer is just a memory unit that contains the
> > address(or starting address)of other data object(i.e. int, float,
> > array, struct, etc.). So it is just an address. How does C implement
> > the pointer underlying? Why there can be different pointer types. If
> > so, what is the difference between an integer pointer type and a float
> > pointer type, for example?

>
> > Thank you!

>
> > Ji

>
> Say, how would you increment the pointer then? You need to do several
> operations on pointer and if compiler does not know what type it is,
> you cannt do even p++. As simple as that.


I understand it. It makes a lot of sense. Pointer++

 
Reply With Quote
 
william
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 3:12 pm, "william" <(E-Mail Removed)> wrote:
> On Mar 7, 11:32 am, "S S" <(E-Mail Removed)> wrote:
>
>
>
> > On Mar 7, 8:13 pm, "william" <(E-Mail Removed)> wrote:

>
> > > code segment:

>
> > > long int * size;
> > > char entry[4][16];
> > > ............
> > > size=&entry[row][col];

>
> > > *************************************
> > > Gcc reported 'assignment of incompatible pointer type'.

>
> > > MY QUESTION IS:
> > > I understand that pointer is just a memory unit that contains the
> > > address(or starting address)of other data object(i.e. int, float,
> > > array, struct, etc.). So it is just an address. How does C implement
> > > the pointer underlying? Why there can be different pointer types. If
> > > so, what is the difference between an integer pointer type and a float
> > > pointer type, for example?

>
> > > Thank you!

>
> > > Ji

>
> > Say, how would you increment the pointer then? You need to do several
> > operations on pointer and if compiler does not know what type it is,
> > you cannt do even p++. As simple as that.

>
> I understand it. It makes a lot of sense. Pointer++


**********************************
My original intention was to use any bytes to comprise an int. So I
used two ways to do that:

1.the function atoi():

However, I still get some problems here:
char c[]="ABCD";
int i;

i=atoi(&c[0]);// I expected to get 65: A's ascii value

the result of the line of code above is ZERO, which confuse me a lot.

2.conversion:

i=(int)c[0];

this case it worked. However, if c[0]is arbitery. 'i' always get
negative value, how come?

Thanks


 
Reply With Quote
 
jlongstreet@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 2:19 pm, "william" <(E-Mail Removed)> wrote:

> My original intention was to use any bytes to comprise an int. So I
> used two ways to do that:
>
> 1.the function atoi():
>
> However, I still get some problems here:
> char c[]="ABCD";
> int i;
>
> i=atoi(&c[0]);// I expected to get 65: A's ascii value
>
> the result of the line of code above is ZERO, which confuse me a lot.


I think you are misinterpreting the meaning of atoi().

atoi() assumes that the string is an ASCII representation of an
integer.

char *c = "1234";
i = atoi(c); // i == 1234

When the string is not an ASCII representation of an integer (like
"ABCD"), it returns zero.

char *c = "ABCD";
i = atoi(c); // i == 0

>
> 2.conversion:
>
> i=(int)c[0];
>
> this case it worked. However, if c[0]is arbitery. 'i' always get
> negative value, how come?


What do you mean by arbitrary?

Think of it this way -- the integral types in C/C++ are just that:
integers.
On most 32-bit architectures, a char is one byte, and an int is 4.
It's still a one-byte integer, though.

 
Reply With Quote
 
william
Guest
Posts: n/a
 
      03-08-2007
On Mar 7, 5:01 pm, (E-Mail Removed) wrote:
> On Mar 7, 2:19 pm, "william" <(E-Mail Removed)> wrote:
>
> > My original intention was to use any bytes to comprise an int. So I
> > used two ways to do that:

>
> > 1.the function atoi():

>
> > However, I still get some problems here:
> > char c[]="ABCD";
> > int i;

>
> > i=atoi(&c[0]);// I expected to get 65: A's ascii value

>
> > the result of the line of code above is ZERO, which confuse me a lot.

>
> I think you are misinterpreting the meaning of atoi().
>
> atoi() assumes that the string is an ASCII representation of an
> integer.
>
> char *c = "1234";
> i = atoi(c); // i == 1234
>
> When the string is not an ASCII representation of an integer (like
> "ABCD"), it returns zero.
>
> char *c = "ABCD";
> i = atoi(c); // i == 0
>
>
>
> > 2.conversion:

>
> > i=(int)c[0];

>
> > this case it worked. However, if c[0]is arbitery. 'i' always get
> > negative value, how come?

>
> What do you mean by arbitrary?
>

By arbitrary, I mean if c[0] contains any possible valued raning from
0x00~0xff. Then sometimes, (int)any_char returns negative value.

I got a solution now: if I specify "any_char" as an unsigned char, the
results of conversion is always postive.

Again, does any one has better solution to creat an int using 4
individual bytes(or a 4 byte array).

Thanks
> Think of it this way -- the integral types in C/C++ are just that:
> integers.
> On most 32-bit architectures, a char is one byte, and an int is 4.
> It's still a one-byte integer, though.



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-08-2007
william wrote:
> On Mar 7, 5:01 pm, (E-Mail Removed) wrote:
>
>>On Mar 7, 2:19 pm, "william" <(E-Mail Removed)> wrote:
>>
>>
>>>My original intention was to use any bytes to comprise an int. So I
>>>used two ways to do that:

>>
>>>1.the function atoi():

>>
>>>However, I still get some problems here:
>>>char c[]="ABCD";
>>>int i;

>>
>>>i=atoi(&c[0]);// I expected to get 65: A's ascii value

>>
>>>the result of the line of code above is ZERO, which confuse me a lot.

>>
>>I think you are misinterpreting the meaning of atoi().
>>
>>atoi() assumes that the string is an ASCII representation of an
>>integer.
>>
>>char *c = "1234";
>>i = atoi(c); // i == 1234
>>
>>When the string is not an ASCII representation of an integer (like
>>"ABCD"), it returns zero.
>>
>>char *c = "ABCD";
>>i = atoi(c); // i == 0
>>
>>
>>
>>
>>>2.conversion:

>>
>>>i=(int)c[0];

>>
>>>this case it worked. However, if c[0]is arbitery. 'i' always get
>>>negative value, how come?

>>
>>What do you mean by arbitrary?
>>

>
> By arbitrary, I mean if c[0] contains any possible valued raning from
> 0x00~0xff. Then sometimes, (int)any_char returns negative value.
>
> I got a solution now: if I specify "any_char" as an unsigned char, the
> results of conversion is always postive.
>
> Again, does any one has better solution to creat an int using 4
> individual bytes(or a 4 byte array).
>
> Thanks
>
>>Think of it this way -- the integral types in C/C++ are just that:
>>integers.
>>On most 32-bit architectures, a char is one byte, and an int is 4.
>>It's still a one-byte integer, though.

>
>
>


A union is one possiblilty

union S
{
char a[4];
int i;
};

S s;
s.a[0] = 'A';
s.a[1] = 'B';
s.a[2] = 'C';
s.a[3] = 'D';
cout << s.i;

Of course the are all sorts of platform dependencies here, this is not
portable code.

john
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-08-2007
>
> A union is one possiblilty
>
> union S
> {
> char a[4];
> int i;
> };
>
> S s;
> s.a[0] = 'A';
> s.a[1] = 'B';
> s.a[2] = 'C';
> s.a[3] = 'D';
> cout << s.i;
>
> Of course the are all sorts of platform dependencies here, this is not
> portable code.
>
> john


BTW i'm not trying to suggest the any sort of conversion from hex is
going on here (not sure if that is what you want or not).

john
 
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
incompatible pointer type warning ? Michael C Programming 8 06-05-2006 09:17 PM
Pointer incompatible type assignment to character. gk245 C Programming 10 04-22-2006 05:37 AM
Passing arg from incompatible pointer type Chris Readle C Programming 15 06-24-2004 11:32 AM
tmpnam, strcpy - incompatible pointer type? Josh Wilson C Programming 1 06-21-2004 05:05 PM
initialization from incompatible pointer type Brian Stubblefield C Programming 3 05-27-2004 01:31 PM



Advertisments