3

#include <stdio.h>
#include <stdlib.h>

struct Node{
  int data;
  struct Node* link;
};
struct Node* A;
int main(){
  struct Node* temp = (struct Node*)malloc(sizeof(Node));
  temp->data = 2;
  temp->link = NULL;
  A = temp; // in this line i have doubt
  return 0;
}

The doubt is that: A and temp both are pointer to node. A = temp can have two meanings:

  1. We are copying the address of temp in A, hence A will point to the same address.(means both of them are same identities/variables)
  2. We are copying the elements of temp and assigning it to elements of A.(means both of them are separate identities/variables). We generally do this in structures.

So please help me to understand it.

uditkumar01
  • 400
  • 5
  • 11
  • You never allocated any memory for `temp`. And `temp->data = NULL` should probably be `temp->link = NULL`. – Barmar Jun 30 '20 at 05:39
  • temp is a pointer, not a struct, so temp->data = 2; is invalid - you're assigning to memory that has not been allocated. You need to either declare "Node temp;" to allocate it on the stack, or allocate an instance with new(). Related: https://stackoverflow.com/questions/9397288/dynamically-allocate-memory-for-struct – Corbell Jun 30 '20 at 05:42
  • If you were allocated memory for temp or A, or assign them an address of some variable then they both pointed to same address. – ValeriF21 Jun 30 '20 at 05:44
  • @Barmar yeah but i am asking something else please check the description. i know the topic isn't clear but i can't put this whole doubt in the topic. – uditkumar01 Jun 30 '20 at 06:16
  • @Corbell yeah you mean i need to use malloc to assign the memory for it first. yeah that was one of the mistakes i did but i was asking something else plz read the descriptions to get what i am asking. By the way thanks. – uditkumar01 Jun 30 '20 at 06:19
  • Please don't tag questions as c and c++, they are different languages and will produce different answers – Alan Birtles Jun 30 '20 at 06:26

2 Answers2

1

Assigning a pointer just copies the address, not what the pointer points to. So A = temp; makes A and temp point to the same address.

If you want to copy the elements, you have to dereference the pointers: *A = *temp;

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The OP's code has enough undefined behaviour that ignoring it is misleading. Technically, in C++, `*A=*temp` won't copy anything in the above code, because (a) the above program's behaviour is already unspecified, and (b) there is no line where `*A=*temp` doesn't also result in unspecified behaviour. Wording this correctly is challenging, but unless you do I think this answer is incorrect *and* harmful to the questioner. – Yakk - Adam Nevraumont Jun 30 '20 at 06:09
  • https://stackoverflow.com/questions/4931123/copying-one-structure-to-another **Check this out ** in this they have used A=temp for copying the elements. – uditkumar01 Jun 30 '20 at 06:11
  • @uditkumar11 Because that's a structure variable, not a pointer. – Barmar Jun 30 '20 at 13:40
1

First thing first right. How can you assign Null intemp->data = NULL; here data is int type.

Your option 1st is correct.

And you have just declared structure pointer variable but not initialized.

Your code had some errors I fixed. Run the code below and see both A and temp have same address after A=temp; statement which means both are pointing same thing.

#include <stdio.h>
#include <stdlib.h>

struct Node{
   int data;
   struct Node* link;
};  // you had forgot ';' here
struct Node* A;
int main(){
   struct Node *temp;
   temp=(struct Node*)malloc(sizeof(struct Node));  
   // this allocates memory and assign its address into temp structure pointer
   temp->data = 2;  
   temp->link = NULL; // here you was doing mistake
   A = temp; // in this line i have doubt
   printf(" Address of pointer A %p", A);
   printf("\n Address of pointer temp is %p",temp);

 return 0;
}

Let me know if you have still any doubt.

Ankit Mishra
  • 530
  • 8
  • 16
  • yeah i wrote temp->data instead of temp-> link. really sorry for that – uditkumar01 Jun 30 '20 at 06:24
  • please check code snippet and ask if any doubt or your answer is still not clear to you – Ankit Mishra Jun 30 '20 at 06:26
  • Thanks my doubt is about to clear. If i declare a new struct like (temp1 = (struct Node*)malloc(sizeof(struct Node)); ) now if i do temp1 = temp then what does it mean either i am assigning the address of temp in temp1 or am i copying the elements – uditkumar01 Jun 30 '20 at 06:28
  • pointer variable is different than normal variables. they are meant to point other variables of same type. That is why you have to allocate memory and then assign it to struct pointer variable here – Ankit Mishra Jun 30 '20 at 06:31
  • Technically a pointer holds the address of the object it points to. But the `printf` text is misleading. A is a pointer and there is an address. The term "Address of pointer A" would be `&A`. – harper Jun 30 '20 at 06:32
  • I mean addresses that pointers are holding they are same. as you have assigned `A=temp` – Ankit Mishra Jun 30 '20 at 06:34
  • You mean if we don't assign memory to that pointer to node (like in case of A=temp) then it will store the address of that temp. But if we assign memory to it then(like in case of temp1=temp) it will copy the elements as memory is already assigned to it. – uditkumar01 Jun 30 '20 at 06:41
  • no copy of element i never said . you are messing up things. – Ankit Mishra Jun 30 '20 at 06:42
  • so how can i copy the elements of one struct to another. You can check this link in this they have done it the same way i thought (temp1 = temp) to copy the elements. https://stackoverflow.com/questions/4931123/copying-one-structure-to-another – uditkumar01 Jun 30 '20 at 06:43
  • you should read it bro. Here what I anyone can do is give direct answer to specific query. – Ankit Mishra Jun 30 '20 at 06:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216914/discussion-between-ankit-mishra-and-uditkumar11). – Ankit Mishra Jun 30 '20 at 06:50