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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:53:17+00:00 2026-05-22T17:53:17+00:00

Hi I am slowly getting my head around pointers and indirection but am still

  • 0

Hi
I am slowly getting my head around pointers and indirection but am still having a little trouble.

In my main function i am creating an array of structs (computers) using malloc

int arraySize = 0;//This may be the first issue
Computer    *ptrComputer = NULL; 
ptrComputer = (Computer*) malloc(sizeof(Computer) * arraySize);

I am then passing the pointer to a function that reads data out of a file and into the array

arraySize = readFileToArray(&ptrComputer, arraySize);

int readFileToArray(Computer **compArray, int arraySize){

Computer newComp;
int foundARecord = 0;
/*File stuff*/

   arraySize = extendArrays(compArray, arraySize, no_elements);

    /*Use fscanf to read file data into the newComputer variable*/
    printf("g %i\n", arraySize);
    *compArray[arraySize - 1] = newComp;//set the newly created part of the array to newComputer


return arraySize;
}

int extendArrays(Computer **compArray, int arraySize){
   arraySize++;
   //Resize the computer array
   *compArray = (Computer*)realloc(*compArray, (sizeof(Computer)*(arraySize + 1)));
   return arraySize;
}

Now as far as i understand, i am passing the address of ptrComputer to the readFileToArray() function. It is then passing that same address to the extendArrays() function which resizes it. I am then trying to assign newComp to the location in memory that compArray points to. This works as long as i try to write to index 0 but any others cause xcode to throw an exc_bad_access error. This is all very confusing, can anyone with a bit of experience with this see where i am going wrong? It was working before when i was passing the actual pointer to the readFile function but it only worked once (think i was reallocating a copy of the pointer)
Any help would be very muchly appreciated
Thanks

  • 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-22T17:53:18+00:00Added an answer on May 22, 2026 at 5:53 pm

    Instead of this:

        *compArray[arraySize - 1] = newComp;//set the newly created part of the array to newComputer
    

    Try this:

        (*compArray)[arraySize - 1] = newComp;//set the newly created part of the array to newComputer
    

    This way you explicitly dereference compArray before subscripting.

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

Sidebar

Related Questions

I'm slowly getting the hang of pointers. But there are still some questions I
I am new to the whole MVP thing and slowly getting my head around
I'm slowly getting into MVC, but it's still relatively new to me. From what
I'm still pretty new to AJAX and javascript, but I'm getting there slowly. I
i'm slowly getting my head around jQuery and learning more each day, however I
I'm having some trouble getting an object to follow a path that is drawn
new guy here and I'm slowly getting the hang of python, but I have
I'm trying to implement an UPnP MediaServer in WCF. I'm slowly getting there, but
Ok, I'm slowly getting a grasp on this, but I need some more help.
I've just started learning Python and I'm slowly getting the hang of it, but

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.