0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

st2.c. puzzle book

Last updated at Posted at 2025-06-18
st2.c
#include "defs.h"

struct S1 {
        char *s;
        int i;
        struct S1 *s1p;
};

void main(){
        static struct S1 a[] = {
                { "abcd", 1, a+1 },
                { "efgh", 2, a+2 },
                { "ijkl", 3, a }
        };
        struct S1 *p = a;                                               //Structures 2.1
        int i;

        PRINT3(s, a[0].s, p->s, a[2].s1p->s);   //Structures 2.2

        for( i=0; i<2; i++ ) {
                PR(d, --a[i].i);
                PR(c, ++a[i].s[3]);                                     //Structures 2.3
                NL;
        }

        PRINT3(s, ++(p->s), a[(++p)->i].s, a[--(p->s1p->i)].s);
         //Structures 2.4
}
bash
$ gcc st2.c
$ ./a.out
a[0].s = abcd   p->s = abcd     a[2].s1p->s = abcd
Segmentation fault (core dumped)
``

```c:st2a.c
#include "defs.h"

struct S1 {
        char *s;
        int i;
        struct S1 *s1p;
};

void main(){
        static struct S1 a[] = {
                { "abcd", 1, a+1 },
                { "efgh", 2, a+2 },
                { "ijkl", 3, a }
        };
        struct S1 *p = a;                                               //Structures 2.1
        int i;

        PR(s, a[0].s);
        PR(s, p->s);
        PR(s,   a[2].s1p->s);

        PRINT3(s, a[0].s, p->s, a[2].s1p->s);   //Structures 2.2

        for( i=0; i<2; i++ ) {
                PR(d, --a[i].i);
                PR(c, ++a[i].s[3]);                                     //Structures 2.3
                NL;
        }
        PR(s, ++(p->s));
        PR(s,  a[(++p)->i].s);
        PR(s, a[--(p->s1p->i)].s);




        PRINT3(s, ++(p->s), a[(++p)->i].s, a[--(p->s1p->i)].s);
                                                                                        //Structures 2.4
}
$ gcc st2.c
inst19@teachB:~$ ./a.out
a[0].s = abcd   p->s = abcd     a[2].s1p->s = abcd      a[0].s = abcd   p->s = abcd     a[2].s1p->s = abcd
Segmentation fault (core dumped)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?