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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:12:17+00:00 2026-06-13T08:12:17+00:00

I am trying to learn about structs, pointers, and dynamic arrays in C. I

  • 0

I am trying to learn about structs, pointers, and dynamic arrays in C. I don’t understand how to create a dynamic array of structs using pointers. My code doesn’t work, and I don’t know what’s wrong with it. I have seen several examples of dynamic arrays, but non with structs. Any help would be appreciated. Please give some explanation, not just code snippets as I do want to understand not just solve this problem.

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

struct *struct_array;
int i,m,n,p;

struct data
{
    char inputA[20];
    char inputB[20];    
};

struct data get_data()
{
    struct data thisdata;

    printf("Please enter input A\n");
    scanf("%s", thisdata.inputA);

    printf("Please enter input B\n");
    scanf("%s", thisdata.inputB);

    return thisdata;
}

void Output(struct data struct_array, int n)
{
    int index = 0;
    for(i = 0; i<n ;i++)
    {
        printf("%s ", struct_array[i].inputA);
        printf("%s ", struct_array[i].inputB);
    }   
}

void resizeArray(int n)
{
    struct_array = (int*)realloc(n*sizeof(int));
}

void mainMenu()
{
    printf("Please select from the following options:\n");
    printf("1: Add new students to database\n");
    printf("2: Display current student database contents\n");
    printf("3: exit the program\n");
    scanf("%d", &p);
    if(p == 1)
    {
        printf("Please enter the number of students to register:\n");
        scanf("%d", &n);
        resizeArray(n);
        for(i = n; i<n ;i++)
        {
            struct_array[i] = get_data();
        }
    }
    else if(p == 2)
    {
         Output(struct_array, n);
    }
    else
    {
        free(struct_array);
        exit(0);
    }        
}

int main()
{    
    struct_array = (int*)realloc(2*sizeof(int));
    mainMenu();
}
  • 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-13T08:12:18+00:00Added an answer on June 13, 2026 at 8:12 am

    You have several errors in your source code:

    • struct *struct_array; (l. 5)
      What does it mean? Did you want to write struct data *struct_array?

    • printf("%s ", struct_array[i].inputA); (l.32 & l. 33)
      The argument struct_array masks the global declaration, and it is not an array. Why did you add this argument?

    • struct_array = (int *)realloc(n * sizeof(int)); (l. 39)
      You have forgotten an argument. Did you want to use malloc instead? Besides, the cast is not necessary (and incorrect!).

    • Unless you are using an hosted environnment and C99/C11, you should return a value from main.

    • Your variable index is not used. Why did you declare it?

    • for(i = n; i < n; i++) (l. 53)
      You won’t have any iteration here…

    The following code works as expected.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* TODO: Avoid global variables. */
    struct data *struct_array;
    
    struct data {
        char inputA[20];
        char inputB[20];
    };
    
    /* 
     * TODO: Try to avoid passing your structure (40 bytes + padding) 
     * without pointer. 
     */
    struct data get_data(void)
    {
        struct data thisdata;
    
        printf("Please enter input A\n");
    
        /* TODO: Avoid using `scanf` for human inputs. */
        scanf("%s", thisdata.inputA);
    
        printf("Please enter input B\n");
        scanf("%s", thisdata.inputB);
    
        return thisdata;
    }
    
    void Output(size_t n)
    {
        size_t i;
        for (i = 0; i < n; i++) {
            printf("%s ", struct_array[i].inputA);
            printf("%s ", struct_array[i].inputB);
        }
    }
    
    void resizeArray(size_t n)
    {
        /* TODO: Handle reallocations errors. */
        struct_array = realloc(struct_array, n * sizeof *struct_array);
    }
    
    void mainMenu(void)
    {
        size_t i, n;
        int p;
    
        /* TODO: Use a loop ? */
        printf("Please select from the following options:\n");
        printf("1: Add new students to database\n");
        printf("2: Display current student database contents\n");
        printf("3: exit the program\n");
        scanf("%d", &p);
    
        switch (p) {
        case 1:
            printf("Please enter the number of students to register:\n");
            scanf("%u", &n);
            resizeArray(n);
    
            for (i = 0; i < n; i++)
                struct_array[i] = get_data();
            break;
        case 2:
            Output(n);
            break;
        }
    }
    
    int main(void)
    {
        struct_array = malloc(2 * sizeof(int));
        mainMenu();
        free(struct_array);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn about makefile, mainly to understand a makefile that I'm using,
I am trying to learn about structs in C, but i do not understand
Trying to learn about php's arrays today. I have a set of arrays like
I'm trying to learn about this feature of javascript I keep seeing in code,
I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using
I'm trying to learn about reverse engineering, using Minesweeper as a sample application. I've
i am currently trying to learn about clean urls. i was using windows once.
I'm trying to learn more about arrays since I'm new to programming. So I
I have been trying to learn about resources and styles, I want to create
I'm trying to learn about JAX-WS, specifically using WSDLs in Java. I want to

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.