# Thread: I get segmentation error

1. ## I get segmentation error

This program asks user to enter numbers between 1 and 100. After i run it i get segmentation error. The program doesnt repeat number twice. Can someone help me.
Code:
```#include<stdio.h>
#define SIZE 20
int main(void)
{
int a[SIZE];
int i;
int x;
int j;
int flag;
int idx=0;

printf("Enter 20 numbers between 1 and 100\n");
scanf("%d",&a[i]);

//this is how to accept multiple numbers
for (int i = 0; i < 20;i++){
scanf("%d",&x);
a[i]=x;

// this is to determine that numbers dont repeat
if(i==0){
a[i]=x;
idx++;
}
else{
for( int j=0; j<idx; j++){

}

}

if(flag !=1){
a[idx]=x;
idx++;
}

printf("The no duplicate values are:\n");
printf("\n%d",x);
}

return 0;
}``` 2. Code:
`scanf("%d",&a[i]);`
You need to initialize i before you can use it. You initialize it after this point in your for loop. 3. The error is probably coming from this though:

Code:
`for (int i = 0; i < 20;i++){`
you already declared i as an int before the for loop, this should just be i = 0 (actually it shouldn't since you've already taken one value for your a array )

You also declare j twice. 4. can you specify, what you mean? 5. Code:
``` int a[SIZE];
int i;
int x;
int j;
int flag;
int idx=0;

printf("Enter 20 numbers between 1 and 100\n");
scanf("%d",&a[i]);```
You have not given a value to i, so where is scanf going to place the inputted value? &a[i] needs a value for i.

Code:
```for (int i = 0; i < 20;i++){
...
for( int j=0; j<idx; j++){```
You have already declared these variables at the beginning:

Code:
``` int a[SIZE];
int i;
int x;
int j;
int flag;
int idx=0;```
you don't need to declare them again, just give them values, i.e. i = 0, j = 0 6. Like this... i still keep getting the segmentation error
Code:
```#include<stdio.h>
#define SIZE 20

int main(void)
{
int a[SIZE];
int i;
int x;
int j;
int flag;
int idx=0;

printf("Enter 20 numbers between 10 and 100\n");
scanf("%d",&a[i]);

//this is how to accept multiple numbers
for (i = 0; i < 20;i++){
scanf("%d",&x);
a[i]=x;

// this is to determine that numbers dont repeat
if(i==0){
a[i]=x;
idx++;
}
else{
for( j=0; j<idx; j++){

}

}

if (a[j]==x){
flag=1;
}

if(flag !=1){
a[idx]=x;
idx++;
}

printf("The no duplicate values are:\n");
printf("\n%d",x);
}

return 0;
}``` 7. Code:
``` int a[SIZE];
int i;
int x;
int j;
int flag;
int idx=0;

printf("Enter 20 numbers between 10 and 100\n");
scanf("%d",&a[i]);```
&a[i] is telling scanf to place the input at the address of the 'i'th term in the 'a' array. Again, you have not supplied a value for i...so it doesn't know what part of the array you want to use. Try:

Code:
``` int a[SIZE];
int i;
int x;
int j;
int flag;
int idx=0;

printf("Enter 20 numbers between 10 and 100\n");
scanf("%d",&a);

//this is how to accept multiple numbers
//i = 1 because a has already been filled
for (i = 1; i < 20;i++){``` 8. I tried everything you told me, it still gives me segmentation error. 9. Isn't the problem that you shouldn't use the addressof operator on the array? 10. what do you mean? 11. Nevermind, I think I'm confusing things here. 12. Warning 3 warning C6001: Using uninitialized memory 'i': Lines: 6, 7, 8, 9, 10, 11, 14, 15 15
Warning 4 warning C6001: Using uninitialized memory 'j': Lines: 6, 7, 8, 9, 10, 11, 14, 15, 18, 19, 20, 23, 24, 25, 35 35
Warning 5 warning C6001: Using uninitialized memory 'flag': Lines: 6, 7, 8, 9, 10, 11, 14, 15, 18, 19, 20, 23, 24, 25, 35, 39 39
Warning 6 warning C4700: uninitialized local variable 'i' used 15

What this is saying is that you are using a lot of uninitialized variables. Popular pages Recent additions 