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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:52:17+00:00 2026-06-17T06:52:17+00:00

I’m trying to work through Kernighan’s book C Programming Language to teach myself C

  • 0

I’m trying to work through Kernighan’s book “C Programming Language” to teach myself C in preparation for a data structures class this spring (C is a required prerequisite), but am stuck on how to deal with multiple structures and how you would store multiples to use later for calculations and output. I wrote some code for structures related to student records with variables for id’s and scores which follows. The function names and parameters must stay as is and comments describe what should be done by each function.

So here’s what I’ve tried. I thought of just setting up an array of structures for ten students within the allocate function as follows:

struct student s[10];

However, when I try to return it to main and then pass it to the generate function, I get incompatibility errors. My current efforts are below. However, as you can see, my code fails to store anything but the last set of records (i.e. student.id and student.score) generated. Clearly I’m missing a key component and it’s preventing me from being able to generate random unique student id’s because I can’t check a new id against the previous ones. I also can’t move forward in writing functions to run calculations on student scores. Any suggestions would be appreciated. Thanks in advance.

#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
#include<assert.h>

struct student{
int id;
int score;
};

struct student* allocate(){
     /*Allocate memory for ten students*/
    struct student* s = malloc(10 * sizeof(struct student));
    assert (s != 0);

     /*return the pointer*/
     return s;
}

void generate(struct student* students){
 /*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0   and 100*/
   int i;
   for (i = 0; i < 10; i++) {
       students -> id = (rand()%10 + 1);
       students -> score = (rand()%(100 - 0 + 1) + 0);
       printf("%d, %d\n", (*students).id, (*students).score);
    }
}

void deallocate(struct student* stud){
     /*Deallocate memory from stud*/
    free(stud);
}

int main(){
   struct student* stud = NULL;

   /*call allocate*/
   stud = allocate();

   /*call generate*/
   generate(stud);

   /*call deallocate*/
   deallocate(stud);

   return 0;
}
  • 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-17T06:52:19+00:00Added an answer on June 17, 2026 at 6:52 am

    Change generate to

    void generate(struct student* students){
    
     /*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0   and 100*/
       int i;
       for (i = 0; i < 10; i++) {
           students[i].id = (rand()%10 + 1);
           students[i].score = (rand()%(100 - 0 + 1) + 0);
           printf("%d, %d\n", students[i].id, students[i].score);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.