Find global variables, c source

Hello.I have been trying to solve the following problem, but to no avail. If anyone could please give me some indications, or anything, it would be amazing.
A C source program and a type name are given. Determine from source,
the list of the global variables having the given type.
For each variable from the list, determine the number of occurences
and the list of lines where it is used.

Is this homework?

not really. it's just me trying to understand better shell scripting.

Hmmmm. The only difference between local and global variables is where the variable is stored, so this gets tricky.

I'd start by mangling the source code into a more convenient form -- deleting all things in /* */, //, " " so they don't cause false matches, making every { } happen on its own line, forcing lines to break on ; etc etc so it's easier to match with tools like grep. Then you can count the brackets to tell whether you're inside or outside a function...

This works, but also finds function declarations:

# Strip out preprocessor statements, remove all comments
<mycode.c sed '/^#/d;s#//.*$##;s#/\*.*\*/##g;#/\*#,#\*/#d' |
# Flatten all remaining whitespace
        tr -s '[ \r\n\t]' ' ' |
# Put new newlines where appropriate
        sed 's/\([{}]\) /\n\0\n/g;s/; */;\n/g' |
# Count brackets, find lines beginning with 'int'
        awk '/{/ { B++; }; /}/ { B-- };  (!B) && /^((static|extern|const) )*int/'

...and since you can declare functions and variables in the same statement, that's going to be very hard to prevent...

hello.
I have been trying to solve the same problem but i don't understand the explication of the Corona668. Please can explain more simply like a beginner or Susan78, if you solved the problem can post this, with little explication. Thank you