newbie Q, segmentation fault

Discussion in 'C Programming' started by Elinore, Mar 3, 2005.

  1. Elinore

    Elinore Guest

    hi

    I am practicing whole structure passing.
    Array 'c' add two arrays 'a' and 'b' in 'sum' function..
    Result is okay, but see segmentation fault (core dumped). Can anyone
    help me with this? Thanks


    #include <stdio.h>
    #include <time.h>

    #define N 10
    #define Rand_MAX 5

    struct par{
    int a[N];
    int b[N];
    int c[N];
    };
    void sum(struct par *);

    main()
    {
    int i;
    struct par *inte;

    srand((unsigned)time(NULL));

    for(i=0;i<N;i++){
    inte->a = (int)rand() % Rand_MAX;
    inte->b = (int)rand() % Rand_MAX;
    printf("a[%d]=%d\n",i,inte->a);
    printf("b[%d]=%d\n",i,inte->b);
    }

    sum(inte);

    for(i=0;i<N;i++)
    printf("c[%d]=%d\n",i,inte->c);
    }


    void sum(intel)
    struct par *intel;
    {
    int i;
    for(i=0;i<N;i++)
    intel->c=intel->a + intel->b;

    }
     
    Elinore, Mar 3, 2005
    #1
    1. Advertising

  2. Elinore

    Eric Sosman Guest

    Elinore wrote:
    > hi
    >
    > I am practicing whole structure passing.
    > Array 'c' add two arrays 'a' and 'b' in 'sum' function..
    > Result is okay, but see segmentation fault (core dumped). Can anyone
    > help me with this? Thanks
    >
    >
    > #include <stdio.h>
    > #include <time.h>
    >
    > #define N 10
    > #define Rand_MAX 5
    >
    > struct par{
    > int a[N];
    > int b[N];
    > int c[N];
    > };
    > void sum(struct par *);
    >
    > main()
    > {
    > int i;
    > struct par *inte;


    `inte' is a variable that can point to `struct par'
    objects. Fine. However, `inte' has not been given any
    value yet, so (just like `i') its value is indeterminate;
    it's a "garbage pointer."

    > srand((unsigned)time(NULL));
    >
    > for(i=0;i<N;i++){
    > inte->a = (int)rand() % Rand_MAX;


    .... and here you use the "garbage pointer" to reference
    the non-existent `struct par' object that it doesn't
    point to. All bets are off; anything at all can happen.
    If you're lucky, your program will crash.

    > [remainder snipped, and not examined for further trouble]


    --
     
    Eric Sosman, Mar 3, 2005
    #2
    1. Advertising

  3. Elinore wrote:
    > hi
    >
    > I am practicing whole structure passing.
    > Array 'c' add two arrays 'a' and 'b' in 'sum' function..
    > Result is okay, but see segmentation fault (core dumped). Can anyone
    > help me with this? Thanks


    This is the usual problem of a pointer never initialized to point to
    anything.
    Below are first a changelog, then a modified version of your code. The
    original code is at EOM.

    [changes]
    4a5
    > #include <stdlib.h>

    17c18
    < main()
    ---
    > int main(void)

    20c21
    < struct par *inte;
    ---
    > struct par inte;

    25,28c26,29
    < inte->a = (int) rand() % Rand_MAX;
    < inte->b = (int) rand() % Rand_MAX;
    < printf("a[%d]=%d\n", i, inte->a);
    < printf("b[%d]=%d\n", i, inte->b);
    ---
    > inte.a = Rand_MAX * (rand() / (1. + RAND_MAX));
    > inte.b = Rand_MAX * (rand() / (1. + RAND_MAX));
    > printf("a[%d]=%d\n", i, inte.a);
    > printf("b[%d]=%d\n", i, inte.b);

    31c32
    < sum(inte);
    ---
    > sum(&inte);

    34c35,36
    < printf("c[%d]=%d\n", i, inte->c);
    ---
    > printf("c[%d]=%d\n", i, inte.c);
    > return 0;

    38,39c40
    < void sum(intel)
    < struct par *intel;
    ---
    > void sum(struct par *intel)


    [resulting code]
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    #define N 10
    #define Rand_MAX 5

    struct par
    {
    int a[N];
    int b[N];
    int c[N];
    };
    void sum(struct par *);

    int main(void)
    {
    int i;
    struct par inte;

    srand((unsigned) time(NULL));

    for (i = 0; i < N; i++) {
    inte.a = Rand_MAX * (rand() / (1. + RAND_MAX));
    inte.b = Rand_MAX * (rand() / (1. + RAND_MAX));
    printf("a[%d]=%d\n", i, inte.a);
    printf("b[%d]=%d\n", i, inte.b);
    }

    sum(&inte);

    for (i = 0; i < N; i++)
    printf("c[%d]=%d\n", i, inte.c);
    return 0;
    }


    void sum(struct par *intel)
    {
    int i;
    for (i = 0; i < N; i++)
    intel->c = intel->a + intel->b;

    }


    [original code]
    >
    > #include <stdio.h>
    > #include <time.h>
    >
    > #define N 10
    > #define Rand_MAX 5
    >
    > struct par{
    > int a[N];
    > int b[N];
    > int c[N];
    > };
    > void sum(struct par *);
    >
    > main()
    > {
    > int i;
    > struct par *inte;
    >
    > srand((unsigned)time(NULL));
    >
    > for(i=0;i<N;i++){
    > inte->a = (int)rand() % Rand_MAX;
    > inte->b[i] = (int)rand() % Rand_MAX;
    > printf("a[%d]=%d\n",i,inte->a[i]);
    > printf("b[%d]=%d\n",i,inte->b[i]);
    > }
    >
    > sum(inte);
    >
    > for(i=0;i<N;i++)
    > printf("c[%d]=%d\n",i,inte->c[i]);
    > }
    >
    >
    > void sum(intel)
    > struct par *intel;
    > {
    > int i;
    > for(i=0;i<N;i++)
    > intel->c[i]=intel->a[i] + intel->b[i];
    >
    > }
    > [/i][/i][/i][/i][/i][/i][/i]
    [i][i][i][i][i][i][/i][/i][/i][/i][/i][/i]
     
    Martin Ambuhl, Mar 3, 2005
    #3
  4. Elinore

    Jack Guest

    hi

    it is simple and i was careless...
    thankyou for comment and correction
     
    Jack, Mar 4, 2005
    #4
  5. Elinore

    CBFalconer Guest

    Jack wrote:
    >
    > it is simple and i was careless...
    > thankyou for comment and correction


    Another of these silly orphan and useless posts. What is simple,
    what comment, what correction, etc. See sig. below. Some people
    really do manage to use google without posting such nonsense.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Mar 4, 2005
    #5
    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. Alex Hunsley
    Replies:
    17
    Views:
    885
  2. Pud
    Replies:
    0
    Views:
    602
  3. Replies:
    0
    Views:
    554
  4. Hendrik Maryns

    Newbie needing some help with segmentation fault

    Hendrik Maryns, Dec 4, 2007, in forum: C Programming
    Replies:
    20
    Views:
    874
    Hendrik Maryns
    Dec 7, 2007
  5. Replies:
    5
    Views:
    1,284
    Terry Reedy
    Jul 18, 2008
Loading...

Share This Page