0

How would I do this, as I dont know if I can read a void pointer so I want to convert it to a string first. Here is my attempt while testing it:

void* test1;
char test2[] = "ha 21";
char test3[50];

test1 = test2;

test3 = test1;

printf("%s", test3);

return 0;

When I try to make test1 = test 2 is probably wrong as well but that is just to show what should be in the void pointer. I am only trying to see how I can convert a void pointer containing a string to a string.

H.S.
  • 11,654
  • 2
  • 15
  • 32
Will A
  • 3
  • 1
  • 2

2 Answers2

1

Since test3 is an array type it can not be assigned. In C you can convert every pointer to void * and back (it is casted implicitly).

So a possible solution is to declare test3 as pointer:

void *test1;
char test2[] = "ha 21";
char *test3;

test1 = test2;
test3 = test1;

printf("%s", test3);

Or if you want to copy the memory (as mentioned in comments):

void *test1;
char test2[] = "ha 21";
char test3[50];

test1 = test2;
strcpy(test3, test1);

printf("%s", test3);
Osiris
  • 2,783
  • 9
  • 17
  • Thanks a bunch, that fixed it – Will A Dec 08 '18 at 00:02
  • 1
    No clue - and sometimes you just never know unless the person has the integrity to leave a comment explaining the DV (few and far between on SO). There isn't anything wrong here (you can always add validations, etc.., but it is abundantly clear from the example what is pointed to by `test1` will fit in `test3` in the 2nd example without more). So don't worry to much about an unexplained, inadvertent DV -- they all come out in the wash. (that doesn't stop them from being frustrating, but it cost the DV'er 1-pt to cast it so it's not for free -- which they used to be...) – David C. Rankin Dec 08 '18 at 00:14
  • @WillA - after a bit of time has passed for any additional answers, please take a look at: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – David C. Rankin Dec 08 '18 at 00:15
  • @DavidC.Rankin Thank you for the kind explanation. I was just worried that I indeed may gave false information. – Osiris Dec 08 '18 at 00:19
  • I didn't downvote you, but I can't think of a valid reason ever to use `strcpy()`. It's not quite as risky as, say, `gets()`, but it's still pretty much asking for a buffer overflow. Even in trivial cases, like here, where you know that won't happen, there's still no reason not to use `strncpy()` – torstenvl Dec 08 '18 at 00:19
  • 4
    There is absolutely nothing wrong with `strcpy` - so long as you provide a *nul-terminated* string and insure the destination has sufficient space to hold the source. There is no zeroing of additional space (as there is with `strncpy` so there is no performance penalty for large buffers). It has no relation whatsoever to the problems posed by `gets`. – David C. Rankin Dec 08 '18 at 00:21
  • @torstenvl Yes the second example could lead to a buffer overflow if the size of `test3` is less than `test2`. But a plain `strncpy` is also considered unsafe since it probably does not add a nul-terminator, see [here](https://stackoverflow.com/questions/869883/why-is-strncpy-insecure). – Osiris Dec 08 '18 at 00:22
  • 3
    @torstenvl: No, `strncpy` is (almost certainly) not the answer. First, it's not really a string function, in spite of the deceptive name. Second, it silently truncates the data if the target array isn't big enough, and that's rarely the right response (think about truncating `"rm -rf $HOME/tmpdir"` to `"rm -rf $HOME"`. More here: https://the-flat-trantor-society.blogspot.com/2012/03/no-strncpy-is-not-safer-strcpy.html – Keith Thompson Dec 08 '18 at 00:27
  • @torstenvl: `strcpy` is perfectly safe if you know that the target is big enough. (That's the problem with `gets`, you can't know how big the input is.) – Keith Thompson Dec 08 '18 at 00:28
0

When I try to make test1 = test2 is probably wrong as well but that is just to show what should be in the void pointer.

test1 = test2 is perfectly right. From C Standard#6.3.2.3p1

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

The problem is with this statement:

test3 = test1;

test3 is an array and you cannot assign test1 to test3 because, in C, array names are non modifiable lvalues.

Instead, you should copy the contents of test1 to test3 and you can use strcpy() for this.

But strcpy() is not safe because it has no way of knowing how large the destination buffer is and you may end up with buffer overflow if the destination buffer is not long enough to hold the source string (including null terminating character). Before copying string if you can ensure that the destination buffer is long enough to hold source string (including null terminating character) than its perfectly fine to use strcpy() to copy source string to destination buffer. In your case since the destination buffer size is 50 which is long enough to hold the source string "ha 21" you can safely use the strcpy(). So, instead of this:

test3 = test1;

You should do:

strcpy(test3, test1);

Additional:

The strncpy() is not a safer version of strcpy().
Read here about why strncpy() is introduced?

However, you can use strncpy() as an alternative (which most of the developers do) because it can prevents from buffer overflow but remember no null-character is implicitly appended at the end of destination if source is longer than the maximum number of characters to be copied from source. You need to take care of this scenario explicitly where the source is longer than destination.

H.S.
  • 11,654
  • 2
  • 15
  • 32