+ 2

Parameter pointer retrieve function local variable address

So, before I dive into the question, here's the premise: void myFunc(int *ip) { int a = 2; *ip = a; } int main() { int catcher; myFunc(&catcher); printf("%i\n", catcher); return 0; } What I'm trying to achieve in the above example is use the pointer parameter (*ip) to store the address (point to) what the variable (a) is. Then catch the variable back in the main function, and use it. I know it's possible because I've made it work several times, but my assignment code has gotten very complex, and I'm getting segmentation fault detection from my GDB debugger. I'm pretty well versed with pointers and memory management, but this is slightly out of my scope in terms of understanding what is happening. My guess is that when the myFunc() frame stack is popped, the pointer points to nothing. Could someone help me understand and achieve this?

9th Dec 2017, 9:50 AM
Sapphire
5 Réponses
+ 5
@Sapphire, I don't know if my answer is what you intend to find, but anyways, you might want to try: ip = &a; Inside void myFunc. Hth, cmiiw
9th Dec 2017, 10:35 AM
Ipang
+ 4
@Sapphire Sorry about that, but when I tried it in Code Playground it did return the address rather than the value of a, which is 2. Can you share the message shown by your IDE? not that I know much, but just curious : )
9th Dec 2017, 10:49 AM
Ipang
+ 4
@Sapphire Looks pretty tough, I can understand what you mean, roughly, you have a Stack (collection) of structures and your Pop is used to obtain a reference to the first item in the collection, to be used somewhere else, and by returning the address to the item, then the item can be manipulated directly, is my understanding correct? As I'm pretty much a C++ noob and your code was written in C I'm afraid I wouldn't be able to assist you further, it's out of my limits, so I hope someone with knowledgeable answer can help you later. Best of luck...
9th Dec 2017, 11:31 AM
Ipang
+ 1
@Ipang That doesn't work. I'm not experienced enough with memory management to know the exact reason, but the IDE tells me it's never used. So I'm guessing it's thrown away when assigned that way?
9th Dec 2017, 10:44 AM
Sapphire
+ 1
Thread 1 received signal SIGSEGV, Segmentation fault. 0x00402454 in Pop (ip=0x1) at C:\Users\Sapphire\CLionProjects\Calc_V2\Stack.c:73 73 *ip = poppedItem; I'll explain the code slightly, I cannot post it as that wouldn't be appropriate since it's a university assignment: A variable named "item" is a struct, that contains a tags for "INT", "DOUBLE", and "STRING", and then a storage variables for all 3 are also given. The tag is to help determine what is stored in the item. The program works off the "Stack" data structure. The Pop(item *pointer) function is where i"m having issues. Pop() is supposed to take the top-most item of the stack, and in this case, use the parameter as a means of passing the popped address of the item. I know it's possible and I've done it before, but I do not understand what is happening in the background, therefore I can't fix the problem because I don't know what needs fixing. I know exactly where the fault is occurring, but I need a way to have the parameter *pointer, point to the item popped off the stack. That error you see above is just my last attempt at playing around with it.
9th Dec 2017, 11:00 AM
Sapphire