CST 334 < ILP < CSUMB < School < Santa Cruz Abalone Works santacruzabaloneworks.com
Home - Info - Arlon - Featured - School - People - Schools
Cabrillo | Cal Poly SLO | CSUMB
ILP
CST 231 | CST 237 | CST 238 | CST 300 | CST 311 | CST 328 | CST 329 | CST 334 | CST 336 | CST 338 | CST 363 | CST 370 | CST 383 | CST 438 | CST 462s | CST 499 | General Ed | Math 130 | Math 150 | Math 151 | Math 170

/** Arlon's C Framework - made with function pointers and nested structs.

It's a struct based program hierarchy - struct t6 defines the program heirarchy - struct t6 t5 creates and runs it. Main calls 'program' which initialilzes and calls the structs.

I learned Java for a reason, and I love hierarchical program paradigms because of how logical your programs can be - which translates to quick logical developement.

This is my current incarnation of this, partly influenced by research listed below, reading these pages, but mostly influenced by my drive to use such a paradigm for my programs.
In my experience if you don't figure out a good program structure paradigm as soon as possible your program could become a mess down the road, making it hard to maintain, and subsequently fragile.

These pages helped me figure out how to make this structure but didn't actually show directly on any of the pages, not the way this program is structured - they just helped show me how I could, and I put all the pieces together:

Google Search: c add function to struct:
https://www.google.com/search?q=c+add+function+to+struct&oq=c+add+function+to+struct&aqs=chrome..69i57.4263j0j7&sourceid=chrome&ie=UTF-8
"No, you cannot define a function within a struct in C. You can have a function pointer in a struct though but having a function pointer is very different from a member function in C++"
..."In C it is not allowed to define a method inside a struct ."

I don't mind, and that's what I went with, adding the functions in upon instantiation! They say you can't, I say, but you can!

Can I define a function inside a C structure? [duplicate]
https://stackoverflow.com/questions/12642830/can-i-define-a-function-inside-a-c-structure#:~:text=No%2C%20you%20cannot%20define%20a,to%20the%20containing%20struct%20instance.&text=In%20C%20it%20is%20not,a%20method%20inside%20a%20struct%20.

Define functions in structs
https://stackoverflow.com/questions/9871119/define-functions-in-structs

Example of how you actually CAN:
https://ideone.com/kyHlQ

This page shows exactly HOW you can:
Anonymous functions using GCC statement expressions
https://stackoverflow.com/questions/10405436/anonymous-functions-using-gcc-statement-expressions

Further encouragement and confirmation this would be doable was found on these pages:
C Nested Structure
https://fresh2refresh.com/c-programming/c-nested-structure/#:~:text=C%20Nested%20Structure,-Prev%20Next&text=Nested%20structure%20in%20C%20is,variable%20to%20access%20the%20data.

Google search: c inner struct:
https://www.google.com/search?q=c+inner+struct&oq=c+inner+struct&aqs=chrome..69i57.2599j0j7&sourceid=chrome&ie=UTF-8

Nested structure in c
https://stackoverflow.com/questions/35869873/nested-structure-in-c

Nested Structures in C
https://overiq.com/c-programming-101/nested-structures-in-c/

Nested Structure in C
https://www.javatpoint.com/nested-structure-in-c

What are nested structures in C language?
https://www.tutorialspoint.com/what-are-nested-structures-in-c-language

My blog from a few weeks ago which had a good example of console colors, but in Java:
https://arlonscsumb.blogspot.com/2021/04/arlons-csumb-java-software-design-cst_13.html

None of those are references I took any code from, just knowledge and C programming language paradigm understanding. In addition NONE of those pages show exactly how you can program in C with a hierarchical paradigm like I show here:
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*	What the include files have/do:
string.h
  (I forgot what exactly this one does already) (I'm brand new to C - the first thing I want to know is how to program it in a hierarchical way - this program is where I am with that)
stdio.h
  printf()
stdlib.h
  rand()
time.h
  time()
*/
int program(){
  
  struct t6{  /* struct based program hierarchy - struct t6 defines the program heirarchy - struct t6 t5 creates and runs it.*/
    int (*add)(int param1, int param2);
    struct inner0{}inner0_0;
    struct inner1{}inner0_1;
    struct inner2{
      struct inner3{}inner0_3;
      struct codeFights{
        struct cF0{
          int (*add)(int param1, int param2);
          int (*centuryFromYear)(int year);
          int (*checkPalindrome)(char * inputString);
        }cF00;        
      }codeFights_0;
      struct colors{
        struct console{
          struct gnulinux{
            struct ansi{  /*  color, random, reset: */
              char * (*color)(int color);
              char * (*random)(int bg);
              char * (*randomFG)();
              char * (*randomBG)();
              char * (*reset)();
            }ansi;
          }gnulinux;
        }console;
      }colors;
    }inner0_2;
    struct program{
      struct data{}data;
      struct state{}state;
      struct api{
        struct tricks{
          struct codeFights2{
            int (*adjacentElementsProduct)(int * inputArray,int sizeOfInputArray);  /*largest contiguous product in an array of two contiguous elements, ie [1,2,3] the answer is 6, [1,4,3,2] the answer is 12. */
           }codeFights2;
        }tricks;
        struct mo{
          int (*zero)();
          int (*one)();
          int (*two)();
        }mo;
      }api;
      struct init{          
        int (*zero)();
      }init;
    }program;
  };
  struct t6 t5={  /* struct based program hierarchy - struct t6 defines the program heirarchy - struct t6 t5 creates and runs it.*/
    ({  int add_implementation_ (int x, int y) { return x + y; }  add_implementation_; })  /*  A trivial example or template function - add two numbers.*/
    ,{  /*inner0 */  }
    ,{  /*inner1 */  }
    ,{  /*inner2 */{  /*inner3 */  },{  /*codeFights */{  /*cF0 */
      ({  int add_implementation_ (int x, int y) { return x + y; }  add_implementation_; })  /*  A trivial example or template function - add two numbers.*/
      ,({  int centuryFromYear_implementation_ (int year) { return 1+(year-1)/100; }  centuryFromYear_implementation_; })  /*  A nearly trivial example or template function - century from year. 2021->21, 1999->20, etc.*/
      ,({  int checkPalindrome_implementation_ (char * inputString) {       
          // printf("Line 127 checkPalindrome_implementation_");printf(inputString);
          int lengthHere(char * inputString){
            printf("Line 129: %d",(int)sizeof(inputString));
            printf("Line 130: %d",(int)sizeof(* inputString));
            return (int)sizeof(inputString)/sizeof(* inputString);
          }    //    int lengthHere(char * inputString)
          for(int i=0;i < strlen(inputString)/2;i++)
              if(!(inputString[i]==inputString[strlen(inputString)-1-i]))
                return 0;
          return 1; 
      }  checkPalindrome_implementation_; })
    }}
    ,{  /*colors */  {  /*console */  {  /*gnulinux */  {    /*ansi */
        ({  char * color (int color) { 
          // return "\u001b["+color+"m"; // There are a few ways to change colors (google search console color codes) - not exactly sure what the differences are - I just try and see what works.
          char buffer[55];
          // snprintf(buffer,55,"\u001b[%dm",color);//How to concatenate string and int in C?//https://stackoverflow.com/questions/5172107/how-to-concatenate-string-and-int-in-c
          snprintf(buffer,55,"3[0;%dm",color);//How to concatenate string and int in C?//https://stackoverflow.com/questions/5172107/how-to-concatenate-string-and-int-in-c
          // printf("%s\n",buffer);printf("<-Line 145");
          // printf("%s",buffer);printf("<-Line 146");
          // printf("\u001b46mAqua BG? -Line 147");
          char * answer=buffer;// This doesn't work without this line even though - I thought pointers were arrays?
          return answer;
        }  color; })
        ,({  char * random (int bg) { 

          time_t t;srand((unsigned) time/*time.h*/(&t));  //  2 line random number example
          printf("Line 154: %d",(int)(1+rand()%2));//prints a random number1-2

          int base=30;
          if(bg>0)base+=10;
          printf("Line 158: rand 1-2: %d base: %d",(int)(1+rand()%2),base);//prints a random number1-2
  
          int color=base+(int)(rand()%8/*stdlib.h*/);//random 0-7
          // return "\u001b["+color+"m"; 
          char buffer[55];
          // snprintf(buffer,55,"\u001b[%dm",color);//How to concatenate string and int in C?//https://stackoverflow.com/questions/5172107/how-to-concatenate-string-and-int-in-c
          snprintf(buffer,55,"3[0;%dm",color);//How to concatenate string and int in C?//https://stackoverflow.com/questions/5172107/how-to-concatenate-string-and-int-in-c
          // printf("%s\n",buffer);printf("<-Line 165");
          // printf("%s",buffer);printf("<-Line 166");
          // printf("\u001b46mAqua BG? -Line 167");
          char * answer=buffer;// This doesn't work without this line even though - I thought pointers were arrays?
          return answer;
        }  random; })
        ,({  char * randomFG () { 
          return t5.inner0_2.colors.console.gnulinux.ansi.random(0); // code reuse, random foreground
        }  randomFG; })
        ,({  char * randomBG () { 
          return t5.inner0_2.colors.console.gnulinux.ansi.random(1); // code reuse, random background
        }  randomBG; })
        ,({  char * reset () { 
          /* This is from my Java colors example: - different color codes
            String color(int what){return "\u001b["+what+"m";}
            String c(int what){return color(what);}
            String reset(){return color(0);}
            String randomBG(){return color(40+(int)(8*Math.random()));}
            String randomFG(){return color(30+(int)(8*Math.random()));}
           */
          // return "\u001b["+color+"m"; 
          char buffer[55];
          // snprintf(buffer,55,"\u001b[%dm",color);//How to concatenate string and int in C?//https://stackoverflow.com/questions/5172107/how-to-concatenate-string-and-int-in-c
          snprintf(buffer,55,"3[0m");//How to concatenate string and int in C?//https://stackoverflow.com/questions/5172107/how-to-concatenate-string-and-int-in-c
          // printf("%s\n",buffer);printf("<-Line 189");
          // printf("%s",buffer);printf("<-Line 190");
          // printf("\u001b46mAqua BG? <-Line 191");
          char * answer=buffer;// This doesn't work without this line even though - I thought pointers were arrays? I literally guessed this line out of thin air - this wasn't working without this line!!
          return answer;
        }  reset; })
     }}}}}
    ,{  /*program */
      {  /*data*/}
      ,{  /*state*/}
      ,{  /*api*/
        {  /*tricks*/
          {  /*codeFights2*/
            ({  int adjacentElementsProduct (int * inputArray,int sizeOfInputArray) {  /*largest contiguous product in an array of two contiguous elements, ie [1,2,3] the answer is 6, [1,4,3,2] the answer is 12. */
              int sz(int * inputArray){return sizeof(inputArray)/sizeof(* inputArray);}
              int biggest=inputArray[0]*inputArray[1];
              printf("\nLine 205: The initial value for biggest: %d",biggest);
              printf("\nLine 206: sz(inputArray): %d",sizeOfInputArray);
              for(int i=1;i<sizeOfInputArray-1;i++)
                if(inputArray[i]*inputArray[i+1]>biggest)
                  biggest=inputArray[i]*inputArray[i+1];              
              return biggest;
            }  adjacentElementsProduct; })
          }
        }
        ,{  /*mo*/  
           ({  int zero () { //t5.program.api.mo.zero();
            
            int add(int param1, int param2) {	// just adding two numbers together
              return param1+param2;
            }
          
            struct trouble{  //  good trouble - try to make something big!
              int (*add)(int param1, int param2);
              // int add(int param1, int param2) {
                // return param1+param2;
              // }  //  I never did figure out how to define with the functions already in them but if I define then I can instantiate with functions for some reason.
            };
                        
            //struct add #0
            struct trouble t1={add};
            printf("Line 230: trouble t1.add(1,2):%d\n",t1.add(1,2));
            
            //struct add #1 annonymous add
            struct trouble t2={
              ({  int add_implementation_ (int x, int y) { return x + y; }  add_implementation_; })
             
            
            };
            printf("Line 238: trouble t2.add(1,3):%d\n",t2.add(1,3));
            
            //struct add #2 annonymous add locally defined
            struct t4{int (*add)(int param1, int param2);};struct t4 t3={

              ({  int add_implementation_ (int x, int y) { return x + y; }  add_implementation_; })
            
            };
            printf("Line 246: trouble t3.add(1,4):%d\n",t3.add(1,4));

            return 1; 
          }  zero; })
          ,({  int one () { //t5.program.api.mo.one();
            printf("\nLine 251: t5.inner0_2.codeFights_0.cF00.centuryFromYear(2021): %d\n",t5.inner0_2.codeFights_0.cF00.centuryFromYear(2021));
            printf("\nLine 252: t5.inner0_2.codeFights_0.cF00.centuryFromYear(1998): %d\n",t5.inner0_2.codeFights_0.cF00.centuryFromYear(1998));
            printf("Line 253: t5.inner0_2.codeFights_0.cF00.checkPalindrome(\"abba\"): %d\n",t5.inner0_2.codeFights_0.cF00.checkPalindrome("abba"));
            printf("Line 254: t5.inner0_2.codeFights_0.cF00.checkPalindrome(\"abbcs\"): %d\n",t5.inner0_2.codeFights_0.cF00.checkPalindrome("abbcs"));
            printf("%sLine 255: Hello? \n",t5.inner0_2.colors.console.gnulinux.ansi.color(46)); // 46 is aqua color background
            printf("%sLine 256: Hello in a Random Color? \n",t5.inner0_2.colors.console.gnulinux.ansi.randomFG());
            printf("Line 257: Hello?");
            printf("%sLine 258: Color Reset? \n",t5.inner0_2.colors.console.gnulinux.ansi.reset());
            return 1; 
          }  one; })
          ,({  int two () { //t5.program.api.mo.two();
             
            //....(Next test branch...)
        
            return 1; 
          }  two; })
        }
      }
      ,{  /*init*/
          ({  int zero () { //t5.program.init.zero();
          
            // t5.program.api.mo.zero();
            t5.program.api.mo.one();
            // t5.program.api.mo.two(); // ...(next test branch)
            
            return 1; 
          }  zero; })      
      }
    }
  };
  t5.program.init.zero();
  
}
int main(){
  program();
}  // (Yes, this all compiles and runs as expected - the question is - does it cause any memory holes? Or anything bad? (It’s inner structs with function pointers - this is C))
(That is not hilitable so it scrolls different - keyboard arrow Keys will scroll it right and left, after clicking it.) That is my C Framework made with function pointers and nested structs I made at the beginning of the class to test C syntax and to get a framework I could use later if I ever wanted to build anything huge with C. It seems to work. I'm still not a C expert though, so I don't know all the implications of using this like if it causes memory leaks, or what. Seems to work fine, but yeah, use at your own risk, I just made it for myself. The html displaying it here was rendered with a code-hiliting-with-html Perl program I made (partly) for Perl class at Cabrillo.

CST 334 - Introduction to Operating Systems - Units - 4

Students in this course will learn about the use and design of modern operating systems, focusing on Linux. On the “use” side, students will learn the Linux command line, to write shell scripts, and to build programs with GNU utilities like awk, sed, and make. On the “design” side, students will develop a deep understanding of process management, memory management, file systems, and concurrency, and how they apply to modern technologies like virtualization and cloud computing.

Prerequisite(s)/Corequisite(s): (Prereq: CST 238 and MATH 130 with a C- or better)

Typically Offered: Fall, Spring

Units: 4

My Experience in CST-334 - Introduction to Operating Systems:

I would say that is all true, a little too true maybe. What I was really hoping for was practical knowledge I guess. How you can get past dependency problems when apt chokes and you really need a certain software. I know you can force it but I always break everything and I'm not sure exactly how to get past it when I break everything without just starting over from ISO, Knoppix makes that super easy but I just want to know how you're supposed to fix it when you break it. Also I'd really like to know how to understand how the init system works and how to install various layers of an OS from just a kernel to a desktop environment. How to implement an army of thin clients running X off a more expensive central server. So those things were not what that class was, it was more like trying to teach us the underlying logic of memory and processor management. I could appreciate the topics almost but the way it was layed out was to demonstrate tons of wrong methodologies ahead of the final solution of a correct methodology and it made for a really confusing mess of ideas in the end that was, for me, difficult to conceptually and logically connect to anything I could really grab and use later - but the labs were useful C programs we could grab concepts from and use later and I got a few good bash commands out of it I didn't know like environment variables I didn't know were present in bash.

I am a huge fan of computing in general just because it is so deep you can literally think of millions of tasks to use computers robots and electronics for. So the knowledge seems obscure to me now but I'm sure in general it will be great to have. I did use the time to do some good experimenting with C and with bash.

Here are links to my journals:

Links:
I put lots of example knowledge in my journals as I came across it. Here are some screenshots from class work:

Screenshot from CST-334 Intro to Operating Systems
Me experimenting with C syntax

Screenshot from CST-334 Intro to Operating Systems
Working on a Bash assignment

Screenshot from CST-334 Intro to Operating Systems
A threads assignment

More thread assignments:
Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems
A matrix thread assignment

Screenshot from CST-334 Intro to Operating Systems

Screenshot from CST-334 Intro to Operating Systems
Fixing the matrix thread assignment

Screenshot from CST-334 Intro to Operating Systems
Locks assignment

Screenshot from CST-334 Intro to Operating Systems
Locks assignment

Screenshot from CST-334 Intro to Operating Systems
Revisiting the matrix thread assignment
List of paradigms I practiced during the class:
  • Lots of practice with C threads
  • C examples
  • I made up my own C framework (see my 25th Journal)
  • Locks
  • Semaphores
  • basic Bash scripting
  • make-files
  • more

And, our group video we made, research paper on the USRA space association:
USRA by Team 5 - https://youtu.be/VdmpvsWU3cU

       Powered by       Santa Cruz Web Factory