#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!

Dani AI

Generated

This crash is classic undefined behavior caused by using an indeterminate local variable inside enter. As and noted, the function reads an uninitialized i and then indexes p_arr with it. Undefined behavior can appear to "work" on one machine (your Mac) and crash on another (the school server) because stack layout and memory contents differ.

Why that happens: when you pass &arr[i] into enter, the pointer points at a valid element. But indexing that pointer with an uninitialized index can produce a huge offset and write outside allocated memory, which sometimes hits unmapped memory and raises SIGSEGV. That explains the different outcomes across environments.

Practical fixes and checks:

  • Initialize every automatic variable before use, or avoid the extra index entirely and store via the pointer that was passed. Alternatively, pass the array and a valid index into enter. Avoid using a static counter inside the function for this purpose — as suggested, it breaks reusability and thread-safety.
  • Validate user input: check scanf return values (or prefer fgets + strtol), and check bounds before writing to an array.
  • Compile and test with warning/sanitizer tools to catch these mistakes early. Example compile command (AddressSanitizer):
gcc -std=c11 -Wall -Wextra -pedantic -g -fsanitize=address program.c -o program

Running under AddressSanitizer or Valgrind will point directly at invalid reads/writes. Also make main return an int and return 0. These practices will prevent the intermittent segmentation faults and help understand why the program behaved differently on different machines.

Recommended Answers

All 8 Replies

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

>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;
}

Point taken, sorry.

thank you guys so much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.