C Video Tutorial 6

[ad_1]
Get the Code Here :

Welcome to part 6 of my C video tutorial. Today I’m going to cover Unions, Enumerated Types, the Designated Initializer, Using unions in Structs, Recursive Structures, Linked Lists and much more.

I’m also going to experiment with a new style that is more interactive and I hope it feels more like a classroom setting. Throughout the tutorial I will constantly insert brain teasers. Hopefully they aren’t distracting. I do this every once in a while to try and improve.


Posted

in

by

Tags:

Comments

34 responses to “C Video Tutorial 6”

  1. Tech Zombie Avatar

    So are names that you refer to your types by are actually substitutes for the memory address of the type/variables? It's almost as if the compiler adds a 'define' statement to the beginning of the code, defining the name as a specific address in memory.(or it just does this at runtime) Thinking of it like this really seems to help understand how variables work in OOP languages.

    Before, my thought process was more along the lines of: classes and functions are like folders filled with files (program variables addressed to segments in memory) and the language's API requests and creates the files to be stored, which left me questioning the limitations of OOP languages. I suppose if someone ever misunderstands the nature of higher level languages as I did, it would help for them to learn about the similarities between C and assembly.

  2. Rubik's Cubed Avatar

    Okay, question. My IDE is telling me that amount orangeAmt = {.ounce = 16}; is an "invalid initializer for scalar type". I am wondering what this means and why it seems to think this is wrong when it will compile with no errors. In addition, my compiler won't even let me initialize a union like amount orangeAmt.individual = 4;. I can, however, do orangeAmt.individual = 4; if my union has already been initialized once using your first method.

  3. swarnadipmitra Avatar

    Some of the modern C and C++ compilers no longer support designated initializers. Good thing you brought it up. Most people don't study that these days.

  4. Andrew Riley Avatar

    for assigning the next pointer in the LL, couldn't you also use this? or is it not equivalent? I read it as "I'm assigning the address of potato to the tomato (pointer "->") next.

    tomato->next = &potato;
    potato->next = &lemon; //etc

  5. Rohit Patil Avatar

    this video is very heavy!! Like its helpful but difficult to understand

  6. Jiaxing Liu Avatar

    Terrific instruction. I think adding the explanation about how the different fields in union occupy the same memory space and get misinterpreted into each other will make it much more easier to comprehend.

  7. Saxon Avatar

    Wouldn't a more elegant way of writing the last function be this?
    void printLinkedList(product *pProduct){
    printf("Name: %s Price: %fn", pProduct->name, pProduct->price);
    if(pProduct->next != NULL)
    printLinkedList(pProduct->next);
    }

  8. Denis Otim Avatar

    Why use union when you can use struct???

  9. Abdulnaser Sheikh Avatar

    Is union like Object()?

  10. The Punk Programmer Avatar

    what was the answer to the homework

  11. Nick boi Avatar

    I'm kind of confused. I know that you use an array of characters to make a string and that you use an asterisk(*) to mark something referring to the value of a pointer, but why does "char *name;" work, and what does it even mean?

  12. Sky King Avatar

    since they all point to the same adress, is it recommended to simply use "amountUnion" instead of "amountUnion.ounce" for example.

  13. ScarfedMunchlax Avatar

    This is mindbogglingly

  14. Michael Ts Avatar

    anyone ever tell you that you sound like Ed Helms from The Hangover?

  15. Jeremy Su Avatar

    I did some homework and testing at 10:34. If I try to call back the ounce or pound variable, then terminal will output the most recently set value of the two, regardless of assignment to ounce or pound. I suspect this is because, as you've warned, a union can only store one value at a time, and thus can only store one of these two seemingly distinct floats. Is this correct? Can someone help me out here?

  16. eth swe Avatar

    THE BEEEEEEEEEEEEESSSSSSSSSSSSSSSSTTTTTTTTTTTTT!!!!!!!! helped me understand inked lists a lot. Thank you.

  17. ollemonster Avatar

    When i changed the 5:50 example number of oranges to f it just said
    Orange juice amount: 16.00 : size: 4
    Number of oranges: 0.000000 : size: 4
    Not sure why because so far I don't really know what I am doing 馃檪

  18. Adam&Dylan Avatar

    Hey Derek, really like your tutorials, but why was I required to format sizeof(orangeAmt.ounce) as %lu, but you were able to format as %d.

    I got a format error when compiling, so I had to use %lu for a long unsigned int.

    I was using your exact code as well.

  19. ProgrammingExperts Avatar

    I couldn't understand what you where trying to do with unions, since you initialized orangeAmt.individual = 4 and in you sample at the same time you are using enum to print out on the terminal 聽oorangeAmt.once – Long story short it doesn't make sens what you are trying to do

  20. Bien Got Avatar

    concise, informative, understandable

  21. 讜讬拽讟讜专 讬讗诪驻讜诇住拽讬 Avatar

    Hi there.
    I'm trying to apply what i learned here into some code of mine… it's not working. it's compiled OK but it looks like a pointer problem:

    typedef struct product{
    聽 聽 聽 聽 char*name;
    聽 聽 聽 聽聽float price;
    聽 聽 聽 聽聽struct product *next;
    }product;

    void reverse(product *first)
    {
    聽 聽 聽 聽聽product 聽Curr = (*first) ;
    聽 聽 聽 聽聽product Next = (*Curr.next) ;
    聽 聽 聽 聽聽product Loop;
    聽 聽 聽 聽聽while (Next.next != NULL)
    聽 聽 聽 聽聽{
    聽 聽 聽 聽聽聽 聽 聽 聽聽if (Curr.next == first->next){ Curr.next = NULL; }
    聽 聽 聽 聽聽聽 聽 聽 聽聽Loop = *Next.next;
    聽 聽 聽 聽聽聽 聽 聽 聽聽Next.next = &Curr;
    聽 聽 聽 聽聽聽 聽 聽 聽聽Curr = Next;
    聽 聽 聽 聽聽聽 聽 聽 聽聽Next = Loop;
    聽 聽 聽 聽聽}
    }

    void main()
    {
    聽 聽 聽 聽 //I have 10 items linked.
    聽 聽 聽 聽 reverse(&a1);
    }

    PLEASE HELP

  22. Ryan DeElena Avatar

    LOL I'm liking how you let it say "A Apple" … I suppose you don't really care as much about grammar as the language syntax, huh? (:

  23. Kunal Mazumdar Avatar

    Sir, You've made me habitual. If I have any doubts, first I search whether you have 聽covered it or not.聽
    Thank You.

  24. za2ear1 Avatar

    My man Derek thanks much tor these tutorials youre awsom . and dont worry about my name its weired ,anyways for the life of me i could'nt figure out why the enum took on the value of orangeAmt.individual ,if you can explain please and thank you ,is it the typedef or b/c enums variable take on 1,2,3 unless you initialize the values ahead?.

  25. rumi syed Avatar

    Derek ur awesome man. your tutorials are great , everything covered in a quick way … thanks

  26. Jack Chan Avatar

    why did聽orangeAmt.pound = 1.5; change orangeAmt.ounce to 1.5 too

  27. Derek Banas Avatar

    Thank you 馃檪 Yes I figured since everyone else makes slow tutorials that I'd be different and make fast ones. i'm glad you like them.

  28. jason bourne Avatar

    You teach it fast but very informative

  29. Derek Banas Avatar

    Thank you 馃檪 Everyone is different. After you gain a true understanding of object oriented design then it is very easy to learn other oop languages. That seems to be the problem people have. I have a object oriented design tutorial that may help.

  30. zelvar1231 Avatar

    Sweet video, but I have a none related question. Will it take a long time to learn C++ if u know C? 馃檪 And then learn C# /.net? Because i need to learn C this year for school and c++ next year and i can choose .net the year after that 馃檪

  31. Derek Banas Avatar

    A block in memory is big enough to hold the largest possible value. This tutorial has a few quizzes in it to make you think about why we get the results we are getting. I explore not only unions, but also how we can be tricked into thinking things while programming. Re-watch it from that perspective and see if you can figure out what is going on. I made this video to make people think about programming on a deeper level.

  32. Ethashamuddin Mohammed Avatar

    Hello Derek,
    How 3 members address show same value in union, How the values are actually stored?
    Can u please elaborate? I assume the largest member size memory is being used to store all 3 members? correct? can u elaborate on this?

  33. Ethashamuddin Mohammed Avatar

    Hello Derek,
    your point is correct, but, typo in the program, audience FYI..
    // This location is the same as individual

    printf("Pound Location: %dnn", &orangeAmt.individual);

  34. Ethashamuddin Mohammed Avatar

    Hello Derek,
    this line is not working in VS 2005.
    amount orangeAmt = {.ounce = 16};

    Why it worked for you using gcc?

Leave a Reply

Your email address will not be published. Required fields are marked *