global variable issue

Status
Not open for further replies.

brownt

Member level 3
Joined
Sep 25, 2017
Messages
63
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
597
I have a program that uses a typedef struct, and passes the address of that through to a function.

Things work ok when the type based on the struct is declared in main(), but when it is declared outside of main(), the program no longer works. there are no error messages, but now the functions within the that used the passed through struct, no longer work.

I realise may more info is required, but thought I would start with this.

thanks
 

I realize may more info is required, but thought I would start with this.
Absolutely. The question has nothing to do with typedef, typedef is just a means to declare types, including structures. The address can only refer to an actual structure instance. There's no principle difference if the variable pointer addresses a simple variable or a structure.

You may want to show how the pointer value is actually generated and how the object instance is declared.

My first guess is that you are creating a pointer of a local variable which is only valid during the lifetime of this memory object, e.g. becoming invalid after returning from the function, unless it's declared static.
 
but now the functions within the that used the passed through struct, no longer work.

You need to specify more precisely the meaning of "work", such as whether it is in the functional sense (being read - and written - somewhere) or just debugged in a watch view window. This is because some compilers make distinct optimizations when the same variable is declared global or local, sometimes simply eliminating it when it is not evaluated and/or updated anywhere.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…