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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:04:35+00:00 2026-06-02T11:04:35+00:00

I have an ordered list of structs (CASE) by a name (string) field. I

  • 0

I have an ordered list of structs (CASE) by a “name” (string) field. I want to create a new struct (ARTCOUNT) with parameters “name”(string) and “count”(integer).

Structs:

typedef struct
 {
   char* name;
   char* art;
   int rating;
 }CASE;

typedef struct
 {
   char* name;
   int count;
 }ARTCOUNT;

I will walk the CASE array and if the “name” in CASE matches the name in ARTCOUNT, I will add 1 to the count, else I will create a new ARTCOUNT array with the new name and continue walking the array.

The problem I’m having is a weird segmentation fault when I try to add to the count if the names match.

/*all is the array of CASE and pLast points to the last CASE in all*/
void countArt(CASE* all, CASE* pLast)
{
  CASE* walker = all;
  ARTCOUNT* artAll;
  ARTCOUNT* artWalker = artAll;
  ARTCOUNT* artLast;


  if((artAll = (ARTCOUNT*)malloc(sizeof(ARTCOUNT)*(pLast-all+1))) == NULL)
  {
    printf("Fatal memory error!\n"); 
    exit(1);
   }


  artWalker->name = (char*)malloc(sizeof(char)*(100));
  strcpy(artWalker->name, walker->name);
  artWalker->count = 1;


  for(walker = all+1; walker <= pLast; walker++)
  {
    if (strcmp(walker->name, artWalker->name) == 0)
    {
      artWalker->count += 1;
    }
    else
    {
      artWalker++;
      artWalker->name = (char*)malloc(sizeof(char)*(100));
      strcpy(artWalker->name, walker->name);
      artWalker->count = 1;  //if I comment this out, no segmentation fault
    }

  }
  artLast = artWalker;

  return;
}

As mentioned in the code above, I managed to narrow down what might be the error to the line artWalker->count-1;. If I comment out this line, the error disappears. However, if I try to print say artWalker->name in the loop, I still get a segmentation error as well. I’ve checked over my memory allocation several times and I don’t think it’s that.

Any tips?

  • 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-02T11:04:37+00:00Added an answer on June 2, 2026 at 11:04 am

    you have declared

    ARTCOUNT* artWalker = artAll;
    

    and then changed artAll

    if((artAll = (ARTCOUNT*)malloc(sizeof(ARTCOUNT)*(pLast-all+1))) == NULL)
    

    at this point artWalker is pointing to a random memory location, so it’s not surprising that you get a segmentation fault

    try assigning

    artWalkr = artAll;
    

    after malloc.

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

Sidebar

Related Questions

I have an ordered list like this: var list = new List<String>(){aaa,aab,aac,baa,bab,bac}; I want
I currently have an ordered list that I want to markup using the new
I have an ordered list of movies. Each movie has a name and a
I have an ordered list, but I want to number is manually in a
I have an ordered list A and a string set B in Java. I
I have an ordered list which has a <input type=text... and <button> field. When
I have an ordered list like 0,1,2, 6,7, 10 I want to get the
I have a list of ordered items, ordered according to the int field order
I have an ordered list in HTML. I would like to add styling only
I have an ordered list (ol) on my page but the numbering is not

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.