| | |
Segmentation Error
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#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!
•
•
•
•
depends on the pass if its the first pass then 0, if second then 1, e.c.t...
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. Last edited by Aia; Jan 18th, 2009 at 10:39 pm.
>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:
However, because you use p_arr[i] 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):
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:
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:
c Syntax (Toggle Plain Text)
void enter(int *p_arr) { int insert; printf("\nplease enter the number:"); scanf("%d", &insert); *p_arr=insert; }
If you want to continue using array notation, this will solve the problem immediately (though it's technically identical to my previous fix):
c Syntax (Toggle Plain Text)
void enter(int *p_arr) { int i = 0; int insert; printf("\nplease enter the number:"); scanf("%d", &insert); p_arr[i]=insert; }
c Syntax (Toggle Plain Text)
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; }
Last edited by Narue; Jan 19th, 2009 at 9:41 am.
I'm here to prove you wrong.
If you have to use array notation, also consider using a static variable instead of a parameter:
C Syntax (Toggle Plain Text)
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:
>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:
c Syntax (Toggle Plain Text)
void enter(int *p_arr) { static int i = 0; int insert; printf("\nplease enter the number:"); scanf("%d", &insert); p_arr[i++]=insert; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Segmentation error (C++)
- Core Segmentation Error (C)
- 'Segmentation Fault' (C++)
- error in the program (C)
- string in c++,segmentation fault,thread (C++)
- segmented error?? (C)
- repost, function alogorithm problem (C)
- sorting and functions/structures (C)
Other Threads in the C Forum
- Previous Thread: about file handling
- Next Thread: Custom function help
| Thread Tools | Search this Thread |
#include * append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pause pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential single socket socketprogramming standard string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






