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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:02:42+00:00 2026-05-20T03:02:42+00:00

I am sorry if this is a really simple question but I am really

  • 0

I am sorry if this is a really simple question but I am really rusty in C and googling hasn’t helped so far. I have a C function that takes in an array of structs and an integer:

int foo(struct record *records, int n)
{

}

and also a linked list where each node has a struct:

struct rnode
{
    struct record rec;
    struct rnode *next;
}

and struct record is:

struct record
{
    enum recordtype type;
    union recordvalue
    {
        int intval;
        char strval[19];
    } value;
};

inside foo(struct record , int) I am iterating through the linked list and assigning the first “n” struct records into the array like:

int foo(struct record *records, int n)
{
    int count = 0;
    struct rnode *cur = recordlist;
    while(cur != NULL)
    {
        records[count] = cur->rec; //Basically, how do I write this line?
        printf("%d\n", records[count].type); //Both these print the correct values
        printf("%d\n", records[count].value.intval); //Prints correct values
        count++;
    }
}

I tried doing:
records[count] = cur->rec

which compiles but when I do the following:

struct record *records = malloc(sizeof(struct record)*n);
foo(records, n); //This should populate the array records but it isn't.
//If I print record[0].value.intval here I get 0.

but when I pass &records[0] to another function like:

checkrecord(&record[0]);

where checkrecord is declared:

checkrecord(const struct record *r)

inside that function, r->type and r->value.intval both return 0 instead of the correct value.

I’m pretty sure I’m storing the struct record into the array correctly, but I’m not sure what else I’m doing wrong.

I don’t mean to sound stubborn but the issue is that the checkrecord() function I am not at liberty to change but I can change how I pass the parameters to it.

  • 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-20T03:02:43+00:00Added an answer on May 20, 2026 at 3:02 am

    Thanks for your help everyone. It was actually a memory issue elsewhere. The above code is correct.

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

Sidebar

Related Questions

Firstly, please don't dismiss this question - I'm aware it's an ugly situation but
Good day. I never really got a good hand at JavaScript, therefore this unusual
Edit: The bug which prompted this question has now been fixed . In the
So i'm writing a C program for a simple shell. Not too difficult but
I've bene working with Objective-C for a while now, and so far I have
The idea is pretty simple: Where the timer is made in Javascript, when it
In my controller class, I initialize two instances of a model class (whose header
in SQL Server Management Studio (SSMS) 2008 R2 Dev on the table (because I
I've been trying write an application which will be able to connect to a

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.