pointer to structure and self pointers

♀尐吖头ヾ 提交于 2019-12-11 06:28:52

问题


What is the difference between self referential pointer in structure and pointer to structure?

    struct abc
    {
    int data;
    struct abc *next;
    }
    struct abc *pt;
  1. What are the differences between *next and *pt??

  2. How they differ in their use??

I am really in doubt between these two

I am a beginner

First example is used mainly for linked list

Are pointer to structure node and self referential pointer the same thing?

please see

see-programming.blogspot.in/2013/05/chain-hashing-separate-chaining-with.html here we have used struct hash *hashTable as an array ..how?? and can we do same with *pt


回答1:


  1. They are of the same type. They behave in the exact same way.

    Some example usage:

    // declare 2 structs
    struct abc s1;
    struct abc s2;
    
    // point pt to s1
    pt = &s1;
    
    // point s1.next to s2
    s1.next = &s2;
    
    // access pt->data
    int a = pt->data;
    
    // access s1.next->data
    int a = s1.next->data;
    
  2. Differences in usage:

    • There's only one pt variable.

    • For every struct abc variable, there is a next variable.

    In the context of a linked-list, there is only one head pointer, thus pt would be it.
    But each node points to the next node, thus next should be used for this.

  3. Using pointers as arrays?

    Yes, this can be done with either pt or next.

    A pointer just points to an address in memory. There can be any number of structs following on each other at that location.

    If you want to use it as an array (or just using pointers in general), you just have to make sure you don't try to access elements that you didn't allocate memory for (with malloc for example) and free the memory after usage (if you used malloc).

    Some example usage with array:

    // declare a struct
    struct abc s1;
    
    // make an array of size 10
    struct abc *a1 = malloc(10*sizeof(struct abc));
    
    // give the 4th element a new value
    a1[4] = s1;
    
    // free the memory
    free(a1);
    

I hope that helps a bit.




回答2:


Conceptually, very little difference at all.

  1. next is a member of the same structure it is pointing to. pt is not a member of the structure it is pointing to.
  2. They are used in a similar way, except that to use next you have a have an existing struct abc, and pt can be used directly. Please consider:
    myABC.next= &myOtherABC ;
    pt= &myOtherABC ;

are pointer to structure node and self referential pointer same thing

They are and they are not. Depends on point of view. They are because they both point to a structure. They are not because a pointer to structure can point to any structure, and as a variable it can be a parameter, a local variable, a member of another struct, etc. But a self referential pointer is necessarily a member of a struct and points to the same struct it is a member of.




回答3:


The only difference applies to people that write compilers. That is because a self-referential pointer refers to itself before itself is fully declared. So someone writing a compiler has to deal with this special case.

As a programmer (that means you), there is no difference, and the terms do not offer any additional meaning.




回答4:


  1. they are the same type but they do not contain the same value

  2. from pt you can access next because pt point to a structure that contain next. but from next you can't access pt.

for linked list, you have to understand, that the list contain at the begining 1 element, and this element know how reach the next element of the list, it is the goal of the next pointer




回答5:


There is no difference between those two pointers.

A possible difference in usage depends on the context but still does not apply to their nature of being both pointer to the same type of structure and therefore undergoing the same rules in terms of assignment, reading and arithmetics.



来源:https://stackoverflow.com/questions/18451788/pointer-to-structure-and-self-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!