How do I access variables in C from different functions? -
my question how function malloc_queue() can access variables init_queue() without giving arguments!
for example:
the main.c:
if (init_queue()) { malloc_queue() } init_queue() creates variable que:
int init_queue{ struct queue *que; return 1; } malloc_queue() want variable que init_queue():
void malloc_queue{ struct queue *que = (struct queue*)malloc(sizeof(struct queue)); return; } but doesnt work since malloc_queue doesnt know que is. there possible ways without giving arguments?
maybe want static variable outside functions, has scope inside residing file after definition.
check code:
#include <stdio.h> static int x = 0; void a() { x = 5; } void b() { x*=2; } int main(int argc, char * argv[]) { printf("%d\n", x); a(); printf("%d\n", x); b(); printf("%d\n", x); return 0; } just comments suggested, read c scopes.
if need declare struct other primary types, might need declare pointer structure static variable outside function, allocate memory (e.g malloc()) inside 1 of functions.
Comments
Post a Comment