2

this is my main:

int main(void)
{
    char w1[] = "Paris";
    ChangeTheWord(w1);
    printf("The new word is: %s",w1);
    return0;
}

and i need to change the value of w1[] in this function:

ChangeTheWord(char *Str)
{

     ...

}
benhi
  • 574
  • 1
  • 6
  • 24

5 Answers5

10

All answers so far are correct, but IMO incomplete.

When dealing with strings in C, it is important to avoid buffer overflows.

Your program crashes (or at least shows undefined behaviour) if ChangeTheWord() tries to change the word to a too long one.

Better do this:

#include <stdio.h>
#include <stddef.h>

void ChangeTheWord(char *str, size_t maxlen)
{
    strncpy(str, "A too long word", maxlen-1);
    str[maxlen] = '\0';
}

int main(void)
{
    char w1[] = "Paris";
    ChangeTheWord(w1, sizeof w1);
    printf("The new word is: %s",w1);
    return 0;
}

With this solution, the function is told which size of memory it is allowed to access.

Be aware that strncpy() doesn't work as one would suspect at the first glance: if the string is too long, no NUL-byte is written. So you have to take care by yourself.

glglgl
  • 89,107
  • 13
  • 149
  • 217
4
int main()
{
    char w1[]="Paris";
    changeWord(w1);      // this means address of w1[0] i.e &w[0]
    printf("The new word is %s",w1);
    return 0;

}
void changeWord(char *str) 
{
    str[0]='D';         //here str have same address as w1 so whatever you did with str will be refected in main(). 
    str[1]='e';
    str[2]='l';
    str[3]='h';
    str[4]='i';
}

Read this answer too

Community
  • 1
  • 1
A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32
2

You can simply access each index and replace with desired value.. Made one change for example...

void ChangeTheWord(char *w1)
{
     w1[0] = 'w';
     //....... Other code as required
}

Now when you try to print the string in main() Output will be Waris.

HadeS
  • 2,020
  • 19
  • 36
  • NO the function is "void ChangeTheWord(char *Str)" – benhi Mar 06 '14 at 09:28
  • @benhi yes `ChangeTheWord` doesn't returns but changes your array in place. You are passing address of your array `w1` to your function. – Grijesh Chauhan Mar 06 '14 at 09:30
  • writing `void` in `C` is redundant ... I guess I have this habit makes things more clear to me... also name of parameter doesn't matter... – HadeS Mar 06 '14 at 09:32
  • @HadeS "writing `void` in `C` is redundant" In what way is it redundant? – glglgl Mar 06 '14 at 09:37
  • @benhi From the outside view, it doesn't matter if you have `w1` or `Str` as your parameter name. – glglgl Mar 06 '14 at 09:38
  • @glglgl `If a function is defined as having a return type of void, it should not return a value.` .. I think I am influenced by this statement ..correct me if I am wrong... :-) – HadeS Mar 06 '14 at 09:43
2

This is how you can do it.

ChangeTheWord(char *Str)
{
        // changes the first character with 'x'
        *str = 'x';

}
rakib_
  • 136,911
  • 4
  • 20
  • 26
1

you can actually change the value of each index with the pointer notation in a loop. Something like...

int length = strlen(str);              // should give length of the array

for (i = 0; i < length; i++)
    *(str + i) = something;

or you should be able to just hardcode the index

   *(str + 0) = 'x';
   *(str + 1) = 'y';

or use array notation

str[0] = 'x';
str[1] = 'y';
SomeTonyGuy
  • 91
  • 1
  • 6