tags:

views:

137

answers:

4

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);
    };
}
+2  A: 

My first guess would be that it fails to allocate larger structs because you're allocating them on the stack instead of with malloc(), though I'd have to see more example code to say for sure.

Edit: Looks like my guess was right. You're using a non-standard extension to C, using a non-constant number to allocate an array. I bet if you replace that with an appropriate malloc call your issue will be fixed.

Reinderien
i've add the scanf code
Singgum3b
You totally right.That save the day ^.^ though i cant get back my mark
Singgum3b
An array with a non-constant size is standard C99 and called a *variable length array* (VLA). I agree that you do not want to use it here given issues like this.
schot
+1  A: 

Arrays start at 0 in C

  for (i=1;i<=n;i++) ... a[i] ...

Should be

  for (i=0;i<n;i++) ... a[i] ...

And maybe

  printf("May tinh so: %d\n",i+1);
ring0
yeah, i know that ,but it isn't solve the problem.
Singgum3b
Using `i` from `1` *may* work if you are lucky, but usually there is will a *out of bounds* problem, or variables overlapping. You probably had some luck with `[10]` arrays (small), but you were more likely to meet a problem with bigger `[99]` arrays.
ring0
actually i do change i from 1 to 0 ,and i tested it.Its still crash after i done with scanf =[
Singgum3b
Starting the array from 1 is **definitely** wrong. Please make an edit and add the *new* code below the *old* code (so that everyone understands). There is something *else* wrong.
ring0
A: 

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);
    };
}
Jeff M
thanks guys.my head gonna explode if i didn't know how to solve this problem.
Singgum3b
"void main()" isnt ANSI C
A: 

I guess you ask it at the right place -- stackoverflow :D

Try to change

struct maytinh a[n];

to

struct maytinh *a = (maytinh*) malloc(sizeof(maytinh) * n);

and insert #include "malloc.h" too.

tia