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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:36:22+00:00 2026-06-02T06:36:22+00:00

I am having trouble with a struct array. I need to read in a

  • 0

I am having trouble with a struct array. I need to read in a text file line by line, and compare the values side by side. For example “Mama” would return 2 ma , 1 am because you have ma- am- ma. I have a struct:

typedef struct{
    char first, second;
    int count;
} pair;

I need to create an array of structs for the entire string, and then compare those structs. We also were introduced to memory allocation so we have to do it for any size file. That is where my trouble is really coming in. How do I reallocate the memory properly for an array of structs? This is my main as of now (doesn’t compile, has errors obviously having trouble with this).

int main(int argc, char *argv[]){
//allocate memory for struct
pair *p = (pair*) malloc(sizeof(pair));
//if memory allocated
if(p != NULL){
    //Attempt to open io files
    for(int i = 1; i<= argc; i++){
        FILE * fileIn = fopen(argv[i],"r");
        if(fileIn != NULL){
            //Read in file to string
            char lineString[137];
            while(fgets(lineString,137,fileIn) != NULL){
                //Need to reallocate here, sizeof returning error on following line
                //having trouble seeing how much memory I need
                pair *realloc(pair *p, sizeof(pair)+strlen(linestring));
                int structPos = 0;
                for(i = 0; i<strlen(lineString)-1; i++){
                    for(int j = 1; j<strlen(lineSTring);j++){
                        p[structPos]->first = lineString[i];
                        p[structPos]->last = lineString[j];
                        structPos++;
                    }
                }
            }
        }
    }
}
else{
    printf("pair pointer length is null\n");
}

}

I am happy to change things around obviously if there is a better method for this. I HAVE to use the above struct, have to have an array of structs, and have to work with memory allocation. Those are the only restrictions.

  • 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-06-02T06:36:23+00:00Added an answer on June 2, 2026 at 6:36 am

    Allocating memory for an array of struct is as simple as allocating for one struct:

    pair *array = malloc(sizeof(pair) * count);
    

    Then you can access each item by subscribing “array”:

    array[0] => first item
    array[1] => second item
    etc
    

    Regarding the realloc part, instead of:

    pair *realloc(pair *p, sizeof(pair)+strlen(linestring));
    

    (which is not syntactically valid, looks like a mix of realloc function prototype and its invocation at the same time), you should use:

    p=realloc(p,[new size]);
    

    In fact, you should use a different variable to store the result of realloc, since in case of memory allocation failure, it would return NULL while still leaving the already allocated memory (and then you would have lost its position in memory). But on most Unix systems, when doing casual processing (not some heavy duty task), reaching the point where malloc/realloc returns NULL is somehow a rare case (you must have exhausted all virtual free memory). Still it’s better to write:

    pair*newp=realloc(p,[new size]);
    if(newp != NULL) p=newp;
    else { ... last resort error handling, screaming for help ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble freeing a pointer in a pointer array (values). typedef struct
I'm having some trouble with some struct typedef declarations in a header file not
I'm having trouble passing a structure array as a parameter of a function struct
I'm having trouble understanding the value of this in the following example: struct A
Having trouble finding an explanation in the docs, would someone be kind enough to
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I have been having alot of trouble with this stupid struct. I don't see
I'm having trouble passing an array of structs to a function in C. I've
I'm having trouble printing a member of a struct that is returned from a
I am having some trouble marshaling a pointer to an array of strings. It

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.