|
Home > Archive > Unix questions > September 2005 > segmentation fault
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
segmentation fault
|
|
| hirenshah.05@gmail.com 2005-09-24, 6:02 pm |
| hi,
i am new to unix. i am learning unix. in am program i getting
segmentation fault.
i have declare one function in header file.
i am passing structure pointer.
eg: function: int add(st *)
i am calling function in .c file and implementing the function in
another .c file.
eg of implementation:
int add(some structure x *)
{
here i want scanf one char member of structure.
scanf("%s",x->charecter);
here i am getting segmentation fault.
}
so please let me know that how can i remove this bug.
i think it is due to null pointer which i am passing.
but still i am not able to remove the error.
| |
| Ed Morton 2005-09-24, 6:02 pm |
| hirenshah.05@gmail.com wrote:
> hi,
> i am new to unix. i am learning unix. in am program i getting
> segmentation fault.
>
>
> i have declare one function in header file.
> i am passing structure pointer.
>
>
> eg: function: int add(st *)
>
>
> i am calling function in .c file and implementing the function in
> another .c file.
>
>
> eg of implementation:
> int add(some structure x *)
> {
> here i want scanf one char member of structure.
>
>
> scanf("%s",x->charecter);
>
>
> here i am getting segmentation fault.
>
>
>
> }
>
>
> so please let me know that how can i remove this bug.
> i think it is due to null pointer which i am passing.
> but still i am not able to remove the error.
>
a) Are you decalrign storage for whatever memory you're passing the
address of to "add()"? Show the calling code.
b) To test for null pointers, just add "if (x != NULL)".
c) Your structure field name is "character" but you're scanning a string
into it. Is it big enough (e.g. is it ust a poorly named array)?
Ed.
| |
| John Gordon 2005-09-26, 6:02 pm |
| In <1127579885.705437.127590@g44g2000cwa.googlegroups.com> "hirenshah.05@gmail.com" <hirenshah.05@gmail.com> writes:
> scanf("%s",x->charecter);
> here i am getting segmentation fault.
> so please let me know that how can i remove this bug.
> i think it is due to null pointer which i am passing.
> but still i am not able to remove the error.
It sounds very much like one of two things:
1. you haven't allocated storage for x
2. you haven't allocated (enough) storage for x->character.
If you want more detailed help, post your whole program.
--
John Gordon "It's certainly uncontaminated by cheese."
gordon@panix.com
|
|
|
|
|