0

I'm trying to understand why the value of each array location of a string cannot be changed when i input the string pointer to a function

i tried to use instead of [] to *

#include <stdio.h>
#include <string.h>
void func(char * p)
{
int i;
char letter;
for(i = 0 ; i < strlen(p) / 2; i++)
{
    letter = p[strlen(p)-i-1];
    p[strlen(p)-1-i] = p[i];
    p[i] = letter;
}
puts(p);

int main()
{
char * p = "dudu";
func(p);
return 0;
}

an example of what im tring to do can be john and than output of the function nhoj like changing sides of the characters of the string

changing values of the beggening with the end of the string

  • 3
    `p` is pointing to a *string literal* which is not supposed to be mutable. Attempting to write it will result in *undefined behavior*. Now to find one of that zillion of duplicates... – Eugene Sh. May 17 '19 at 14:15
  • 2
    Use: `char p[] = "dudu";` – Paul Ogilvie May 17 '19 at 14:20
  • 1
    And _don't_ call a function repeatedly when the result won't change. Use a variable to hold the result once. I mean `strlen(p)`. – Paul Ogilvie May 17 '19 at 14:21
  • By "i tried to use instead of [] to *", do you mean you know it's ok for `char []` and why isn't it ok for `char *`?` – doctorlove May 17 '19 at 14:27

2 Answers2

1

String literals are "baked" into your exe, often somewhere read only. (See this question).

When you say

char * p = "dudu";

you point there - so cannot change this. (Or at least shouldn't try to).

If you say

char p [] = "dudu";

things change. Your array of chars is now contains the string literal, on your stack so you can change individual chars.

You could also allocate a char * with malloc, and memcpy a string literal in there and change it.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

In this declaration

char * p = "dudu";

the pointer p is initialized by the address of the first character of the string literal "dudu".

Inside the function func you are trying to change the string literal using the pointer.

String literals are immutable in C (and C++). Any attempt to change a string literal results in undefined behaviour.

To make the code valid you need to use a character array as for example

char s[] = "dudu";

Another way is to allocate an array dynamically and store a string in it as for example

const char *p = "dudu";
char *s = malloc( strlen( p ) + 1 );
strcpy( s, p ); 

Take into account that it is much better to rewrite the function func the following way

char * func( char *s )
{
    for ( size_t i = 0, n = strlen( s ); i < n / 2; i++ )
    {
        char c = s[n - i - 1];
        s[n - i - 1] = s[i];
        s[i] = c;
    }

    return s; 
}

and call it like

puts( func( s ) );

Here is a demonstrative program

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

char * func( char *s )
{
    for ( size_t i = 0, n = strlen( s ); i < n / 2; i++ )
    {
        char c = s[n - i - 1];
        s[n - i - 1] = s[i];
        s[i] = c;
    }

    return s; 
}

int main( void )
{
    char s[] = "Hello JustAskingSmartPeople";

    puts( s );
    puts( func( s ) );
}

The program output is

Hello JustAskingSmartPeople
elpoePtramSgniksAtsuJ olleH
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335