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

The Archive Base Latest Questions

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

I have a struct with many char array like this (and it works) :

  • 0

I have a struct with many char array like this (and it works) :

struct maytinh {
    char tenmay[10];
    char mamay[10];
    char test[10];
    float manhinh;
    int gia;
};

But if its like this,

struct maytinh {
    char tenmay[99];
    char mamay[99];
    char test[99];
    float manhinh;
    int gia;
};

it breaks when I compile and scanf data in.
Moreover when I put another array in the struct, it also breaks:

struct maytinh {
    char tenmay[10];
    char mamay[10];
    char test[10];
    char test2[10];
    float manhinh;
    int gia;
};

Why? (I’m compiling with c-free 4.0)

Here is the scanf code :

void main() {
    int n,i;
    printf("input :");
    scanf("%d",&n);
    struct maytinh a[n];
    for (i=1;i<=n;i++) {    
        printf("May tinh so: %d\n",i);
        printf("Nhap ten may :");
        scanf("%s",a[i].tenmay);
        printf("Nhap ma may :");
        scanf("%s",a[i].mamay);
        printf("Nhap test :");
        scanf("%s",a[i].test);
        printf("Nhap kich thuoc man hinh:");
        scanf("%d",&a[i].gia);
    };
}

this code totally work with the first struct.
here is my fix for dynamic size struct array with malloc() and typedef (just for someone like me in need):

typedef struct maytinh {
    char tenmay[99];
    char mamay[99];
    char test[99];
    char test3[99];
    float manhinh;
    int gia;
};
void main(){
    int n,i;
    printf("input :");
    scanf("%d",&n);
    maytinh *a;
    a=(maytinh*)malloc(n*sizeof(maytinh));
    for (i=0;i<=n;i++) {    
    printf("May tinh so: %d\n",i);
    printf("Nhap ten may :");
    scanf("%s",a[i].tenmay);
    printf("Nhap ma may :");
    scanf("%s",a[i].mamay);
    printf("Nhap test :");
    scanf("%s",a[i].test);
    printf("Nhap kich thuoc man hinh:");
    scanf("%d",&a[i].gia);
    };
}
  • 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-16T23:03:14+00:00Added an answer on May 16, 2026 at 11:03 pm

    In addition to ring0’s answer, the size or the amount of the arrays in your structure shouldn’t matter. You should just be aware that even if you allocate characters in your array, it doesn’t stop people from typing in longer strings (i.e., buffer overflow). You should always set a length to the scanf() formats to ensure that it doesn’t overflow. This is done by adding the length-1 in %s.

    Also assuming c-free 4.0 supports dynamic arrays, you need to use malloc() to allocate your struct array following Reinderien’s advice.

    i.e.,

    void main() {
        int n,i;
        printf("input :");
        scanf("%d",&n);
        struct maytinh a[n]; /* change to a malloc() call if it doesn't support dynamic arrays */
        for (i=0;i<n;i++) {    
            printf("May tinh so: %d\n",i);
            printf("Nhap ten may :");
            scanf("%9s",a[i].tenmay);
            printf("Nhap ma may :");
            scanf("%9s",a[i].mamay);
            printf("Nhap test :");
            scanf("%9s",a[i].test);
            printf("Nhap kich thuoc man hinh:");
            scanf("%d",&a[i].gia);
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I have struct like this typedef struct { float x; float y;
I have many classes/methods like this: template<typename CharT, typename TraitsT = std::char_traits<CharT> > struct
Have to create an array of struct. Since don't know how many entries will
I have seen many programs consisting of structures like the one below typedef struct
I have this structure: typedef struct { char *str; ... } nameType; I create
-In my c code I have a struct which contains many unknown sized arrays
I have many structs (classes) and standalone functions that I like to compile separately
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have a struct that looks something like: struct foo_t { template <std::size_t x,
I have a structure that has an array of pointers. I would like 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.