Reputation: 151
I have written a program in which a function takes two pointers as arguements and that function changes the address of pointer ,so when i call the value of pointers in the main function it is displaying the same old value.
Code:-
#include <stdio.h>
void sample(int *d, int *m);
int main(){
int day=12,month=3;
int *d,*m;
d = &day;
m = &month;
printf("Day = %d \t MOnth = %d\n",*d,*m);
sample(d,m);
printf("Day = %d \t Month = %d\n",*d,*m);
}
void sample(int *d, int *m){
int month = 1;
int day = 30;
m = &month;
d = &day;
}
Based on answers to the same question on stackoverflow i even called the functions using sample(&d,&m)
but that too did'nt work.
I even tried defining the function like :-
void sample(int *d,int *m){
int month = 1;
int day = 30;
*m = month;
*d = day;
}
But the program crashed.
Upvotes: 0
Views: 143
Reputation: 5001
When you want to pass any variable to a function, change it in this function and return its new value, you need to pass a reference to it (passing-by-reference), not the variable itself (passing-by-value).
Specifically in your case, if you want to change the addresses of d
and m
you need to pass references to the addresses you want to change. This means that your function should be modified like this :
void sample(int *d,int *m){
*m = 1;
*d = 30;
}
and accordingly modify the body of your main
function like this :
int day=12, month=3;
sample(&day, &month);
However, if you insist in your way, the following modifications should be made :
void sample(int **d, int **m){
int month = 1;
int day = 30;
*m = &month;
*d = &day;
}
and accordingly you should change the call of the function like this :
int day=12, month=3;
int *d, *m;
d = &day;
m = &month;
sample(d,m);
Upvotes: 1
Reputation: 151
Changing the calling of the function solved the problem
sample(&day,&month);
New Code :-
#include<stdio.h>
void sample(int *d,int *m);
int main(){
int day=12,month=3;
int *d,*m;
d = &day;
m = &month;
printf("Day = %d \t MOnth = %d\n",*d,*m);
sample(&day,&month);
printf("Day = %d \t Month = %d\n",*d,*m);
}
void sample(int *d,int *m){
int month = 1;
int day = 30;
*m = month;
*d = day;
}
Upvotes: 0
Reputation: 19864
In sample()
API you are assigning some new values to your pointers and these values are valid only within the sample API, the purpose you are passing a pointer is to retain the location where the value needs to be changed.So you shouldn't change the locaion where your pointer is pointing you just have to change the value in the location as shown below.
void sample(int *d,int *m)
{
int month=1,day = 30;
*m = month;
*d = day;
}
Upvotes: 0
Reputation: 39370
Let's see what your function is doing:
void sample(int *d,int *m){ // take a temporary pointer arg
int month = 1; // create a temporary variable
m = &month; // assign the address of the temporary
// to the temporary pointer
}
a function takes two pointers as arguements and that function changes the address of pointer
If you really wanted that, then:
Your added example:
void sample(int *d,int *m) {
int month = 1;
int day = 30;
*m = month;
*d = day;
}
Is ok; as in, it does something completely different from the first one, but at least has a chance of working. The only way it could crash is if the values passed to it were bad; so the problem is in the caller.
Upvotes: 0