0

Let me make this clear right away, this is for a college class. I cannot use C++ libraries, only standard C libraries. Do not suggest that I use C++ strings or cin/cout because that will not help me for this assignment.

My issue: I have global character arrays in the main function. I need to pass strings to the global character arrays from scanf() in a function foo(). Everything compiles fine, the issue is, the scanf() function seems to have no affect on the global character arrays that it points to. I'm using the "address of" operator (&) as the reference books indicate to do. Perhaps, I'm not understanding the relationship between the character array pointer and the scanf() "address of" (&). I feel I've looked everywhere for a solution.

I've spent several hours on this issue so I'm now looking for expert advice.

Here is a simplified version of my program.

#include <stdio.h>

void foo(char arr1[], char arr2[]);

int main(void)
{
    char arr1[20] = "initial";
    char arr2[25] = "second";

    foo(arr1); // <------- should change it to the string "second" upon scanf()

    printf("Test Return Value: %s\n",arr1); // <---- returns "initial" (the problem)

    printf("Enter a test value: ");
    scanf("%s", &arr1);

    printf("Test Return Value: %s\n",&arr1);

// ---------------------- this code is not part of the issue
fflush(stdin);
getchar();
return 0;
// ----------------------
}
void foo(char arr1[], char arr2[])
{
    // there will be many returned values

    printf("Enter a test value: ");
    scanf("%s", &arr1); // <---------- the problem function (input < 20 chars)
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
SacWebDeveloper
  • 2,563
  • 2
  • 18
  • 15
  • You made many mistakes when copying your code: `arr2` is never used and `arr` is not declared. – Simon Oct 20 '11 at 04:19

3 Answers3

2
scanf("%s", &arr); // <---------- the problem function (input < 20 chars)

should be

scanf("%s", arr); // <---------- the problem function (input < 20 chars)

The perils of using the C io functions!

Ayjay
  • 3,413
  • 15
  • 20
  • Sorry about the arr without the 1, I thought I fixed that before posting. Thank you. I can't believe it was that easy. – SacWebDeveloper Oct 20 '11 at 05:20
  • With IO, it's generally incredibly easy to make silly mistakes. That's why we have the stream libraries and really recommend against using strcpy and the like. Good coders are lazy coders - make it easy for yourself! – Ayjay Oct 20 '11 at 05:22
  • Ha, 8 years later, I'm accepting this answer because I should have back then. Thanks for the help way back when. – SacWebDeveloper Dec 10 '19 at 22:20
2

Although you have updated saying solved I have a few observation which you might want to consider:
1. Get rid of & before arr1 in scanf & printf calls (which has solved your problem as mentioned by Ayjay & Dennis)
2. Correct number of parameters not passed to function foo (as mentioned by Adrian Cornish). Thus code will not compile.
3. fflush(stdin); is undefined behavior. fflush is only for output streams. Please don't use it with stdin. Refer this SO question for details.
4. If this is a C++ source please use #include <cstdio> instead of #include <stdio.h>
Always compile code with full compiler warnings and resolve all of them. It is a good practice. :)
Hope this helps!

Community
  • 1
  • 1
another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
0

The correct syntax for the scanf function is:

scanf("%s", arr);

You only need the & operator for simple variables, not for arrays / pointers.

Besides of that, you will have to correct the improper use of arr1, arr2 and arr. Parts of your code make use of the first two arrays, others of the latter.

Dennis
  • 14,264
  • 2
  • 48
  • 57