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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:19:21+00:00 2026-06-03T04:19:21+00:00

I have an array of structs, each of which describes what I would call

  • 0

I have an array of structs, each of which describes what I would call a signal. I declared my struct this way :

 /* Structure for keywords */
 struct varStruct
 {
    char* varName;
    int varOccurence;
 } signals[MAX_SIGNALS];

I’m working in a loop which analyze a file and dynamically find the signal declarations, called newArrayName in my case. What I would like to do, is to add the read signal to the array ONLY IF it’s not already contained. Otherwise, I should increment the varOccurence variable.

Here is my code, but I have a segmentation fault (so no further information)…

    // We are in the loop that get the signals sequentially
    char* newArrayName = TheValueOfTheReadSignal.

    int i;
    // We browse all the array...
    for(i=0; i < MAX_SIGNALS; i++)
    {
        // If a signal already has the name of the currently-read signal, we inc its occurence
        if(strcmp(signals[i].varName, newArrayName) == 0)
        {
            signals[i].varOccurence++;
        }
        // Otherwise, we add a new signal with the read-name and an occurence corresponding to the value of a static variable that's incremented after each signal-reading.
        else
        {
            signals[index_Array].varName = newArrayName;
            signals[index_Array].varOccurence = index_Array;

        }
    }
     // We increment index_Array, which is a static int variable
       index_Array ++;
    // End of the loop that gets the signals 

Which leads to a segmentation fault. I’m not very good in C language, I would even say I am very bad at it. My guess here is that the signal array has not been initialized, so signal[i] doesn’t have any sense for him, but I don’t know how to initialize an array of structs. Maybe it’s another reason, I don’t know.

Thank you very much for your help.

  • 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-03T04:19:22+00:00Added an answer on June 3, 2026 at 4:19 am

    While you have no entries in recorded signals, or when i is larger than the number of recorded entries minus one,, the strcmp(signals[i].varName, newArrayName) tries to dereference the invalid (uninitialised or NULL) pointer signals[i].varName.

    Beyond that, as soon as index_Array becomes as large as MAX_SIGNALS (or larger),

    // We browse all the array...
    for(i=0; i < MAX_SIGNALS; i++)
    {
        // If a signal already has the name of the currently-read signal, we inc its occurence
        if(strcmp(signals[i].varName, newArrayName) == 0)
        {
            signals[i].varOccurence++;
        }
        // Otherwise, we add a new signal with the read-name and an occurence corresponding to the value of a static variable that's incremented after each signal-reading.
        else
        {
            signals[index_Array].varName = newArrayName;
            signals[index_Array].varOccurence = index_Array;
    
        }
    }
     // We increment index_Array, which is a static int variable
       index_Array ++;
    

    you write past the end of the array, which is undefined behaviour and will likely cause memory corruption and/or a segmentation fault – directly, or as a consequence of the memory corruption.

    In your loop, you write to signals[index_Array] every time you find a signal with a different name, and you increment index_Array each time after the loop is run, regardless of whether you already have some signal with that name or not. You should check whether you already have recorded the signal, and only if not write the new entry and increment index_Array:

    // Check for newArrayName == NULL and exit or otherwise handle the situation if it is
    // We browse all the array...
    for(i=0; i < index_Array && i < MAX_SIGNALS; i++)
    {
        // If a signal already has the name of the currently-read signal, we inc its occurence
        if(signals[i].varName != NULL && strcmp(signals[i].varName, newArrayName) == 0)
        {
            signals[i].varOccurence++;
            break; // we already have that signal, can stop looping
        }
    }
    // Check if i == MAX_SIGNALS
    // if it is, we have no more space in the array,
    // that needs handling
    if (i == index_Array && index_Array < MAX_SIGNALS) {
        // We haven't had that signal before, record it
        signals[index_Array].varName = newArrayName;
        signals[index_Array].varOccurence = index_Array;
        // And increment index_Array, which is a static int variable
        index_Array ++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small array of structs, each struct has three fields, all strings.
I have an array of structs. The struct has two function pointers. Each element
I have data structure which stores POD-structs (each instantiation stores a single type only,
I have a 32x1 struct array . Each element of this array has several
I have an array of structs and I intend to pass each element of
I have an array of structs and one of the fields in the struct
I have a byte array which needs to be marshalled into the following struct:
I have a Deque that contains this kind of stucts. struct New_Array { array<array<int,4>,4>
Assume I have an array which holds some struct defined as follows: static struct
How does one malloc an array of structs correctly if each struct contains an

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.