0
int _tmain(int argc, _TCHAR* argv[])
{
char string1[20], append_string[40];
char *string2 = "APPENDED"; /* string to be appended */

/* prompt for string 1 */
printf("Enter string1: ");
scanf_s("%s", &string1);
while (!strcmp(string1, "done") == 0)
{
    printf("Before append. \nString1: %s \nString2: %s\n\n", string1, string2);
    mystrappend(string1, string2, append_string);
    printf("After append. \nString1: %s \nString2: %s \nAppended string: %s\n\n", string1,            string2, append_string);

    // prompt for string 1 
    printf("Enter string1: ");
    scanf_s("%s", &string1);
   }

 return 0;
 }

Why doesn't String1 store anything after input?
I have the header files of stdio.h, conio.h, string.h and stdafx.h included.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 2
    And how is `mystrappend` implemented? – M Oehm Nov 16 '14 at 15:10
  • Atm, i'm still working on it, but even if its empty shouldnt String1 have something inside of it when I input something? –  Nov 16 '14 at 15:13
  • 4
    I see. I think your `scanf_s` call shouldn't take the address of the array, just `scanf_s("%s", string1)`. – M Oehm Nov 16 '14 at 15:13
  • Still prints out nothing for String1..? –  Nov 16 '14 at 15:17
  • 4
    I've checked on the [`scanf_s` function](http://msdn.microsoft.com/de-de/library/w40768et.aspx). It requires a buffer size after string arguments, so it should probably be `scanf_s("%s", string1, sizeof(string1));`. Or use plain old `scanf`. These functions return a value, which you should check. – M Oehm Nov 16 '14 at 15:22
  • Ive had to change some properties inside Microsoft Visio, thanks for your information, got it working –  Nov 16 '14 at 15:29
  • @MOehm but isnt `&OfArray == OfArray` for array decay? – dhein Apr 13 '15 at 14:57
  • @Zaibis: Their "raw" values are the same, but they have different types: [See here.](http://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c) or in Cool Guy's answer below. – M Oehm Apr 13 '15 at 16:32

1 Answers1

0

Quoting from the documentation of scanf_s,

Remarks:

[...]

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

So, the scanf_s

scanf_s("%s", &string1);

is wrong because of two reasons:

  1. %s expects a char*, but you give the argument &string1 which is of type char(*)[20]. This leads to Undefined Behavior.
  2. As seen in the quote above, you need to pass a third argument denoting the size of the buffer.

Fix problem #1 by using string1 instead of &string1. string1 will "decay" into a pointer to its first element(char*).

Fix problem #2 by using a third argument denoting the size of the buffer using sizeof or _countof.

After fixing these problems, your scanf_s should look like

scanf_s("%s", string1, sizeof(string1));

or

scanf_s("%s", string1, _countof(string1));
Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83