2

I am trying to understand the logic of some code and getting with their use of pointers.

So for example, if two pointers (say A and B) to structures are declared and the equated to each other, and then the data in a structure is changed through one of the pointers, does it get changed in the second one too or is it just re-written? I guess my question is: will A and B literally point to the same thing or will one point to the copy of the other?

The code does something like this:

 somestruct *A;
 somestruct *B;
 B = A;
 A->data = 5;
 B->data = 6; 
Sasha
  • 109
  • 2
  • 12

5 Answers5

3

In your example above they will literally point to the same thing.

It is also important to allocate and deallocate memory before and after use however, or else you will get unexpected behaviour and/or segmentation faults.

somestruct *A;
somestruct *B;

A = malloc(sizeof(somestruct));

B = A;
A->data = 5;
B->data = 6; 

printf("Data in A: %d", A->data); /* Prints 6 */
printf("Data in B: %d", B->data); /* Also Prints 6 */

...

free(A);

It's also good practice to check the result of malloc and ensure that the operation was successful. This kind of situation can become an issue if for example you want to change the location A points to, and would also like to change the location B points to. Unless you change both, changing one pointers location will not update the other. This is where a double pointer would come in use. EG

somestruct *A;
somestruct **B;

A = malloc(sizeof(somestruct));
B = &A; // Get the address of A

A->member = 5;
printf("Member: %d", (*B)->member); /* Prints 5 */

free(A);
A = malloc(sifeof(somestruct));
A->member = 10;
printf("Member: %d", (*B)->member); /* Prints 10 */

Note that in the double pointer example B does not need updating again.

See the example here: Double Pointer

Community
  • 1
  • 1
Élie
  • 1,285
  • 1
  • 12
  • 27
2
  • will A and B literally point to the same thing or will one point to the copy of the other?

A and B will literally point to the same thing

kotlomoy
  • 1,420
  • 8
  • 14
2

suppose you do something like this

 somestruct *A;
 somestruct *B;
 A = malloc( sizeof(somestruct)) // addded this statement to show that memory allocation is done for A only
 B = A;
 A->data = 5;
 B->data = 6; 

this would mean that now pointer B points to the same memory location that A points to, therefore whatever you change (either by using A or B) would change both the pointers data, because essentially its the same data. It is now just being pointed by 2 pointers (namely A and B).

Haris
  • 12,120
  • 6
  • 43
  • 70
1

You are changing not the pointers but the object that is referenced to by the pointers. That is it is the object that is been changing using one of the pointers.

For example

#include <stdio.h>

int main()
{
    struct A { int i; } a = { 0 };

    struct A *p1 = &a;
    struct A *p2 = &a;

    p1->i = 10;

    printf( "%d\n", p2->i );
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

A pointer is not the structure, but simply a memory address (and an instruction to the compiler about the layout of memory), so when you act through the pointer you change the memory. Given another pointer points to the same piece of memory it points to the changed structure.

Of course, if you do something like this:

long *a = malloc(sizeof long);
long *b = a;

and then later:

b = malloc(sizeof long);

then a is not changed, though b is.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44