Segment fault with pointer to method

J

Juggernaut

I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know
how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with
them later from main so I thought I'd make them pointers.

So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)

All this works..but when I try to get the numbers that are supposed to be in
var1 and var2 I get some long crazy numbers (like -1218520428) and I am
guessing these are the address to the int I want?

And the segment error happens when I use these in an array
(array[var1][var2]) because it gets extremely out of bounds.

But how do I get the values and not the address?

I tried grabbing them from the pointer and placing it in a new variable like
this: int var = (int)var1

But the new variable also has the huge address value

So my question is, what am I doing wrong? How to I access the actual value
thats in main from the other method and not the address?

I need the number to calculate with, and the starting number has to be
created in main.


Hope someone can help.

-Juggernaut

P.S. I can post the actual code, but I thought It wouldn't be necessary
since I know whats wrong, but just not how I do it right.
 
D

Don Morris

Juggernaut said:
I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know
how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with
them later from main so I thought I'd make them pointers.

So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)

All this works..but when I try to get the numbers that are supposed to be in
var1 and var2 I get some long crazy numbers (like -1218520428) and I am
guessing these are the address to the int I want?

So my question is, what am I doing wrong? How to I access the actual value
thats in main from the other method and not the address?

Since you're passing the addresses to method() from main, you need to
dereference the pointers in method().

i.e. method() should operate with/on *var1, *var2 to get the values
from main. As you correctly state, var1 and var2 themselves are the
addresses of var1, var2 in main's scope.

Don
 
K

Keith Thompson

Juggernaut said:
I have a program that compiles without errors, but when I run it I get a
segment fault.

I have managed to trace where it happens, but since im new to C I don't know
how to fix this or how I did it wrong.

In main lets say I have 2 variables var1 and var2 that has values that I
want to pass to a method for calculations with them. But I want to work with
them later from main so I thought I'd make them pointers.

C doesn't have "methods"; you mean you want to pass them to a function.
So I called them to a method so: method(&var1, &var2)

and the method receives them so: void method(int *var1, *var2)

void method(int *var1, int *var2)
{
printf("var1 = %p, *var1 = %d\n", (void*)var1, *var1);
printf("var2 = %p, *var2 = %d\n", (void*)var2, *var2);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top