| | |
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
>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.
New members chased away this month: 5
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; }
New members chased away this month: 5
![]() |
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
Views: 695 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






