linked list

Discussion in 'C Programming' started by Zhang Yuan, Jun 9, 2012.

  1. Zhang Yuan

    Zhang Yuan Guest

    ----------------------------------------------------------------------------
    #include<stdio.h>
    #include<stdlib.h>
    struct stu{
    int num;
    char name[12];
    int age[0];
    struct stu *next;
    };

    int main(){

    struct stu *curr,*head;

    head=(stu *)malloc(sizeof(stu));
    curr=head;
    scanf("%d",&(head->num));
    curr->next=NULL;

    while(curr->num!=0){
    scanf("%s%d",curr->name,curr->age);
    curr=(stu *)malloc(sizeof(struct stu));
    scanf("%d",&(curr->num));
    curr->next=NULL;
    }
    return 0;
    }

    --------------------------------------------------------------------------
    it doesn't work.When I debug:

    input :
    101 zhang 19

    the memory information:4 pictures
    http://flic.kr/p/ccMQey

    why 0x6f17e0 change from 00-->13 unexpectedly.
    I think the "*next" and "int age"storaged in different place.
    but it looks like it overwrite?
    Zhang Yuan, Jun 9, 2012
    #1
    1. Advertising

  2. Zhang Yuan

    Kaz Kylheku Guest

    On 2012-06-09, Zhang Yuan <> wrote:
    > ----------------------------------------------------------------------------
    > #include<stdio.h>
    > #include<stdlib.h>
    > struct stu{
    > int num;
    > char name[12];
    > int age[0];
    > struct stu *next;
    > };
    >
    > int main(){
    >
    > struct stu *curr,*head;
    >
    > head=(stu *)malloc(sizeof(stu));


    If you're really working in C and not C++, you want sizeof (struct stu)
    here (just like in your other malloc call below).

    (In C++, a struct stu { ... } declaration will introduce stu as a type name,
    but not in C.

    In C, if you have some defined object called "stu" somewhere, or a type "stu",
    this sizeof will measure the size of that object or type, and not "struct stu".

    Example:

    int stu;

    head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
    head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */

    > curr=head;
    > scanf("%d",&(head->num));


    scanf without checking the return value is a very bad way to obtain input.
    What if the next input character is not a digit? What if EOF is signaled?

    scanf with error checking is still very bad for interactive input.

    > curr->next=NULL;
    >
    > while(curr->num!=0){
    > scanf("%s%d",curr->name,curr->age);
    > curr=(stu *)malloc(sizeof(struct stu));
    > scanf("%d",&(curr->num));
    > curr->next=NULL;
    > }


    This leaks memory. You have assigned a new object to curr
    and populated some of its fields, num and next.
    but this object is not entered into the linked list.

    If the input ends with EOF or is erroneous, you potentially
    have an infinite loop. Worse, an infinite loop which
    allocates memory.

    None of that readily explains the overwrite of 19.

    > return 0;
    > }
    >
    > --------------------------------------------------------------------------
    > it doesn't work.When I debug:
    >
    > input :
    > 101 zhang 19
    >
    > the memory information:4 pictures
    > http://flic.kr/p/ccMQey
    >
    > why 0x6f17e0 change from 00-->13 unexpectedly.


    At what point in the execution of the program?

    If you single step the program, which line?
    Kaz Kylheku, Jun 9, 2012
    #2
    1. Advertising

  3. Zhang Yuan

    Zhang Yuan Guest

    On Saturday, June 9, 2012 12:59:57 PM UTC+8, Kaz Kylheku wrote:
    > On 2012-06-09, Zhang Yuan <> wrote:
    > > ----------------------------------------------------------------------------
    > > #include<stdio.h>
    > > #include<stdlib.h>
    > > struct stu{
    > > int num;
    > > char name[12];
    > > int age[0];
    > > struct stu *next;
    > > };
    > >
    > > int main(){
    > >
    > > struct stu *curr,*head;
    > >
    > > head=(stu *)malloc(sizeof(stu));

    >
    > If you're really working in C and not C++, you want sizeof (struct stu)
    > here (just like in your other malloc call below).
    >
    > (In C++, a struct stu { ... } declaration will introduce stu as a type name,
    > but not in C.
    >
    > In C, if you have some defined object called "stu" somewhere, or a type "stu",
    > this sizeof will measure the size of that object or type, and not "struct stu".
    >
    > Example:
    >
    > int stu;
    >
    > head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
    > head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
    >
    > > curr=head;
    > > scanf("%d",&(head->num));

    >
    > scanf without checking the return value is a very bad way to obtain input.
    > What if the next input character is not a digit? What if EOF is signaled?
    >
    > scanf with error checking is still very bad for interactive input.
    >
    > > curr->next=NULL;
    > >
    > > while(curr->num!=0){
    > > scanf("%s%d",curr->name,curr->age);
    > > curr=(stu *)malloc(sizeof(struct stu));
    > > scanf("%d",&(curr->num));
    > > curr->next=NULL;
    > > }

    >
    > This leaks memory. You have assigned a new object to curr
    > and populated some of its fields, num and next.
    > but this object is not entered into the linked list.
    >
    > If the input ends with EOF or is erroneous, you potentially
    > have an infinite loop. Worse, an infinite loop which
    > allocates memory.
    >
    > None of that readily explains the overwrite of 19.
    >
    > > return 0;
    > > }
    > >
    > > --------------------------------------------------------------------------
    > > it doesn't work.When I debug:
    > >
    > > input :
    > > 101 zhang 19
    > >
    > > the memory information:4 pictures
    > > http://flic.kr/p/ccMQey
    > >
    > > why 0x6f17e0 change from 00-->13 unexpectedly.

    >
    > At what point in the execution of the program?
    >
    > If you single step the program, which line?


    I single step the program in line :

    scanf("%d",&(head->num));
    Zhang Yuan, Jun 9, 2012
    #3
  4. Zhang Yuan

    Ike Naar Guest

    On 2012-06-09, Zhang Yuan <> wrote:
    > struct stu{
    > int num;
    > char name[12];
    > int age[0];


    There is room for zero ages in the struct ...

    > struct stu *next;
    > };
    >
    > int main(){
    >
    > struct stu *curr,*head;
    >
    > head=(stu *)malloc(sizeof(stu));
    > curr=head;
    > scanf("%d",&(head->num));
    > curr->next=NULL;
    >
    > while(curr->num!=0){
    > scanf("%s%d",curr->name,curr->age);


    .... but here you try to store something in curr->age.
    Since curr->age has size zero, the address of curr->age
    can be the same as the address of curr->next, and storing
    a value in curr->age may overwrite whatever was in curr->next.

    > curr=(stu *)malloc(sizeof(struct stu));
    > scanf("%d",&(curr->num));
    > curr->next=NULL;
    > }
    > return 0;
    > }
    >
    Ike Naar, Jun 9, 2012
    #4
  5. Zhang Yuan

    Ian Collins Guest

    On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
    > On 2012-06-09, Zhang Yuan<> wrote:
    >> ----------------------------------------------------------------------------
    >> #include<stdio.h>
    >> #include<stdlib.h>
    >> struct stu{
    >> int num;
    >> char name[12];
    >> int age[0];
    >> struct stu *next;
    >> };
    >>
    >> int main(){
    >>
    >> struct stu *curr,*head;
    >>
    >> head=(stu *)malloc(sizeof(stu));

    >
    > If you're really working in C and not C++, you want sizeof (struct stu)
    > here (just like in your other malloc call below).
    >
    > (In C++, a struct stu { ... } declaration will introduce stu as a type name,
    > but not in C.
    >
    > In C, if you have some defined object called "stu" somewhere, or a type "stu",
    > this sizeof will measure the size of that object or type, and not "struct stu".
    >
    > Example:
    >
    > int stu;
    >
    > head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
    > head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */


    Which one reason why

    head = malloc( sizeof *head );

    is a good idea!

    --
    Ian Collins
    Ian Collins, Jun 9, 2012
    #5
  6. Zhang Yuan

    Ian Collins Guest

    On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
    > ----------------------------------------------------------------------------
    > #include<stdio.h>
    > #include<stdlib.h>
    > struct stu{
    > int num;
    > char name[12];
    > int age[0];


    Didn't your compiler warn about this?

    --
    Ian Collins
    Ian Collins, Jun 9, 2012
    #6
  7. Zhang Yuan

    Zhang Yuan Guest

    On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
    > On 2012-06-09, Zhang Yuan <> wrote:
    > > struct stu{
    > > int num;
    > > char name[12];
    > > int age[0];

    >
    > There is room for zero ages in the struct ...
    >
    > > struct stu *next;
    > > };
    > >
    > > int main(){
    > >
    > > struct stu *curr,*head;
    > >
    > > head=(stu *)malloc(sizeof(stu));
    > > curr=head;
    > > scanf("%d",&(head->num));
    > > curr->next=NULL;
    > >
    > > while(curr->num!=0){
    > > scanf("%s%d",curr->name,curr->age);

    >
    > ... but here you try to store something in curr->age.
    > Since curr->age has size zero, the address of curr->age
    > can be the same as the address of curr->next, and storing
    > a value in curr->age may overwrite whatever was in curr->next.
    >
    > > curr=(stu *)malloc(sizeof(struct stu));
    > > scanf("%d",&(curr->num));
    > > curr->next=NULL;
    > > }
    > > return 0;
    > > }
    > >


    Why the address of curr->age can be the same as the address of curr->next,
    not the address of curr-name[12],or other?
    Zhang Yuan, Jun 9, 2012
    #7
  8. Zhang Yuan

    Zhang Yuan Guest

    On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
    > On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
    > > ----------------------------------------------------------------------------
    > > #include<stdio.h>
    > > #include<stdlib.h>
    > > struct stu{
    > > int num;
    > > char name[12];
    > > int age[0];

    >
    > Didn't your compiler warn about this?
    >
    > --
    > Ian Collins


    Yes,just warn.I didn't pay much attention to that
    Thank you.
    Zhang Yuan, Jun 9, 2012
    #8
  9. Zhang Yuan

    Zhang Yuan Guest

    On Saturday, June 9, 2012 1:47:53 PM UTC+8, Ian Collins wrote:
    > On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
    > > On 2012-06-09, Zhang Yuan<> wrote:
    > >> ----------------------------------------------------------------------------
    > >> #include<stdio.h>
    > >> #include<stdlib.h>
    > >> struct stu{
    > >> int num;
    > >> char name[12];
    > >> int age[0];
    > >> struct stu *next;
    > >> };
    > >>
    > >> int main(){
    > >>
    > >> struct stu *curr,*head;
    > >>
    > >> head=(stu *)malloc(sizeof(stu));

    > >
    > > If you're really working in C and not C++, you want sizeof (struct stu)
    > > here (just like in your other malloc call below).
    > >
    > > (In C++, a struct stu { ... } declaration will introduce stu as a type name,
    > > but not in C.
    > >
    > > In C, if you have some defined object called "stu" somewhere, or a type "stu",
    > > this sizeof will measure the size of that object or type, and not "struct stu".
    > >
    > > Example:
    > >
    > > int stu;
    > >
    > > head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
    > > head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */

    >
    > Which one reason why
    >
    > head = malloc( sizeof *head );
    >
    > is a good idea!
    >
    > --
    > Ian Collins


    Sorry,I don't understand very well.
    sizeof *head ==4 0r ==sizeof struct?
    Zhang Yuan, Jun 9, 2012
    #9
  10. Zhang Yuan

    Ian Collins Guest

    On 06/ 9/12 05:59 PM, Zhang Yuan wrote:
    > On Saturday, June 9, 2012 1:47:53 PM UTC+8, Ian Collins wrote:
    >> On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
    >>> On 2012-06-09, Zhang Yuan<> wrote:
    >>>> ----------------------------------------------------------------------------
    >>>> #include<stdio.h>
    >>>> #include<stdlib.h>
    >>>> struct stu{
    >>>> int num;
    >>>> char name[12];
    >>>> int age[0];
    >>>> struct stu *next;
    >>>> };
    >>>>
    >>>> int main(){
    >>>>
    >>>> struct stu *curr,*head;
    >>>>
    >>>> head=(stu *)malloc(sizeof(stu));
    >>>
    >>> If you're really working in C and not C++, you want sizeof (struct stu)
    >>> here (just like in your other malloc call below).
    >>>
    >>> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
    >>> but not in C.
    >>>
    >>> In C, if you have some defined object called "stu" somewhere, or a type "stu",
    >>> this sizeof will measure the size of that object or type, and not "struct stu".
    >>>
    >>> Example:
    >>>
    >>> int stu;
    >>>
    >>> head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
    >>> head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */

    >>
    >> Which one reason why
    >>
    >> head = malloc( sizeof *head );
    >>
    >> is a good idea!

    >
    > Sorry,I don't understand very well.
    > sizeof *head ==4 0r ==sizeof struct?


    "sizeof *head" translates to "size of the type of head". So whatever
    type head is, malloc will return the correct size block.

    --
    Ian Collins
    Ian Collins, Jun 9, 2012
    #10
  11. Zhang Yuan

    Ian Collins Guest

    On 06/ 9/12 05:53 PM, Zhang Yuan wrote:
    > On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
    >> On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
    >>> ----------------------------------------------------------------------------
    >>> #include<stdio.h>
    >>> #include<stdlib.h>
    >>> struct stu{
    >>> int num;
    >>> char name[12];
    >>> int age[0];

    >>
    >> Didn't your compiler warn about this?

    >
    > Yes,just warn.I didn't pay much attention to that
    > Thank you.


    Valuable lesson learned then!

    --
    Ian Collins
    Ian Collins, Jun 9, 2012
    #11
  12. Zhang Yuan

    Ike Naar Guest

    On 2012-06-09, Zhang Yuan <> wrote:
    > On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
    >> On 2012-06-09, Zhang Yuan <> wrote:
    >> > struct stu{
    >> > int num;
    >> > char name[12];
    >> > int age[0];

    >>
    >> There is room for zero ages in the struct ...
    >>
    >> > struct stu *next;
    >> > };

    >>
    >> > scanf("%s%d",curr->name,curr->age);

    >>
    >> ... but here you try to store something in curr->age.
    >> Since curr->age has size zero, the address of curr->age
    >> can be the same as the address of curr->next, and storing
    >> a value in curr->age may overwrite whatever was in curr->next.

    >
    > Why the address of curr->age can be the same as the address of curr->next,
    > not the address of curr-name[12],or other?


    6.7.2.1 p13 of the standard (Structure and union specifiers) says:
    "Within a structure object, the non-bitfield members and the units in
    which bitfields reside have addresses that increase in the order in
    which they are declared."

    That means, for struct stu S,

    (char*)&S.num <= (char*)&S.name <= (char*)&S.age <= (char*)&S.next

    Since sizeof S.num and sizeof S.name are nonzero, it follows that

    (char*)&S.num < (char*)&S.name < (char*)&S.age <= (char*)&S.next

    so the only members that can have the same address are S.age and S.next .

    There can be padding between S.age and S.next, so it is
    not *required* that they have the same address.

    Just out of curiousity: why did you declare age as a zero-sized array?
    Ike Naar, Jun 9, 2012
    #12
  13. Zhang Yuan

    Ike Naar Guest

    On 2012-06-09, Ian Collins <> wrote:
    > "sizeof *head" translates to "size of the type of head". So whatever
    > type head is, malloc will return the correct size block.


    Since head has a pointer type, it would be more accurate to
    say that sizeof *head translates to "size of the type that
    head points to", instead of "size of the type of head".
    Ike Naar, Jun 9, 2012
    #13
  14. Zhang Yuan

    Zhang Yuan Guest

    On Jun 9, 2:45 pm, Ike Naar <> wrote:
    > On 2012-06-09, Zhang Yuan <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
    > >> On 2012-06-09, Zhang Yuan <> wrote:
    > >> > struct stu{
    > >> >     int num;
    > >> >     char name[12];
    > >> >     int age[0];

    >
    > >> There is room for zero ages in the struct ...

    >
    > >> >     struct stu *next;
    > >> >     };

    >
    > >> >         scanf("%s%d",curr->name,curr->age);

    >
    > >> ... but here you try to store something in curr->age.
    > >> Since curr->age has size zero, the address of curr->age
    > >> can be the same as the address of curr->next, and storing
    > >> a value in curr->age may overwrite whatever was in curr->next.

    >
    > > Why the address of curr->age can be the same as the address of curr->next,
    > > not the address of curr-name[12],or other?

    >
    > 6.7.2.1 p13 of the standard (Structure and union specifiers) says:
    > "Within a structure object, the non-bitfield members and the units in
    > which bitfields reside have addresses that increase in the order in
    > which they are declared."
    >
    > That means, for struct stu S,
    >
    >   (char*)&S.num <= (char*)&S.name <= (char*)&S.age <= (char*)&S.next
    >
    > Since sizeof S.num and sizeof S.name are nonzero, it follows that
    >
    >   (char*)&S.num < (char*)&S.name < (char*)&S.age <= (char*)&S.next
    >
    > so the only members that can have the same address are S.age and S.next .
    >
    > There can be padding between S.age and S.next, so it is
    > not *required* that they have the same address.
    >
    > Just out of curiousity: why did you declare age as a zero-sized array?


    thank you.very explicit!
    i admire you to figure much language details.
    i just made a mistake declared age as array,but i didn't notice that.
    Zhang Yuan, Jun 9, 2012
    #14
  15. Zhang Yuan

    Zhang Yuan Guest

    On Jun 9, 2:53 pm, Ike Naar <> wrote:
    > On 2012-06-09, Ian Collins <> wrote:
    >
    > > "sizeof *head" translates to "size of the type of head".  So whatever
    > > type head is, malloc will return the correct size block.

    >
    > Since head has a pointer type, it would be more accurate to
    > say that sizeof *head translates to "size of the type that
    > head points to", instead of "size of the type of head".


    i'm fairly new to c .how to read the book: the c programming, remeber
    all details in mind?
    Zhang Yuan, Jun 9, 2012
    #15
  16. Zhang Yuan

    Zhang Yuan Guest

    On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:
    > pete wrote:
    > >
    > > Zhang Yuan wrote:
    > > >
    > > > ----------------------------------------------------------------------------
    > > > #include<stdio.h>
    > > > #include<stdlib.h>
    > > > struct stu{
    > > > int num;
    > > > char name[12];
    > > > int age[0];
    > > > struct stu *next;
    > > > };
    > > >
    > > > int main(){
    > > >
    > > > struct stu *curr,*head;
    > > >
    > > > head=(stu *)malloc(sizeof(stu));
    > > > curr=head;
    > > > scanf("%d",&(head->num));
    > > > curr->next=NULL;
    > > >
    > > > while(curr->num!=0){
    > > > scanf("%s%d",curr->name,curr->age);
    > > > curr=(stu *)malloc(sizeof(struct stu));
    > > > scanf("%d",&(curr->num));
    > > > curr->next=NULL;
    > > > }
    > > > return 0;
    > > > }
    > > >
    > > > --------------------------------------------------------------------------
    > > > it doesn't work.When I debug:
    > > >
    > > > input :
    > > > 101 zhang 19
    > > >
    > > > the memory information:4 pictures
    > > > http://flic.kr/p/ccMQey
    > > >
    > > > why 0x6f17e0 change from 00-->13 unexpectedly.
    > > > I think the "*next" and "int age"storaged in different place.
    > > > but it looks like it overwrite?

    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > >
    > > struct stu{
    > > struct stu *next;
    > > int num;
    > > char name[12];
    > > int age;
    > > };
    > >
    > > int
    > > main(void)
    > > {
    > > struct stu *curr,*head;
    > >
    > > head = malloc(sizeof *head);
    > > if (head != NULL) {
    > > curr = head;
    > > puts("enter number");
    > > scanf("%d", &(head -> num));
    > > curr -> next = NULL;
    > > while(curr -> num != 0){
    > > puts("enter name and age");
    > > scanf("%s%d", curr -> name, &(curr -> age));
    > > curr = malloc(sizeof *curr);

    >
    > That doesn't make any sense!
    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > struct stu{
    > struct stu *next;
    > int num;
    > char name[12];
    > int age;
    > };
    >
    > void stu_free(struct stu *head);
    > void stu_print(struct stu *head);
    >
    > int
    > main(void)
    > {
    > struct stu *curr,*head;
    >
    > puts("/* BEGIN new.c */ output\n");
    > head = malloc(sizeof *head);
    > if (head != NULL) {
    > curr = head;
    > curr -> next = NULL;
    > puts("enter number");
    > if (1 > scanf("%d", &(head -> num))) {
    > puts("\n\nscanf problem.\n\n");
    > exit(EXIT_FAILURE);
    > }
    > while(curr -> num != 0) {
    > puts("enter name and age");
    > scanf("%s%d", curr -> name, &(curr -> age));
    > if (1 > scanf("%d", &(head -> num))) {
    > puts("\n\nscanf problem.\n\n");
    > exit(EXIT_FAILURE);
    > }
    > curr -> next= malloc(sizeof *(curr -> next));
    > if (curr -> next == NULL) {
    > break;
    > }
    > curr = curr -> next;
    > curr -> next = NULL;
    > puts("enter number");
    > scanf("%d", &(curr -> num));
    > }
    > *(curr -> name) = '\0';
    > curr -> age = 0;
    > }
    > stu_print(head);
    > stu_free(head);
    > puts("\n/* END new.c */ output");
    > return 0;
    > }
    >
    >
    > void
    > stu_free(struct stu *head)
    > {
    > struct stu *next;
    >
    > while (head != NULL) {
    > next = head -> next;
    > free(head);
    > head = next;
    > }


    Why not just free head?


    > }
    >
    > void
    > stu_print(struct stu *head)
    > {
    > while (head != NULL) {
    > printf("\nnumber == %d\n", head -> num);
    > printf("name is %s\n", head -> name);
    > printf("age is %d\n", head -> age);
    > head = head -> next;
    > }
    > }
    >
    >
    > /* END new.c */
    >
    > --
    > pete
    Zhang Yuan, Jun 9, 2012
    #16
  17. Zhang Yuan <> writes:

    > On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:

    <snip>
    >> void
    >> stu_free(struct stu *head)
    >> {
    >> struct stu *next;
    >>
    >> while (head != NULL) {
    >> next = head -> next;
    >> free(head);
    >> head = next;
    >> }

    >
    > Why not just free head?


    Do you understand what pete's code is doing? Are you saying that there
    is a simpler why to write code that does the same thing (if,write it out
    so it's clear to everyone exactly what you are suggesting), or are you
    saying that pete's function does not need to do everything that it does
    and thus can be sorter or simpler?

    >> }


    You might want to start sniping your replies so that they contain only
    the parts needed to understand your comments. It will make them easier
    to read and simpler to reply to.

    --
    Ben.
    Ben Bacarisse, Jun 9, 2012
    #17
  18. Ike Naar <> writes:
    > On 2012-06-09, Zhang Yuan <> wrote:
    >> struct stu{
    >> int num;
    >> char name[12];
    >> int age[0];

    >
    > There is room for zero ages in the struct ...

    [...]

    Actually, C doesn't permit zero-sized arrays. Declarating "int age[0]",
    is a constraint violation, requiring a diagnostic (N1570 6.7.6.2p1).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2012
    #18
  19. Zhang Yuan <> writes:
    > On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
    >> On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
    >> > #include<stdio.h>
    >> > #include<stdlib.h>
    >> > struct stu{
    >> > int num;
    >> > char name[12];
    >> > int age[0];

    >>
    >> Didn't your compiler warn about this?

    >
    > Yes,just warn.I didn't pay much attention to that


    Always pay close attention to warnings.

    gcc in particular often produces warnings for constraint violations,
    i.e., errors for which a compiler is permitted to reject the program
    altogether.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2012
    #19
  20. Zhang Yuan

    Ike Naar Guest

    On 2012-06-09, Keith Thompson <> wrote:
    > Ike Naar <> writes:
    >> On 2012-06-09, Zhang Yuan <> wrote:
    >>> struct stu{
    >>> int num;
    >>> char name[12];
    >>> int age[0];

    >>
    >> There is room for zero ages in the struct ...

    > [...]
    >
    > Actually, C doesn't permit zero-sized arrays. Declarating "int age[0]",
    > is a constraint violation, requiring a diagnostic (N1570 6.7.6.2p1).


    True. Some compilers allow it as an extension
    (e.g. gcc in default mode accepts it without a diagnostic).
    With -ansi -pedantic gcc does give a diagnostic, but still generates
    an executable.
    Ike Naar, Jun 9, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    464
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    454
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    654
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    736
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page