#include<stdio.h>
#define COUNT 5


void enter(int *p_arr);
void report(int arr[], int count);

int main()
{
	
	
	int arr[5];
	int i;
	for(i=0; i < COUNT; i++)
	{
		enter(&arr[i]);
	}
	
	report(arr, COUNT);
	
	
}   

void enter(int *p_arr)
{
	int i;
	int insert;
	printf("\nplease enter the number:");
	scanf("%d", &insert);
	p_arr[i]=insert;
}


void report(int arr[], int count)
{
	int i;
    printf("The numbers you have entered are:");
	for(i=0; i< count; i++)
		printf("\n%d", arr[i]);
}

Hello Guys.,
I'm new and learning C, when i compiled and ran this program on my mac in bash using gcc, it works... on our school server however it compiles but after the first entry it sais "Segmentation Error" and quits, any thoughts would be greatly appreciated!
Thanx!

>p_arr=insert;
Tell me the value of i in this line of code.

depends on the pass if its the first pass then 0, if second then 1, e.c.t...

depends on the pass if its the first pass then 0, if second then 1, e.c.t...

Nope. That i has the value of "segmentation fault".
It never gets initialized.
If you want to pass it a value you need to do it as an argument. void enter(int *p_arr, int i); Make sure the passed int contains a meaningful value.

>depends on the pass if its the first pass then 0, if second then 1, e.c.t...
Bzzt! Wrong. The correct answer is "I don't know". Though I would have accepted "indeterminate" as well. Technically you don't need i at all in that function because you pass the address of the correct array element:

void enter(int *p_arr)
{
  int insert;
  printf("\nplease enter the number:");
  scanf("%d", &insert);
  *p_arr=insert;
}

However, because you use p_arr where i is indeterminate (usually this means a large number), then you're pretty sure to index the array well out of its bounds.

If you want to continue using array notation, this will solve the problem immediately (though it's technically identical to my previous fix):

void enter(int *p_arr)
{
  int i = 0;
  int insert;
  printf("\nplease enter the number:");
  scanf("%d", &insert);
  p_arr[i]=insert;
}

Note that i is always 0, because p_arr always points to the element you want to initialize. You can also change your logic as Aia suggested such that the index actually makes sense:

int main()
{
  int arr[5];
  int i;

  for ( i = 0; i < COUNT; i++ )
    enter ( arr, i );

  report ( arr, COUNT );

  return 0;
}   

void enter ( int *p_arr, int i )
{
  int insert;

  printf ( "\nplease enter the number:" );
  scanf ( "%d", &insert );
  p_arr[i] = insert;
}

If you have to use array notation, also consider using a static variable instead of a parameter:

void enter(int *p_arr)
{
  static int insert = 0;
  printf("\nplease enter the number:");
  scanf("%d", &insert);
  p_arr[i++]=insert;
}

>If you have to use array notation, also consider
>using a static variable instead of a parameter
That's a horrible suggestion. You've managed with one change to ruin the function's flexibility, potential error handling, actual error handling, and thread safety. Congratulations, this gets my vote for the most damaging minor change of the week award.

p.s. Your code is broken, so I mentally changed it to this:

void enter(int *p_arr)
{
  static int i = 0;
  int insert;
  printf("\nplease enter the number:");
  scanf("%d", &insert);
  p_arr[i++]=insert;
}
This question has already been answered. Start a new discussion instead.