incompatible pointer type

Discussion in 'C++' started by william, Mar 7, 2007.

  1. william

    william Guest

    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
     
    william, Mar 7, 2007
    #1
    1. Advertising

  2. william

    Mike Wahler Guest

    "william" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Mar 7, 2007
    #2
    1. Advertising

  3. william

    Jim Langston Guest

    "william" <> wrote in message
    news:...
    > 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).
     
    Jim Langston, Mar 7, 2007
    #3
  4. william

    S S Guest

    On Mar 7, 8:13 pm, "william" <> 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.
     
    S S, Mar 7, 2007
    #4
  5. william

    william Guest

    On Mar 7, 11:32 am, "S S" <> wrote:
    > On Mar 7, 8:13 pm, "william" <> 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++ :)
     
    william, Mar 7, 2007
    #5
  6. william

    william Guest

    On Mar 7, 3:12 pm, "william" <> wrote:
    > On Mar 7, 11:32 am, "S S" <> wrote:
    >
    >
    >
    > > On Mar 7, 8:13 pm, "william" <> 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
     
    william, Mar 7, 2007
    #6
  7. william

    Guest

    On Mar 7, 2:19 pm, "william" <> 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.
     
    , Mar 7, 2007
    #7
  8. william

    william Guest

    On Mar 7, 5:01 pm, wrote:
    > On Mar 7, 2:19 pm, "william" <> 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.
     
    william, Mar 8, 2007
    #8
  9. william wrote:
    > On Mar 7, 5:01 pm, wrote:
    >
    >>On Mar 7, 2:19 pm, "william" <> 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
     
    John Harrison, Mar 8, 2007
    #9
  10. >
    > 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
     
    John Harrison, Mar 8, 2007
    #10
  11. william

    Jim Langston Guest

    (Received in e-mail, replying in group so people can correct any mistakes I
    make)

    On Mar 7, 10:56 am, "Jim Langston" <> wrote:
    > "william" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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).



    - Thank you, Jim. What I really plan to do is to assign 4 arbitery bytes
    - to an int. I was actually manipulating the bytes in MBR entry, I want
    - to read the 4 bytes start from any address I specified(here I use the
    - char[] to specify the address).
    -
    - So how could I get any 4 bytes in the MBR entry and convert it as an
    - integer? Thank you again.
    -
    - Sincerely Ji

    size = reinterpret_cast<int*>( somecharpointer );
    *may* work for you, or it may not It depends on a lot on the architecture
    you plan on running this on.

    Some CPUs have problems reading integers that are not aligned on specific
    byte boundaries, some do not. I believe (but could be mistaken) that AMD
    and Intel are okay with this.

    So, say, you had some char pointer pointing to arbitrary data that you can
    read. If you are not worried about cross platform compatability, then I
    would just point the int pointer to the start of where you think the array
    is.

    char* Data = SomeFunctionReturningChar*( someparm );
    int* IntData = reinterpret_cast<int*>( Data );
    At this point, if your architecture isn't too restrictive, you should be
    able to read the contents of IntData as an integer, I.E.
    std::cout << *IntData;
    Should give you some int value, as long as the pointer points to memory you
    have rights to read.
    Notice, however, that incrementing your int pointer will increment it 4
    bytes, not 1, because the compiler thinks it's int data you're pointing to.

    ++IntData;
    will make IntData point to 4 bytes later, not one. Just as
    IntData[1];
    will look at the 5th through 8th bytes in the data.

    Now, there are other ways to do it. There've been times I wanted to look at
    data and didn't care to store it, so
    std::cout << *reinterpret_cast<int*>( Data );
    would give me an int value.
    std::cout << *reinterpret_cast<int*>( Data + 1 );
    would give me the 2nd through 5th bytes as an int, as would
    std::cout << *reinterpret_cast<int*>( &Data[1] );

    It really depends on what you are trying to accomplish how you would do it.
    Looking and displaying the data the worst that usually can happen is you can
    crash your program, reading data you don't own, etc.. Changing the data can
    get you in trouble if you're not careful at what your'e pointing at, but
    that's pretty much the same for all pointers.
     
    Jim Langston, Mar 8, 2007
    #11
  12. william

    Marcus Kwok Guest

    John Harrison <> wrote:
    > 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;


    Technically it is undefined behavior to store something in a union and
    then to try to read a different member of the union.

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


    Right, it *may* work on the OP's implementation.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Mar 13, 2007
    #12
  13. william

    william Guest

    On Mar 13, 3:17 pm, (Marcus Kwok) wrote:
    > John Harrison <> wrote:
    > > 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;

    >
    > Technically it is undefined behavior to store something in a union and
    > then to try to read a different member of the union.
    >
    > > Of course the are all sorts of platform dependencies here, this is not
    > > portable code.

    >
    > Right, it *may* work on the OP's implementation.
    >
    > --
    > Marcus Kwok
    > Replace 'invalid' with 'net' to reply


    Thank you for all the replies and the effort.
     
    william, Mar 17, 2007
    #13
  14. william

    william Guest

    On Mar 8, 5:37 am, "Jim Langston" <> wrote:
    > (Received in e-mail, replying in group so people can correct any mistakes I
    > make)
    >
    > On Mar 7, 10:56 am, "Jim Langston" <> wrote:
    >
    >
    >
    > > "william" <> wrote in message

    >
    > >news:...

    >
    > > > 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).

    >
    > - Thank you, Jim. What I really plan to do is to assign 4 arbitery bytes
    > - to an int. I was actually manipulating the bytes in MBR entry, I want
    > - to read the 4 bytes start from any address I specified(here I use the
    > - char[] to specify the address).
    > -
    > - So how could I get any 4 bytes in the MBR entry and convert it as an
    > - integer? Thank you again.
    > -
    > - Sincerely Ji
    >
    > size = reinterpret_cast<int*>( somecharpointer );
    > *may* work for you, or it may not It depends on a lot on the architecture
    > you plan on running this on.
    >
    > Some CPUs have problems reading integers that are not aligned on specific
    > byte boundaries, some do not. I believe (but could be mistaken) that AMD
    > and Intel are okay with this.
    >
    > So, say, you had some char pointer pointing to arbitrary data that you can
    > read. If you are not worried about cross platform compatability, then I
    > would just point the int pointer to the start of where you think the array
    > is.
    >
    > char* Data = SomeFunctionReturningChar*( someparm );
    > int* IntData = reinterpret_cast<int*>( Data );
    > At this point, if your architecture isn't too restrictive, you should be
    > able to read the contents of IntData as an integer, I.E.
    > std::cout << *IntData;
    > Should give you some int value, as long as the pointer points to memory you
    > have rights to read.
    > Notice, however, that incrementing your int pointer will increment it 4
    > bytes, not 1, because the compiler thinks it's int data you're pointing to.
    >
    > ++IntData;
    > will make IntData point to 4 bytes later, not one. Just as
    > IntData[1];
    > will look at the 5th through 8th bytes in the data.
    >
    > Now, there are other ways to do it. There've been times I wanted to look at
    > data and didn't care to store it, so
    > std::cout << *reinterpret_cast<int*>( Data );
    > would give me an int value.
    > std::cout << *reinterpret_cast<int*>( Data + 1 );
    > would give me the 2nd through 5th bytes as an int, as would
    > std::cout << *reinterpret_cast<int*>( &Data[1] );
    >
    > It really depends on what you are trying to accomplish how you would do it.
    > Looking and displaying the data the worst that usually can happen is you can
    > crash your program, reading data you don't own, etc.. Changing the data can
    > get you in trouble if you're not careful at what your'e pointing at, but
    > that's pretty much the same for all pointers.


    Thank you very much for the detailed reply and abundant background
    stated above.
     
    william, Mar 17, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Stubblefield

    initialization from incompatible pointer type

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    3
    Views:
    15,233
    Brian Stubblefield
    May 27, 2004
  2. Chris Readle

    Passing arg from incompatible pointer type

    Chris Readle, Jun 20, 2004, in forum: C Programming
    Replies:
    15
    Views:
    24,986
    Dan Pop
    Jun 24, 2004
  3. Josh Wilson

    tmpnam, strcpy - incompatible pointer type?

    Josh Wilson, Jun 21, 2004, in forum: C Programming
    Replies:
    1
    Views:
    3,799
    Eric Sosman
    Jun 21, 2004
  4. gk245
    Replies:
    10
    Views:
    1,018
    Peter Shaggy Haywood
    Apr 22, 2006
  5. Michael

    incompatible pointer type warning ?

    Michael, Jun 5, 2006, in forum: C Programming
    Replies:
    8
    Views:
    519
    Ian Collins
    Jun 5, 2006
Loading...

Share This Page