newbie Q, segmentation fault

E

Elinore

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;

}
 
E

Eric Sosman

Elinore said:
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]
 
M

Martin Ambuhl

Elinore said:
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);
> 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);
> 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 = (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;

}
 
C

CBFalconer

Jack said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top