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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:12:42+00:00 2026-05-19T04:12:42+00:00

struct bucket { int nStrings; //No. of Strings in a Bucket. char strings[MAXSTRINGS][MAXWORDLENGTH]; //

  • 0
struct bucket
{
   int nStrings;        //No. of Strings in a Bucket.
   char strings[MAXSTRINGS][MAXWORDLENGTH];     // A bucket row can contain maximum 9 strings of max string length 10.
};//buck[TOTBUCKETS];

void lexSorting(char array[][10], int lenArray, int symb)       //symb - symbol, sorting based on character symbols.
{
   int i, j;
   int bucketNo;
   int tBuckNStrings;
   bucket buck[TOTBUCKETS];

   for(i=0; i<lenArray; i++)
   {
      bucketNo = array[i][symb] - 'a';          // Find Bucket No. in which the string is to be placed.
      tBuckNStrings = buck[bucketNo].nStrings;  // temp variable for storing nStrings var in bucket structure.
      strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]);   // Store the string in its bucket.
      buck[bucketNo].nStrings = ++tBuckNStrings;        //Increment the nStrings value of the bucket.
   }

//   lexSorting(array, lenArray, ++symb);

   printf("****** %d ******\n", symb);
   for(i=0; i<TOTBUCKETS; i++)
   {
      printf("%c = ", i+'a');
      for(j=0; j<buck[i].nStrings; j++)
         printf("%s ",buck[i].strings[j]);
      printf("\n");
   }
}

int main()
{
   char array[][10] = {"able","aback","a","abet","acid","yawn","yard","yarn","year","yoke"};
   int lenArray = 10;
   int i;

   printf("Strings: ");
   for(i=0; i<lenArray; i++)
      printf("%s ",array[i]);
   printf("\n");

   lexSorting(array, lenArray, 0);
}

Well here is the complete code, that I am trying. since its been a long time since i have touched upon C programming, so somewhere i am making mistake in structure declaration.
The problem goes here:-

1) I have declared a structure above and its object as array(buck[]).

2) Now when I declare this object array along with the structure, it works fine.. I have commented this thing right now.

3) But when I declare this object array inside the function.. because ultimately i have to declare inside function( as i need to build a recursive program, where objects will be created in very recursive call) then the program is throwing segmentation fault.

Expected Output

> [others@centos htdocs]$ ./a.out
> Strings: able aback a abet acid yawn
> yard yarn year yoke
> ****** 0 ****** 
> a = able aback a abet acid 
> b = 
> c 
> .
> .
> y = yawn yard yarnyear yoke 
> z =

Actual Output

[others@centos htdocs]$ ./a.out
Strings: able aback a abet acid yawn yard yarn year yoke
Segmentation fault

I have no idea, what difference I made in this. Kindly help.

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-19T04:12:42+00:00Added an answer on May 19, 2026 at 4:12 am

    What’s wrong with your program is that it doesn’t contain a main() function hence it won’t link.

    Beyond that, you should always do the following when asking questions here:

    • Provide a complete, minimal code sample that demonstrates the problem.
    • Detail the expected behaviour.
    • Detail the actual behaviour.

    In fact, when I add the line:

    int main (void) { return 0; }
    

    it compiles and links fine.

    That means it’s almost certainly a run-time error you’re experiencing hence we need the main() to figure out what you’re doing wrong.

    Using my psychic debugging skills, an important difference between declaring it at file scope and block scope is that the file-scope version will be initialised to zeros.

    That means all the structure fields will be effectively zero (for the count) and empty strings (for the strings). With block scope, those counts and strings will be uninitialised.

    The fact that you’re using TOBUCKETS to print the structure out probably means you’re trying to print out one of those uninitialised strings.

    I think what’s probably happening is that the nStrings field contains a garbage value when you start the processing. You should probably initialise it to zero manually (with a loop) and see if that fixes your problem. Put this after the declaration of buck in your sort function:

    for (i = 0; i < TOTBUCKETS; i++)
        buck[i].nStrings = 0;
    

    Right. It turns out that was the problem. When I fix up the errors in your latest code, I get the segmentation violation as well but, when I add that section above, it works fine:

    #include <stdio.h>
    #include <string.h>
    
    #define MAXSTRINGS 9
    #define MAXWORDLENGTH 10
    #define TOTBUCKETS 26
    
    struct bucket
    {
       int nStrings;        
       char strings[MAXSTRINGS][MAXWORDLENGTH];     
    };
    
    void lexSorting(char array[][10], int lenArray, int symb)       
    {
       int i, j;
       int bucketNo;
       int tBuckNStrings;
       struct bucket buck[TOTBUCKETS];
    
       for(i=0; i<TOTBUCKETS; i++) buck[i].nStrings = 0;
    
       for(i=0; i<lenArray; i++)
       {
          bucketNo = array[i][symb] - 'a';          
          tBuckNStrings = buck[bucketNo].nStrings;  
          strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]);   
          buck[bucketNo].nStrings = ++tBuckNStrings;        
       }
    
       printf("****** %d ******\n", symb);
       for(i=0; i<TOTBUCKETS; i++)
       {
          printf("%c = ", i+'a');
          for(j=0; j<buck[i].nStrings; j++)
             printf("%s ",buck[i].strings[j]);
          printf("\n");
       }
    }
    
    int main()
    {
       char array[][10] = {"able","aback","a","abet","acid",
                           "yawn","yard","yarn","year","yoke"};
       int lenArray = 10;
       int i;
    
       printf("Strings: ");
       for(i=0; i<lenArray; i++)
          printf("%s ",array[i]);
       printf("\n");
    
       lexSorting(array, lenArray, 0);
    }
    

    The output of that was:

    Strings: able aback a abet acid yawn yard yarn year yoke 
    ****** 0 ******
    a = able aback a abet acid 
    b = 
    c = 
    d = 
    e = 
    f = 
    g = 
    h = 
    i = 
    j = 
    k = 
    l = 
    m = 
    n = 
    o = 
    p = 
    q = 
    r = 
    s = 
    t = 
    u = 
    v = 
    w = 
    x = 
    y = yawn yard yarn year yoke 
    z = 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a struct ABC to contain an int ID, string NAME, string
I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct
struct elem { int i; char k; }; elem user; // compile error! struct
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
Say I have a struct s with an int pointer member variable i. I
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
If I define a struct in C# using automatic properties like this: public struct
I have a struct defined like follows as part of an object. I'm trying

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.