-3
 char* xpx(char* src) 
 {  
      char result[sizeof(src)];  

      strcpy(result,src);  

      return result;

 }

There are 2 bugs in the above code. 1) strcpy is passing the src as parameters but it is not legal as str is a pointer.

I could not able to find the another one. Could you help me?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Smith
  • 47
  • 5
  • But `strcpy` *does* take a pointer. – Oliver Charlesworth May 07 '18 at 21:28
  • 3
    You missed both bugs. Passing `src` as the second parameter to `strcpy` is perfectly fine. – user3386109 May 07 '18 at 21:28
  • @user3386109 Can you please correct me then? – Smith May 07 '18 at 21:29
  • 1
    How many bytes is sizeof(src)? – StaticBeagle May 07 '18 at 21:30
  • 4
    I think it's your homework, and you need to do it. Or talk to one of the teacher's assistants to get some help. Or hire a tutor. – user3386109 May 07 '18 at 21:30
  • Tip: run this function on paper, using square-ruled paper (the type you see in mathematics schoolwork exercise books) to illustrate the state of the stack. Consider what happens to the *values* of `result` when the function returns. – Dai May 07 '18 at 21:33
  • @user3386109 it is not the homework. It is from the sample exam which I am trying to solve it. It is fine if you can give me some hint. – Smith May 07 '18 at 21:33
  • My hint is that the other comments have already mentioned both bugs. – user3386109 May 07 '18 at 21:35
  • 4
    Specifically, please see [finding length of array inside a function](https://stackoverflow.com/questions/17590226/c-finding-length-of-array-inside-a-function), and see [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c) – Weather Vane May 07 '18 at 21:41
  • How do you tell the size of a string? What happens when you return local memory? Pray that `src` is actually a string so that using extremely unsafe `strcpy()` won't buffer overflow. `src` is unmodified, perhaps we should tell the compiler about this. Hell, look at warnings. – Michael Dorgan May 07 '18 at 21:52
  • 1
    main bug is that it returns the address of a local variable. second bug is sizeof(src) - that gives you the size of a pointer on your platform, not useful – pm100 May 07 '18 at 22:30

2 Answers2

0

not legal as str is a pointer

Not sure what you mean by that, but passing pointers to functions is perfectly fine.

As for "bugs" in the function, it is hard to say if there are any, given that you have not provided what is the expected behavior of the function. The function is legal C, although quite useless and dangerous to use (and will trigger warnings from common compilers):

  1. Do not return addresses to local variables, since they do not exist anymore after your return, so you shall not access them.

  2. If you are passing a null-terminated string into a function and you need to get a buffer of its size; then you have to find out its length at runtime using something like strlen() and then allocate the memory on the heap with malloc() (or using VLAs on C99 and later).

Acorn
  • 24,970
  • 5
  • 40
  • 69
0

Look at strcpy docs:

char *strcpy( char *restrict dest, const char *restrict src );

Copies the null-terminated byte string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest. The behavior is undefined if the dest array is not large enough. [...] (important but not relevant to the question so omitted part)

So strcpy does take 2 pointers. That's fine. It's not a bug.

To find the bug pay attention to (and think about it, it's logical): dest array must be large enough. What does "large enough" mean here? Well since the functions copies the string from src, including null terminator to dest it means dest must be at least the length of the string in src + 1 for the null terminator. That means strlen(src) + 1.

sizeof(src) is the same as sizeof(int*) which is the size of a char pointer on the platform. The size of the pointer. Not what you want.

The next error is that the functions returns the address of an automatic storage object, aka the result array. This means that the array will cease to exist when the function exits and thus the function returns a pointer to an object that is no longer valid. A solution to this would be to use malloc to allocate the array. Another is to change the signature of xpx to something similar to strcpy where the destination array is supplied.

So summing them you need something along this:

char* result = malloc(strlen(src) + 1);

Another bug (yes, technically it's not a bug, but semantically it's a bug in my opinion) is that src should be of const char* type.

Another source of potential problems and bugs is if the input is not well-behaved, e.g. if src is not null-terminated, but it is debatable how much responsibility this function should carry regarding this.

bolov
  • 72,283
  • 15
  • 145
  • 224