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.

6
  • You never allocated any memory for temp. And temp->data = NULL should probably be temp->link = NULL. Commented Jun 30, 2020 at 5: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: stackoverflow.com/questions/9397288/… Commented Jun 30, 2020 at 5: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. Commented Jun 30, 2020 at 5: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. Commented Jun 30, 2020 at 6: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. Commented Jun 30, 2020 at 6:19

2 Answers 2

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;

Sign up to request clarification or add additional context in comments.

3 Comments

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.
stackoverflow.com/questions/4931123/… **Check this out ** in this they have used A=temp for copying the elements.
@uditkumar11 Because that's a structure variable, not a pointer.
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.

11 Comments

yeah i wrote temp->data instead of temp-> link. really sorry for that
please check code snippet and ask if any doubt or your answer is still not clear to you
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
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
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.