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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:12:13+00:00 2026-05-16T05:12:13+00:00

Hii , I have been trying to write a program… We have a structure

  • 0

Hii ,

I have been trying to write a program… We have a structure which has a rank field and the name field.The pointer to this structure is stored in an array of fixed size. I have implemented it as follows and i have certain problems…
The code i have written is :

 #include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct 
{
 int rank;
 char *name;
}node;

int insert(node **a , char name[] , int *rank)
 {
 if(*rank >= 5)
  {
   printf("\n Overflow ");
   return 0;
  } 
  (*rank)++;
  node *new = (node *)malloc(sizeof(node));
  new->name = name;
  new->rank = *rank;
  a[*rank] = new;

  return 0;
 } 

int delete(node **a , int *rank)
 {
  int i = *rank;
  if(*rank<0)
   {
    printf("\n No elements");
    return 0;
   }
   printf("\n Deleting %d , %s ",((a[*rank]))->rank,((a[*rank]))->name);
   printf("\n Reordering the elements ");
   while(i<5)
    {
     a[i] = a[i+1];
    }  
  return 0;
 }

 int display(node **a , int rank)
  {
   while(rank>0 && (a[rank])>0)
    {
     printf(" rank = %d    name = %s \n",((a[rank])->rank),((a[rank])->name));
     rank--;
    }            
    return 0;
  }

int main()
 {
  node *a[5] = {NULL};
  char ch = 'y';
  int choice,rank = -1;
  char name[10];
  while(ch!='n' || ch!= 'N')
   {
    printf("\n Enter 1 to insert , 2 to delete , 3 to display and 4 to exit \n");
    scanf("%d",&choice);
    switch(choice)
     {
      case 1:
        printf("\n Enter name to insert");
        gets(name);
        insert(a,name,&rank);
        break;
      case 2:
        printf("\n Enter rank to delete ");
        scanf("%d",&rank);
        delete(a,&rank);
        break;
      case 3:
        display(a,rank);
        break;
      case 4:
        exit(0);
      default:
        printf("\n Invalid choice...please enter again ");
        break;
     } 
    ch = getchar();
  }
 return 0;
 } 

First thing is the system automatically takes the choice except for the first time…(i couldn’t find the fault there…) and i am a bit confused about this pointer stuff…Please see if its alright…Any corrections are welcome and please give me some explanation as to why it is wrong and how we shd do it…

Thank You

  • 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-16T05:12:13+00:00Added an answer on May 16, 2026 at 5:12 am

    First of all, all your functions always return 0 — even for an error condition. Life would be so much easier, if you passed rank in as an int, and returned it’s new value.

    rank = insert(a, name, rank); 
    /* : */
    /* : */
    int insert(node **a , char name[] , int rank)  
    {  
     if(rank >= 5)  
     {  
       printf("\n Overflow ");  
       return 0;  
     }   
     rank++;  
     node *new = (node *)malloc(sizeof(node));  
     new->name = name;  
     new->rank = rank;  
     a[rank] = new;  
     return rank;  
    }
    

    It’s been many years since I last used scanf, but as I recall, you must account for every character in the stream, meaning,”Don’t forget the Enter”.

    scanf("%d\n",&choice);  
    

    Also with gets(name);, if you type more tha 9 characters, you are quite screwed, as it will overwrite the stack of your program.

    UPDATE:
    Also, you have two ways to exit this program, except one will never work. You could choose option “4” which will call exit(0). Alternately, at the end of each command, you wait for a character before stepping over. It appears you want to be able to enter “N” ther and exit, except that won’t work:

    while(ch!='n' || ch!= 'N') 
    

    for that to evaluate to false, ch must be both “n” & “N” at the same time. You really want

    while(ch!='n' && ch!= 'N') 
    

    UPDATE2:
    I just noticed the biggest problem in you code. name everywhere in your code only ever points to the single array defined in main(). Everytime you enter a new name, it overwrites that array, and since every node points to that one array, the name changes everywhere. You need to make a copy.
    in insert():

    node *new = (node *)malloc(sizeof(node));     
    new->name = strdup(name);    // use malloc internally.
    

    Then in delete() you’ll have to free that memory (speaking of which, you need to free node there too…)

    printf("\n Deleting %d , %s ",((a[*rank]))->rank,((a[*rank]))->name);       
    free(a[*rank]->name);
    free(a[*rank]);
    printf("\n Reordering the elements ");
    

    Remember, Whenever you call malloc, you will eventually have to call free.

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

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are two closely-related issues. First, within recipes.py, you need… May 16, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer EDIT : Mark is correct. The earlier query was syntactically… May 16, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer For the record, as far as I can tell, you… May 16, 2026 at 11:22 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hi I have been trying to find an answer to this question. I am
Hi I am trying to learn CSS and have been looking at the source
Hi I have been looking at certain tutorials on sharparchitecture and trying to no
hi i am trying to create a URL that looks like this: black/granite/worktops where
Hi i have been using an IDE but now I need to run and
Hii....i am new to iPhone programming..Can anybody help me out please i have an
Hii , I am developing an application where I ran across into this problem
Hi I have a datagridview which binds XML manually. I wish to sort out
Hi I have a html form with the following code <td ><label> <textarea name=comments
Hi I have a colorbox which opens on every page when the user clicks

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.