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

  • Home
  • SEARCH
  • 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 5939723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:52:05+00:00 2026-05-22T15:52:05+00:00

Below is a function from a program: //read the specified file and check for

  • 0

Below is a function from a program:

//read the specified file and check for the input ssn
int readfile(FILE *fptr, PERSON **rptr){
    int v=0, i, j;
    char n2[MAXS+1], b[1]=" ";

    for(i=0; i<MAXR; i++){
        j=i;
        if(fscanf(fptr, "%c\n%d\n%19s %19s\n%d\n%19s\n%d\n%19s\n%19s\n%d\n%d\n%19s\n\n",
                    &rptr[j]->gender, &rptr[j]->ssn, rptr[j]->name, n2, &rptr[j]->age, 
                    rptr[j]->job, &rptr[j]->income, rptr[j]->major, rptr[j]->minor,
                    &rptr[j]->height, &rptr[j]->weight, rptr[j]->religion)==EOF) {
            i=MAXR;
        }
        strcat(rptr[j]->name, b);
        //strcat(rptr[j]->name, n2);
        if(&rptr[MAXR]->ssn==&rptr[j]->ssn)
            v=j;
    }
    return v;
}

the commented line is like that because for some reason the array ‘b’ contains the string ‘n2’ despite an obvious lack of an assignment. This occurs before the first strcat call, but after/during the fscanf call.

it does accomplish the desired goal, but why is n2 concatenated onto the end of b, especially when b only has reserved space for 1 array element?

Here is a snippet of variable definitions after the fscanf call:

*rptr[j]->name = "Rob"

b = " Low"

n2= "Low"
  • 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-22T15:52:06+00:00Added an answer on May 22, 2026 at 3:52 pm

    It works, because you got lucky. b and n2 happened to be next to each other in memory, in the right order. C doesn’t do boundary checking on arrays and will quite happily let you overflow them. So you can declare an array like this:

    char someArray[1] = "lots and lots of characters";
    

    The C compiler (certainly old ones) is going to think this is fine, even though there clearly isn’t enough space in the someArray to store that many characters. I’m not sure if it’s defined what it’ll do in this situation (I suspect not), but on my compiler it limits the population to the size of the array, so it doesn’t overflow the boundary (someArray=={'l'}).

    Your situation is the same (although less extreme). char b[1] is creating an array with enough room to store 1 byte. You’re putting a space in that byte, so there’s no room for the null terminator. strcat, keeps copying memory until it gets to a null terminator, consequently it’ll keep going until it finds one, even if it’s not until the end of the next string (which is what’s happening in your case).

    If you had been using a C++ compiler, it would have thrown at least a warning (or more likely an error) to tell you that you were trying to put too many items into the array.

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

Sidebar

Related Questions

The function below takes a python file handle, reads in packed binary data from
I'm testing the VB function below that I got from a Google search. I
The user should input some file names in the command line and the program
I typically use the below function to return the root URL if I ever
How do I make the below function more dynamic for example commentLink and commentContainer
I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
The function below prints a color raster image to a PCL-5 printer. The function
The function below is logging the 0, z and the 1 ok... but its
Below is my current char* to hex string function. I wrote it as an
My function is pretty much a standard search function... I've included it below. In

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.