Code:
void test(int *i) {
*i = 3; /* where the sig fault occurs */
}
int main() {
int *p;
test(p);
}
|
Newbie Member
|
|
| 24Aug2009,22:21 | #1 |
|
For the following code, I receive sig fault. Why? Thanks.
Code:
void test(int *i) {
*i = 3; /* where the sig fault occurs */
}
int main() {
int *p;
test(p);
}
|
|
Contributor
|
|
| 25Aug2009,00:26 | #2 |
|
see friend as per my logic u cannot assign a no. to a pointer so its showing u the fault....
>>*i=3 is the wrong statement.... hav a gud day... |
|
Newbie Member
|
|
| 25Aug2009,03:13 | #3 |
|
actually, I wasn't assigning a number to a point. *i is dereferencing a pointer. *i = 3 is to let i point to value 3. I'm still confused why this caused the seg fault.
This is a simplified version of the problem. Originally, I wanted to manipulate a string parameter in a function. It seems as long as I change the value pointed by a pointer (this pointer is a parameter to a function), seg fault appears... |
|
Go4Expert Member
|
|
| 25Aug2009,10:22 | #4 |
|
Since it is not initialized with valid address and you are accessing that address to modify the value, the segmentation fault occurs! Just allocate some valid address and try... it should work.
|
|
~ Б0ЯИ Τ0 С0δЭ ~
|
![]() |
| 25Aug2009,12:44 | #5 |
|
Quote:
Originally Posted by c_user Quote:
Originally Posted by Amar_Raj ![]() You are de-referencing a pointer which is not assigned to any address. It's always a good practice to check the pointer b4 assigning values to it. Like this : Code: C++
|
|
Newbie Member
|
|
| 25Aug2009,21:24 | #6 |
|
Thank you all!
|