1

Im just starting to learn pointers and I'm trying to figure out why my code doesn't work. I get no compilation error yet the code doesn't what I want it to do. Why am I not passing the adress of an array? If I try to do so I get a compiilation error :(

#include <stdio.h>

void switch_name(char* name)

{
     name= "testv2";
}

void main()
{
char *name1 = "test_name"; 
printf("%s\n", name1);
switch_name(name1);
printf("%s\n", name1);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Jan Chabik
  • 37
  • 2
  • recommended read: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Aditi Rawat Dec 26 '17 at 19:21
  • " why my code doesn't work' --> Code does work. "yet the code doesn't what I want it to do" --> What did you want code to do? – chux - Reinstate Monica Dec 26 '17 at 19:34

2 Answers2

1

Well because C is pass by value. You make changes to a local variable(name in function swicth_name()). To retain changes pass the address of the char* and assign the address of the string literal to the char* directly (By dereferencing the char**). For example this would work

#include <stdio.h>

void switch_name(char** name)
{
     *name= "testv2";
}

int main(void)
{
   char *name1 = "test_name"; 
   printf("%s\n", name1);
   switch_name(&name1);
   printf("%s\n", name1);
   return 0;
}

Here the function swicth_name got the address of the char* name1. Now when you dereference it using unary * in switch_name you assign the address of the string literal to the name variable of main(). That' swhy the change retains.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Function parameters are its local variables.

You can imagine the function call and its definition the following way

char *name1 = "test_name"; 
switch_name(name1);

//...

void switch_name( /* char* name */ )
{
     char *name = name1;
     name= "testv2";
}

As you can see the original variable name1 was not changed in the function. It is the variable name that is the parameter that was changed in the function.

You have to pass the original variable by reference if you are going to change it in the function.

For example

#include <stdio.h>

void switch_name( char ** name)
{
    *name= "testv2";
}

int main( void )
{
    char *name1 = "test_name"; 

    printf("%s\n", name1);

    switch_name( &name1 );

    printf("%s\n", name1);
}

Compare the above program with the following program

#include <stdio.h>
#include <string.h>

void switch_name( char* name )
{
    strcpy( name, "testv2" );
}

int main(void) 
{
    char s[] = "test_name"; 
    char *name1 = s;

    printf( "%s\n", name1 );

    switch_name( name1 );

    printf( "%s\n", name1 );

    return 0;
}

In the first program you are going to reassign the original pointer itself using a function.

In the second program it is the data pointed to by the pointer that are reassigned using the standard C function strcpy.

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335