-2

I am new in learning c language and I am trying to learn about functions with char arrays. In this code I want to wrtie a function which uses a char array as a parameter and give another char array as a result. When I run the code the output should be : Helloooo world!

However, when I run the code the program crashes. How can I fix the problem? Am I using the right type of variables?

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

char *write();

int main()
{
    char x[10] = "ooo";
    printf("%s, world!\n", *write(x));
    return 0;
}

char *write(char x[10])
{
    char str[10];
    strcpy(str, "Hello");
    strcat(str,x);
    x = str;
    return x;
}
user6210457
  • 397
  • 1
  • 7
  • 22

1 Answers1

2

You have two issues:

  1. str is a local variable in the write function scope. So you return the pointer to something which does not exist any more.
  2. Str is too short to accommodate your data. You copy "hello" (5 chars) + possible 10 chars from the function parameters.

char *write();

int main()
{
    char x[10] = "ooo";
    char buff[20];
    printf("%s, world!\n", write(buff, x, 20));
    return 0;
}

char *write(char *buff, char *s2, int maxsize)
{
    strcpy(buff, "Hello");
    if(strlen(buff) + strlen(s2) < maxsize)
        strcat(buff,s2);
      else 
        strcpy(buff,"Error");
    return buff;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    `*write(...)` is wrong, it should just be `write(...)` – Barmar Jul 06 '17 at 21:50
  • Instead of hard-coding `20`, better to use `sizeof(buff)` – Barmar Jul 06 '17 at 21:50
  • @Barmar - yes I have not noticed this * in the original code which was just copied. – 0___________ Jul 06 '17 at 21:54
  • @Barmar unfortunately you cant use sizeof buff inside the function write as it will show you the size of the pointer itself. If you mean on the function call yes - but i did intentionally to show what i mean (would say for the educational purposes) – 0___________ Jul 06 '17 at 21:55
  • I mean: `write(buff, x, sizeof(buff))` – Barmar Jul 06 '17 at 21:57
  • And don't forget to subtract 1 for the trailing null. – Barmar Jul 06 '17 at 21:57
  • Yes but that guy is a very beginner and I decided to show this 20 as a distinctive number - easy to spot and understand – 0___________ Jul 06 '17 at 21:58
  • 1
    @Barmar you do not need it because ther is < not <= so the string length can br max 19 + 1 for '\0' and the buff length is 20 – 0___________ Jul 06 '17 at 21:59
  • OK, missed that. Regarding the 20, maybe define a macro and use it in both places. `#define BUFSIZE 20`. That makes it clear and is also good practice. – Barmar Jul 06 '17 at 22:02
  • I think that will be less understandable for the beginners. And I want to show that sizeof not always is a good sollution eg if _char *x = malloc(SIZE);_ the sizeof will return only size of the char *x pointer. Most beginners are lost in this. If you have noticed he has not used any defines in his code so maybe it is to early to do it – 0___________ Jul 06 '17 at 22:07