Ok, this is kinda C programming basics, so I'm not sure that C is necessarily the language for you to be using for GPIO programming (Python might be easier for you). If you do want to press ahead with C, and I don't want to completely steer you away from it (C coding is a valuable skill and is not going away any time soon), you need to buy a copy of "The C Programming Language" by Brian Kernighan and Dennis Ritchie (sometimes known as the K&R C book), ideally 2nd edition for the revised syntax of ANSI C. It's a small book, but it covers all of the basics of the C language. Kernighan and Ritchie didn't just write the book, Dennis Ritchie created the language (and co-created UNIX) in the first place, and the pair of them were working together on UNIX from the earliest days at Bell Labs.
Anyway, here's a quick example with a mix of global and local variables, which I hope more or less speaks for itself. Have a play with it, try making changes to it, etc. Local variables must be passed to functions as a parameters, if you want the function to be able to see them. They must be passed as a pointer to the variable if the function is to be able to manipulate the value of the variable in the calling function; or if you are passing arrays and data structures. It's usually best practice to minimise the use of global variables, as they can make it difficult to understand the flow of data in more complex code. With local variables, it's more clear how data is being passed around, but with global variables any random function can play with them when you may not have been expecting it (or may have forgotten that function used that global). Globals are often looked at as bad coding style, but they are part of the language and occasionally may be useful.
Anyway, here's a quick example with a mix of global and local variables, which I hope more or less speaks for itself. Have a play with it, try making changes to it, etc. Local variables must be passed to functions as a parameters, if you want the function to be able to see them. They must be passed as a pointer to the variable if the function is to be able to manipulate the value of the variable in the calling function; or if you are passing arrays and data structures. It's usually best practice to minimise the use of global variables, as they can make it difficult to understand the flow of data in more complex code. With local variables, it's more clear how data is being passed around, but with global variables any random function can play with them when you may not have been expecting it (or may have forgotten that function used that global). Globals are often looked at as bad coding style, but they are part of the language and occasionally may be useful.
Code:
/* * global vs local struct example */#include <stdio.h>static struct lines_struct { int a; int b;} lines1;static int n = 0;// Printing from the global structstatic void print_lines1(){ printf("%i: %i %i\n", n++, lines1.a, lines1.b);}// Printing from a pointer to a struct passed as a parameterstatic void print_lines2(struct lines_struct* lines){ printf("%i: %i %i\n", n++, lines->a, lines->b);}// func1 & func2 access a global structurestatic void func1(){ lines1.a++;}static void func2(){ lines1.b++;}// func3 & func4 access a structure through a pointer passed as a parameterstatic void func3(struct lines_struct* lines){ lines->a++;}static void func4(struct lines_struct* lines){ lines->b++;}int main(int argc, char *argv[]){ // lines1 is global lines1.a=0; lines1.b=0; // lines2 is local struct lines_struct lines2; lines2.a=10; lines2.b=20; print_lines1(); func1(); print_lines1(); func2(); print_lines1(); print_lines2(&lines2); func3(&lines2); print_lines2(&lines2); func4(&lines2); print_lines2(&lines2);}Statistics: Posted by Murph9000 — Sat Sep 28, 2024 3:41 am