0

The value pointed by p does change inside the function but in the main procedure, it pintts out the previous value. How can I fix that?

#include <stdio.h>
#include <stdlib.h>
void changer(char *p, char user1[], char user2[]);
int main()
{
    char *p=malloc(10*sizeof(char));
    char user1[10]="rania";
    char user2[10]="souad";
    p=user1;
    changer(p,user1,user2);
    printf("%s\n",p);
      return 0;
}
void changer(char *p, char user1[], char user2[])
    {
        if (p==user1){
           p=user2;
           }

        else
            p=user1;
    }
Elas
  • 21
  • 2
  • You need to research about "Pass by value vs Pass by reference in C". You only change a copy of your pointer – Gerhardh Feb 06 '23 at 14:48
  • Apart from this being a common FAQ, you can't do stuff like `p=user1;`, that doesn't copy any data. You need to study how pointers work before anything else. – Lundin Feb 06 '23 at 14:50

0 Answers0