Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:06:50+00:00 2026-05-26T10:06:50+00:00

Let me make this clear right away, this is for a college class. I

  • 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)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:06:50+00:00Added an answer on May 26, 2026 at 10:06 am
    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's make it immediately clear: this is not a question about memory leak! I
Let me make this clear, I am NOT using a cms nor a blogging
Before I get anywhere with this post, let me make this clear, there is
Let me make this disclaimier : I have clear understanding of virtual function call
Let me make this clear, I have this enum: enum Token { Number(v:Float); Identifier(v:String);
Let's make this very easy. What I want: @array = qw/one two one/; my
Ok, let's see if I can make this make sense. I have a program
Let's say I have this string var: string strData = 1|2|3|4||a|b|c|d Then, I make
The title is not clear so let me explain: In my apps, I have
I am going to try to make this question as clear as I can.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.