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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:22:36+00:00 2026-06-14T15:22:36+00:00

I am trying to set up an array of structs for a few sample

  • 0

I am trying to set up an array of structs for a few sample parts which then need to output to a file. It seems to work ok but will only print the first 3 parts and then crashes.

    #include "stdio.h"

main()
{
struct stock{
    char name[20];
    int partNum;
    int quantity;
};

struct stock someStock[3];

strcpy(someStock[0].name, "License plate");
someStock[0].partNum = 1234;
someStock[0].quantity= 4;

strcpy(someStock[1].name, "Head lamp");
someStock[1].partNum = 1111;
someStock[1].quantity= 2;

strcpy(someStock[2].name, "Rear wiper");
someStock[2].partNum = 2222;
someStock[2].quantity= 6;

strcpy(someStock[3].name, "Tyres");
someStock[3].partNum = 3333;
someStock[3].quantity= 10;

struct stock *ptr = &someStock[0];
int i;
FILE *file_ptr;
file_ptr = fopen("stock.dat", "w");

for(i=0;i<4;i++)
{
    fprintf(file_ptr, "%s %d %d\n",(*ptr).name,(*ptr).partNum, (*ptr).quantity);
    ptr++;
}

fclose(file_ptr);
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-14T15:22:38+00:00Added an answer on June 14, 2026 at 3:22 pm

    As others said:

    struct stock someStock[4];
    

    Some other tips for you:

    struct stock *ptr = &someStock[0];
    

    Is equivalent to:

    struct stock *ptr = someStock;
    

    As Daniel pointed out this is since the array name evaluates to a pointer to its first element in that context.

    And:

    fprintf(file_ptr, "%s %d %d\n",(*ptr).name,(*ptr).partNum, (*ptr).quantity);
    

    Is equivalent to:

    fprintf(file_ptr, "%s %d %d\n", ptr->name, ptr->partNum, ptr->quantity);
    

    -> is known as the structure pointer operator, it dereferences a pointer and accesses a member all in one, as oppose to the . which is the structure member operator and so requires the indirection operator * as well when you have a pointer.

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

Sidebar

Related Questions

Trying to set an array and a count, which are both static members, by
Im trying to set an array in Javascript , and then give it values
I'm trying to use an array to set the where parameters for a Zend
Trying to set it so if a certain condition is met then one of
I'm trying to create an array of a struct, which I have done and
I'm trying to create a structure array which links input strings to classes as
I am trying to set an array based on query results that are NOT
I'm trying to consume a DLL-located method in C#, which returns a dynamic array
Im trying to set an array for a timer program im writing in xcode.
So I'm basically just trying to take in some file input, and then take

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.