what difference between direct pointer and indirect pointer? [duplicate] - c

This question already has an answer here:
Dynamic memory access only works inside function
(1 answer)
Closed 8 months ago.
pointer to a struct
code that can not work
struct node
{
/* data */
int data;
};
void addnode(struct node* n)
{
n = (struct node*)malloc(sizeof(struct node));
printf("value of pointer n = %p",&n);
(*n).data = 9;
}
int main()
{
struct node * n1 = NULL;
addnode(n1);
printf("data is %d\n",n1->data);
}
but the code below is fine
struct node
{
/* data */
int data;
};
void addnode(struct node ** n1)
{
// the address of struct
*n1 = (struct node*)malloc(sizeof(struct node));
printf("address of pointer &n1 = %p\n",&n1);
printf("address of pointer *n1 = %p\n",*n1);
(*n1)->data = 99;
}
int main()
{
struct node * n1 = NULL;
//pass the address of pointer instead of value of it
addnode(&n1);
printf("address of pointer &n1 = %p\n",&n1); // self address
printf("address of pointer *n1 = %p\n",&(*n1)); // the address of struct
printf("data is %d\n",n1->data);
}
what makes me confused is why the indirect pointer as a parameter is working, instead of the direct pointer.

The 'indirect' pointer, as you called it, is actually 'a pointer to your pointer variable'.
If you inspect the value of n1 in your debugger, you will see, in your first example, that it will remain NULL (because you did not assign a new value to it).
In the second example however, you assign a value to the variable by accessing it's address.
Your first code can work too if it looks like this:
struct node
{
/* data */
int data;
};
struct node* addnode()
{
struct node* n = (struct node*)malloc(sizeof(struct node));
printf("value of pointer n = %p",&n);
n->data = 9;
return n;
}
int main()
{
struct node * n1 = NULL;
n1 = addnode();
printf("data is %d\n",n1->data);
}

Related

no effect happening to the program when i am putting r=NULL in function ins()

While working with the pointers we are working on address, right?
So when a struct node pointer n is passed to t(struct node *t=n) and later if t is assigned NULL shouldn't n also become NULL?
ps-: it's a program of a binary tree
#include<stdio.h> //check at third line of ins() function
#include<stdlib.h>
struct node{
int data;
struct node* left,*right;
};
struct node* n(int dat){
struct node *x=(struct node*)malloc(sizeof(struct node));
x->data=dat;
x->left=NULL; x->right=NULL;
return x;
};
void ins(struct node* n,struct node* r){
struct node* t=r,*y=NULL; //ok so when i put r=NULL in this next line should this block of memory go
//r=NULL; //NULL
while(t!=NULL){
y=t;
if(t->data>n->data)
{
if(t->left==NULL)
{t->left=n;
t=NULL;
}
else
t=t->left;
}
else {
if(t->right==NULL){
t->right=n;
t=NULL;
}else
t=t->right;
}
}
}
void inorder(struct node* n){
if(n!=NULL){
inorder(n->left);
printf("%d ",n->data);
inorder(n->right);
}}
void main(){
struct node *a,*b,*c,*d,*e,*f,*g,*h;
a=n(32); b=n(20); c=n(100); d=n(16);
e=n(25); f=n(50); g=n(144); h=n(19);
a->left=b; a->right=c;
b->left=d; b->right=e;
c->left=f; c->right=g;
ins(h,a);
inorder(a);
}```
With struct node* t=r you are creating a new and independent variable t that points to the same location as r (lets call that A).
This means any changes to *r are reflected in *t as they both point to the same location A.
when assigning NULL to r, the t variable still points to location A, but r no longer does.
A small example:
int A = 0;
int *r = &A;
int *t = r;
// *r==0, *t==0, point to same location
*r = 55;
// *r==55, *t==55 (same location)
r = NULL;
// *t==55 (*r is no longer valid as r is NULL)

Printing a double pointer to a linked list in C [duplicate]

I have this code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void pointerOfPointer(struct node **reference)
{
struct node *temporary = malloc(sizeof(struct node));
temporary->data = 100;
temporary->next = 0;
printf("before: temporary->data %d\n", temporary->data);
temporary = *reference;
printf("after: temporary->data %d\n", temporary->data);
}
int main()
{
struct node *linkedlist = malloc(sizeof(struct node));
linkedlist->data = 15;
linkedlist->next = 0;
pointerOfPointer(&linkedlist);
return 0;
}
How can I access the pointer to pointer of struct in the pointerOfPointer function without copying the *reference address to the temporary local variable? So in the end I can access the reference variable data using operator -> directly, like reference->data?
Remember that foo->bar is just syntactic sugar for (*foo).bar. What you're asking for is essentially (**reference).data, which you can rewrite as (*reference)->data if you want.

Struct Pass by reference Issue [duplicate]

This question already has answers here:
How can I allocate memory and return it (via a pointer-parameter) to the calling function?
(11 answers)
Closed 7 years ago.
I am having problem while initializing the node value to passed pointer in C language,
I have written something like follow,
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void add(struct node *head, int val){
struct node *n1 = NULL;
n1 = (struct node *)malloc(sizeof(struct node ));
n1 -> data = val;
n1 -> next = NULL;
if(head == NULL){
head = n1;
printf("Head2 is initialized");
return;
}
}
int main(){
struct node *ptr = NULL;
struct node *temp;
add(ptr, 11);
printf("\nData = %d", ptr->data);
return 0;
}
Could you please tell me what is the issue in this code,
When i execute
printf("\nData = %d", ptr->data);
System shows Windows has stopped working
Thanks
short answer: if you want to change the value the pointer points to, you have to pass the pointer to the pointer:
void add(struct node **head, int val) {
...
if(*head == NULL){
*head = n1
}
int main(){
...
add(&ptr, 11)
...
}
long answer: when you call add(ptr, 11) in main you pass a memory address and a number. both memory address and a number are passed by value. as the result the changes to these variables are both local
in your add method - when you assign a value to head in head = n1 you change the value of your local variable to point to a new memory address. when your function returns the changes are gone, so main never sees the assignment and ptr remains NULL.
if you pass a pointer to ptr - &ptr, you will pass a location in memory where the value of ptr (which is memory address) resides in main, so when you call *head = n1* you write the address ofn1` the value in this location will change, and main will see it.
see also this question

Assigning a pointer the address of another pointer

I am very confused as to what the following code means. In somefunction the parameter is a pointer to a struct node. In the main the argument I am putting in is the address location of another pointer called A. So what does that exactly mean? What is the difference between A and B? Do A and B represent the same pointer? Does B now point to C after the line (*B)=C?
struct node{
int value;
};
void somefunction(Struct node *B)
{
struct node *C = (struct node *)malloc(sizeof(struct node));
(*B)=C;
};
main()
{
struct node *A;
somefunction(&A);
}
When you pass by pointer, you want changes made within a function to be visible to the caller:
struct node {
int value;
};
void foo(struct node* n) {
n->value = 7;
}
struct node n;
foo(&n);
// n.value is 7 here
and you pass an address of a pointer when you want to change the pointer itself:
void createNode(struct node** n) {
*n = malloc(sizeof(struct node));
}
struct node* nodePtr;
foo(&nodePtr);
May be this Modified and commented code will help you in understanding.
// Step-3
// Catching address so we need pointer but we are passing address of pointer so we need
// variable which can store address of pointer type variable.
// So in this case we are using struct node **
//now B contains value_in_B : 1024
void somefunction(struct node **B)
{
// Step-4
// Assuming malloc returns 6024
// assume address_of_C : 4048
// and value_in_C : 6024 //return by malloc
struct node *C = (struct node *)malloc(sizeof(struct node));
// Step-5
// now we want to store value return by malloc, in 'A' ie at address 1024.
// So we have the address of A ie 1024 stored in 'B' now using dereference we can store value 6024 at that address
(*B)=C;
};
int main()
{
// Step-1
// assume address_of_A : 1024
// and value_in_A : NULL
struct node *A = NULL;
// Step-2
// Passing 1024 ie address
somefunction(&A);
// After execution of above stepv value_in_A : 6024
return 0;
}

Struct and Pointers

So in my code I have two structures. The first is a node which contains an int value and a pointer to another node. The second structure is used to create an array of 10 pointers each point to a another node. And it also contains link2 which will be used to traverse the array and all the nodes that it points to. I'm trying to add 3 nodes each holds the value 3 into the third index of the array. The pointer in the third index should point to the first 3 then that should point to the second three and so on. When I put add(a,3) three times and then print i get segmentation fault. I tried tracing the code but that still didn't make any sense to me because I always end up with three nodes. Can someone point me in some direction? Thanks!:)/>
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int x;
struct node *link;
};
struct listofnodes
{
struct node *alist[10];
struct node *link2;
};
addFirst(struct listofnodes *a, int num)
{
struct node *nodeone = (struct node *)malloc(sizeof(struct node));
nodeone->x = num;
a->alist[num] = nodeone;
// printf("IT WENT THROUGH\n");
}
add(struct listofnodes *a, int num)
{
struct node *current;
current = a->alist[3];
struct node *nodeone = (struct node *)malloc(sizeof(struct node));
nodeone->x = num;
current->x = 5;
{
while (a->alist[3] != NULL)
{
if (a->alist[3]->link == NULL)
{
a->alist[3]->link = nodeone;
printf("IT WENT THROUGH\n");
break;
}
a->alist[3] = a->alist[3]->link;
}
}
}
main(void)
{
struct listofnodes *a =
(struct listofnodes *)malloc(sizeof(struct listofnodes));
// a->alist[3]=NULL;
addFirst(a, 3);
add(a, 3);
add(a, 3);
add(a, 5);
}
In your main() function you have:
struct listofnodes *a =
(struct listofnodes *)malloc(sizeof(struct listofnodes));
"a" is now a pointer to a listofnodes-sized block of memory containing random locations. You probably want to do something like
a->link2 = NULL;
Also, in your add() function, you have...
add(struct listofnodes *a, int num)
{
struct node *current;
current = a->alist[3];
struct node *nodeone = (struct node *)malloc(sizeof(struct node));
nodeone->x = num;
You have malloc()d random memory and put "num" into x, but you need to put a null into where 'link' goes, i.e.
nodeone->link = NULL;
When you create the memory block using malloc(), you have to manually make sure the 'link' member is set to NULL - otherwise the number left there from last use will point who knows where...

Resources