-5

I'm new to using c and I'm trying to make a program that makes an array using pointers,

#include <stdio.h>
int main()
{
  int *i;
  scanf("%d",i);i++;
  while(1){
    scanf("%d",i);
    if(*i==*(i-1)){
     break;}
    printf("%d\n",*i);
    i++;
  }
 return 0;
}

I Keep getting this error

Command failed: ./a.out Segmentation fault

J...S
  • 5,079
  • 1
  • 20
  • 35

3 Answers3

0

I think you want to create an array and read data to it and later display it using pointers dynamically.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *A, n;

    printf("\n Enter the size of array : "); // Reading the size of array
    scanf("%d",&n);

    A = (int*) malloc(sizeof(int) * n); // Allocating memory for array 

    int i = 0;

    for(i = 0; i < n; i++) // Reading data to array
        scanf("%d", (A+i));

    // Operations on array

    for(i = 0; i < n; i++) // Printing array
        printf("%d ", A[i]);
    return 0;
}

Hope this help.!!

Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
0

just a bit of explanation here. You are declaring your i variable as a pointer here:

int *i;

The pointer does not point anywhere and contains a random value. The following operation tries to write an integer in the memory pointed by the pointer. Since it points to an undefined location, the result of this operation is unpredictable. It can crash, or can write in a place of memory which could create unexpected behavior later, or just work. In any case it causes memory corruption.

scanf("%d",i);i++;

The i++ statement actually increases the value of the pointer so that it points to the next place in memory, which would also be invalid. And so on.

Depending on the purpose of your program you can work around this issue in multiple ways. i.e. if you need just a single integer to work with , you can do the following:

int i;
scanf("%d", &i); // use an address of 'i' here
...
printf("%d", i);

now you can use the 'i' in normal arithmetic operations then. Or if you need an array of integers, you can do the followig:

int i = 0;
int a[mysize];
scanf("%d", &a[i]);
i++; // do not forget to check 'i' against 'mysize'
...
printf("%d", a[i]);

or have 'i' as a pointer:

int a[mysize];
int *i = a;
scanf("%d", i);
i++; // do not forget to check 'i' against 'mysize'
...
printf("%d", *i);

or even have the array allocated in memory by malloc, as this:

int *a = malloc(sizeof(int) * mysize);
int *i = a;
scanf("%d", i);
i++; 
...
printf("%d", *i);

Note, that at some point you would need to free the memory in the last example. So, you'd better keep the pointer to the beginning of the array to be able to do free(a);

Serge
  • 11,616
  • 3
  • 18
  • 28
-1

edits prefixed with @@

after cleaning up the code formatting:

  1. to consistently indent the code
  2. so it follows the axiom: only one statement per line and (at most) one variable declaration per statement.

Then it is obvious that the code contains undefined behavior. (see the @@ comments in the code)

@@ each of the statements containing undefined behavior can cause a seg fault event.

#include <stdio.h>

int main( void )
{
    int *i;        @@ uninitialized pointer declared

    scanf("%d",i); @@ accessing random memory I.E. undefined behavior
    i++;      

    while(1)
    {
        scanf("%d",i);   @@ accessing random memory I.E. undefined behavior

        if(*i==*(i-1))  @@ reads two consecutive integers using uninitialized pointer  I.E. undefined behavior
        {
            break;
        }

        printf("%d\n",*i);  @@ reads integer from memory using uninitialized pointer  I.E. undefined behavior
        i++;
    }
    return 0;
}

The undefined behavior, from accessing of memory that the program does not own, is why the seg fault event occurs.

user3629249
  • 16,402
  • 1
  • 16
  • 17