Thread: Incompatible pointer type !?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    10

    Incompatible pointer type !?

    hi

    i am having an structure like
    Code:
     
    struct structag
    {
    datatype datavariable;
    struct structag *ptr2struct;    
    };
    in a program... and

    i am allocating memory for this structure and assign the pointer to the another structure variable of same type

    as

    struct structag *head = malloc ()...
    .
    struct structag *obj = malloc()...
    .
    now in the head variable i tried to assign like
    Code:
    head->ptr2struct[0] = obj;
    .
    obj = malloc()..
    .
    head->ptr2struct[1] = obj;
    ..
    i am trying this in gcc compiler on linux os.. this generates a compile time error as "Incompatible pointer type"., for the statment
    Code:
    head->ptr2struct[0] = obj; and head->ptr2struct[1] = obj;
    i dont understand why this says error., Can anyone explain this.. Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post a small "working" (in your case, "broken") example that you're attempting to compile.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Quzah.. Is my question is not understandable ...

    I am not having a problem in posting it... But it is in my linux environment.. i am posting this question from XP...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > head->ptr2struct[0] = obj; and head->ptr2struct[1] = obj;
    You don't have an array of pointers.

    head->ptr2struct = obj;
    Would be valid in this instance.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Thanks salem...

    since i dont want to declare it as array of pointers since, i dont know the size in the compile time, so i have declared this as pointer itself.. (i assume not wrong)

    But anyway, we can access this array na...

    since an array and pointer are interchangeble .. since representing the array in pointer notation as
    Code:
    {
    int array[10];
    int number;
    
    number = *array;
    number = *(array+1); // same as number = array[1]
    number = *(array+2); // same as number = array[2]
    }
    is acceptable then why not

    Code:
    head->ptr2struct[0] = obj;
    head->ptr2struct[1] = obj;
    also i done the program upon reading the concept of structure hacking..
    that is ...

    Code:
    struct structag
    {
    datatype datavariable;
    struct structag *ptr2struct;     ===> this is * equivalently, int ptr2struct[1]; can also be used is it !?
    };
    so i tried like allocating the memory at runtime one by one and decided to add like..array accessing

    But getting this incompatible error...

    One of my collegue saying that it will be compatible in C99.. but i dont know.. since i have not tried, it .. any info.. on this... plz..

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by nilathinesh
    since an array and pointer are interchangeble
    Common myth. This applies only to a few situations, and you're doing it wrong, anyway.

    Code:
    head->ptr2struct[0] = obj;
    head->ptr2struct[1] = obj;
    ptr2struct is of type struct whatevr*. obj is too. But ptr2struct[0] is of type struct whatevr, not a pointer to it.

    Code:
    struct structag
    {
    datatype datavariable;
    struct structag *ptr2struct;     ===> this is * equivalently, int ptr2struct[1]; can also be used is it !?
    };
    No, they're not the same. The pointer places a pointer in the struct. The array of size 1 places an array of size 1 in the struct (which is impossible, because you'd need the complete definition of the struct at this point, which you don't have; and anyway, it would be like placing a simple instance of the struct in the struct, i.e. infinite size.)

    so i tried like allocating the memory at runtime one by one and decided to add like..array accessing

    But getting this incompatible error...
    You're allocating objects and then you're trying to assign pointers to those objects.

    One of my collegue saying that it will be compatible in C99.. but i dont know.. since i have not tried, it .. any info.. on this... plz..
    No, it will not. You've got a simple type mismatch, and never in a thousand years will this ever be compatible.
    The C99 feature is for undefined size arrays, but it works differently, and definitely not for nesting itself.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You want something like this then
    Code:
    struct structag
    {
        datatype datavariable;
        struct structag **ptr2struct;
    };
    
    head->ptr2struct = malloc ( 2 * sizeof *head->ptr2struct );
    head->ptr2struct[0] = obj;
    head->ptr2struct[1] = obj;
    Change 2 for some other number, if you've stored however many you want somewhere else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    And now i tried this as below and it compiles.. i am not sure is it ok..

    Code:
    #include <stdio.h>
    struct structag
    {
      int a;
      struct structag *ptr;
    };
    int main()
    {
    struct structag *head, *obj;
    head =(struct structag*) malloc(sizeof(struct structag));
    
    head->a =10;
    obj =(struct structag*) malloc(sizeof(struct structag));
    obj->a =20;
    head->ptr[0] = obj[0];
    obj =(struct structag*) malloc(sizeof(struct structag));
    obj->a =20;
    head->ptr[1] = obj[0];
    }
    rather i got the segmentation fault then :-(

    Can anyone update me is it ok with C99 since i dont have that compiler...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you keep trying to index these pointers as if they were arrays?
    Why do you keep casting malloc?
    Why aren't you paying attention to anything anyone tells you?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what you're really after is a linked list.

    Code:
    #include <stdio.h>
    struct structag
    {
      int a;
      struct structag *ptr;
    };
    int main()
    {
    struct structag *head, *current, *obj;
    
    current = malloc( sizeof *current );
    head = current;
    
    current->a =10;
    obj = malloc(sizeof *obj );
    current->ptr = obj;
    current = obj;
    
    current->a =20;
    obj = malloc(sizeof *obj );
    current->ptr = obj;
    current = obj;
    
    current->ptr = NULL; /* end of list */
    
    }
    Now wrap those up into concepts such as
    - initialise list
    - append to like
    - create node
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    quzah...

    Why do you keep trying to index these pointers as if they were arrays?
    I understand that it is wrong to access the pointer as array.. But one of my collegue informed that it is compatible in C99 so i tried..
    Why do you keep casting malloc?
    what is the wrong on it... !?
    Why aren't you paying attention to anything anyone tells you?
    Dont think of i am not paying attention to it. Since i need to found whether it is compatible with C99 that is it ...

    anyway thanks quazh for your reply...

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Thanks to all... but still i could not ensure is it compatible with C99 !?.. but i assume it is not... with all 'C' and doze off by collegue. :-)

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider:
    Code:
    char *ptr = "abcde";
    char c;
    
    c = ptr[ 0 ];
    This is fine. Indexing further down the line is also fine, so long as you actually have something valid to point to. You were doing this:
    Code:
    struct foo *ptr = malloc( sizeof *ptr );
    ...
    ptr[ 1 ].something = somethingelse;
    You can't index 1, because you've only allocated enough space for one structure. Thus, the only valid index above would be:
    Code:
    ptr[ 0 ].something = somethingelse;
    Or, if you prefer:
    Code:
    ptr->something = somethingelse;
    You were trying to walk off the end of your allocated space. That's why it wasn't working.

    Yes, any single pointer can be done like so:
    Code:
    int *ptr;
    int x;
    
    ptr = &x;
    
    ptr[0] = 10;
    It's fine to use array style indexing, so long as you're actually accessing valid memory.
    Code:
    ptr[10] = foo;
    You can't just do that if you don't actually have something valid at that location.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but still i could not ensure is it compatible with C99
    By making it compatible with C89 of course.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: assignment from a incompatible pointer type
    By enderandrew in forum C Programming
    Replies: 8
    Last Post: 09-22-2007, 04:07 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM