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 6004927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:54+00:00 2026-05-23T01:19:54+00:00

the function work ok while i checked it with debuging but after the function

  • 0

the function work ok while i checked it with debuging but after the function finish the array is empty

this is the structs:

typedef struct coordinates  
{
int x_l;
int y_l;
int x_r;
int y_r;
} Coordinates;

typedef struct field 
{
int Id;
Coordinates location;
int area;
int price;
} Field;

this is the function:

    int ReadFromFile(Field *pArr)
{
    int size;
    int i;


 FILE *f = fopen("c:\\migrashim\\migrashim.txt", "r");

 if (f == NULL)
    {
        printf("error open file");
        exit (1);
    }
fseek(f,0,SEEK_SET);    
fscanf(f, "%d\n", &size);

pArr=(Field*)realloc(pArr,sizeof(Field)*(size));


for (i=0 ; i<size ; i++)
{
    fscanf(f, "%d\n", &pArr[i].Id);
    fscanf(f, "%d %d\n", &pArr[i].location.x_l,&pArr[i].location.y_l);
    fscanf(f, "%d %d\n", &pArr[i].location.x_r,&pArr[i].location.y_r);
}

    return size;
    }

and this is the calling from the main:

 {
    counter_2=ReadFromFile(pFieldArr);
    printf("\nFile loaded successfully!");
        New_Field_flag=0;           
        Exit=0;
        break;
 }

tnx guys…

  • 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-23T01:19:54+00:00Added an answer on May 23, 2026 at 1:19 am

    You’re passing a pointer into ReadFromFile and resizing.

    That realloc will try to resize in-place, but if it can’t, it’ll allocate a new block, copy the existing data, and then free the old block.

    Your function doesn’t take into account that the block may have moved (you’re not returning the pointer which returned by realloc).

    EDIT:

    The code should look something like this:

    int ReadFromFile(Field **pArr)
    {
        int size;
        int i;
    
        FILE *f = fopen("c:\\migrashim\\migrashim.txt", "r");
    
        if (f == NULL)
        {
            printf("error open file");
            exit (1);
        }
    
        fscanf(f, "%d\n", &size);
    
        *pArr = (Field*)realloc(*pArr, sizeof(Field)* size);
    
        for (i = 0; i < size; i++)
        {
            fscanf(f, "%d\n", &(*pArr)[i].Id);
            fscanf(f, "%d %d\n", &(*pArr)[i].location.x_l, &(*pArr)[i].location.y_l);
            fscanf(f, "%d %d\n", &(*pArr)[i].location.x_r, &(*pArr)[i].location.y_r);
        }
    
        fclose(f);
    
        return size;
    }
    

    and then:

    {
        counter_2 = ReadFromFile(&pFieldArr);
        printf("\nFile loaded successfully!");
        New_Field_flag = 0;
        Exit = 0;
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How come this doesn't work (operating on an empty select list <select id=requestTypes></select> $(function()
In the code below, why does the open function work but the close function
In Guile 1.6.*, the function scm_istring2number(char *str,int strlen,int radix) does the work. However, this
I can't get nl2br function to work after fetching data from my database: $result
I've done some searches and I think this should work but I guess I'm
Does SQL Server's (2000) Soundex function work on Asian character sets ? I used
Does anyone know how function indexes work in databases?
How come the following doesn't work? CREATE FUNCTION Test (@top integer) RETURNS TABLE AS
What would be better practice when giving a function the original variable to work
What's the best way to get a function like the following to work: def

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.