1

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

2
  • 3
    There is no difference; they are both just pointers to struct abc objects. Any difference is only how they are used (and you haven't shown any code that uses them, so there's not much to say here...) Aug 26, 2013 at 19:44
  • what difference does pointer to a structure within structure and outside it means,,can u please give example where they both work differently or they behave in same way Aug 26, 2013 at 19:47

5 Answers 5

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

5
  • what difference does pointer to a structure within structure and outside it means,,can u please give example where they both work differently or they behave in same way Aug 26, 2013 at 19:50
  • 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 Aug 26, 2013 at 19:54
  • do we need to free the array Aug 26, 2013 at 20:10
  • and yeah while declaring nodes like a tree node or a linked list node do we need to allocate space for it using malloc??cant we declare it like we declare int?? Aug 26, 2013 at 20:12
  • @ayushnigam We also need to use malloc to allocate space for linked-list or tree nodes. If we try to do it without it, we'll inevitably end up trying to access a variable that's gone out of scope, which is undefined behaviour. Aug 27, 2013 at 7:18
1

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.

1
  • 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 Aug 26, 2013 at 19:58
1

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.

0
  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

6
  • do we need to allocate the memory using malloc for any structure??cant we declare it like declaring int Aug 26, 2013 at 20:01
  • you can declare it like an int. like that : struct abc variable; // variable is the name
    – jambono
    Aug 26, 2013 at 20:01
  • then while creating a node why do we allocate space by malloc ?? Aug 26, 2013 at 20:04
  • there are two ways, etheir you declare it from a pointer with malloc or you declare it 'like an int' it is the same result, the malloc as the advantage that you can free it when you will not need it anymore
    – jambono
    Aug 26, 2013 at 20:06
  • free it does what benefit to me?? Aug 26, 2013 at 20:39
0

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.

4
  • can u please tell difference in usage?? Aug 26, 2013 at 20:01
  • @ayushnigam: I can't, as the usage is the code and I see to few sources to know the code.
    – alk
    Aug 26, 2013 at 20:02
  • 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 Aug 26, 2013 at 20:05
  • @ayushnigam: struct hash * is not the same as struct node *. I do not see how the linked code is related to the code you show us (at least not in the way strcut hash * is used).
    – alk
    Aug 26, 2013 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.