1/40
Looks like no tags are added yet.
Write a prototype and definition for min2 that returns the smaller of two ints.


→ 20 30 20 31. L is re-created every call; S is static, so it keeps its value between calls.

→ 300 300 200. The inner B shadows the outer one and is re-created each time through the loop.

then rewrite it so it don't send the copy of the values


→ -1 -1 -1. The array is passed as a pointer to its first element, so the function changes the original. You have to pass N yourself.

33 42

→ 12 12 12 99. Changing *p changes x.

x=5 y=12 z=17

65 -> 'A' The cast changes how the bytes are read (little-endian: first byte is 65).

7+7 is 14 q is an alias of p (same memory). free(p) only ONCE: freeing q too is a double free.

(a) uninitialized pointer: seg fault / undefined behavior.
(b) q is a dangling pointer (memory was freed): garbage.
(c) memory leak: never freed and the pointer is lost.

Segmentation fault (runtime crash). Always check pointers (especially malloc's result) against NULL before using them.
Getting garbage means you read a real memory location your program is allowed to access, and it just held leftover junk.
NULL is address 0.
Read N, malloc an int array of size N, read N values, print them in reverse, then free it.


-1 2 4 6 8 -1 -1 -1 -1 -1 Missing elements start at 0, and p and A share the same memory.
Rewrite arr[k] and &arr[k] using pointer arithmetic.
arr[k] == *(arr + k) &arr[k] == arr + k

p+1 = 0x…c4 (moves sizeof(int) = 4 bytes), c+1 = 0x…c1 (moves 1 byte). p+k moves k * sizeof(type).
Write int *makeArray(int n) that returns a new int array filled with 0s, or NULL if malloc fails.


(a) is the bug: x is a local (stack) variable that disappears when f returns, so you get a dangling pointer. (b) static lives the whole program. (c) heap memory lives until free.

30 20 3 q - p = how many ELEMENTS apart (index 4 - index 1).

Start with A = {10, 20} and p = A, reset before each one. What value is printed, and what are *p and A afterward?
++ binds tighter than *, so *p++ moves the pointer, not the value.


21. Same setup (A = {10, 20}, p = A, reset each time).
What value is printed, and what are *p and A afterward?

Write void printStr(char *s) that prints a string using only pointer arithmetic (no []).
or : while (*s) printf("%c", *s++);


→ !dlrow ,olleh. The string comes out reversed.
(*—p) and not (*p—) because We want to print backwards starting from '!', and we do NOT want to print that \0. So we need to move back first, then print.

→ g and s are in static storage aka Data segment (they last the whole program). L is on the stack (automatic, gone after the return). *d is on the heap and stays until you free it.
25. Use typedef to define a Laptop with brand, ram, and screen. Create one, double its RAM, and print it.
typedef gives a type a nickname.
Without it, you have to write struct every time you use the type:
c
struct _Laptop {
char brand[20];
int ram;
};
struct _Laptop my; // long nameWith typedef, you name it once at the bottom:
c
typedef struct _Laptop {
char brand[20];
int ram;
} Laptop; // nickname
Laptop my; // short namestruct _Laptop and Laptop are now the same type, and Laptop is just shorter to write.
It works for any type, not just structs:
c
typedef int Number;
Number x = 5; // same as int x = 5;![<p><code>typedef</code> gives a type a <strong>nickname</strong>.</p><p>Without it, you have to write <code>struct</code> every time you use the type:</p><p>c</p><pre><code class="language-c">struct _Laptop {
char brand[20];
int ram;
};
struct _Laptop my; // long name</code></pre><p>With <code>typedef</code>, you name it once at the bottom:</p><p>c</p><pre><code class="language-c">typedef struct _Laptop {
char brand[20];
int ram;
} Laptop; // nickname
Laptop my; // short name</code></pre><p><code>struct _Laptop</code> and <code>Laptop</code> are now the <strong>same type</strong>, and <code>Laptop</code> is just shorter to write.</p><p>It works for any type, not just structs:</p><p>c</p><pre><code class="language-c">typedef int Number;
Number x = 5; // same as int x = 5;</code></pre><p></p>](https://assets.knowt.com/user-attachments/11571e63-860a-4a62-870b-728c5da02cb8.png)
Use sizeof to get the number of elements in inv, then print each brand.
you are given:
Laptop inv[] = {{“Lenovo”, 16, 15.4}, {“Dell”, 8,14.0}, {“HP”, 32, 17.0}};

Malloc an array of N Laptops, read each brand and RAM into it, then free it.
you noramlly put & when using a scanf
array of chars dont need &


→ 16 32. b = a copies every member into a separate struct, while y points to a itself.

→ 24. The members add up to 10 + 4 + 8 = 22, but the compiler adds padding.
That specific example = 24. Memorize it

48 32
add_ram changed a copy (returned into b). upgrade changed a through its pointer: 16 + 32 = 48.

→ 21 21 42 42. p->member is the same as (*p).member. *s.pointer means *(s.pointer) because . binds tighter than *.
because p is a pointer if you did p.member first that itsn’t a thing


→ Seg fault. Passing by value copies about 4 MB onto the stack. Fix it by passing a pointer: void f(Matrix *a) and f(&m).
Declaring a variable/parameter |
| “this is a pointer” → |
Using a variable |
| “give me its address” → |


Modularization
33. Write newLaptop for a Laptop whose brand is a char *.
Laptop *p = newLaptop("Lenovo", 16); // b = lenovo a = 16
Write a function Laptop *newLaptop(const char *b, int r) that:
Mallocs space for one Laptop struct. If that fails, return NULL.
Mallocs space for the brand string: enough for every letter in b plus 1 for the '\0'. If that fails, free the struct from step 1 and return NULL.
Copies the string b into the new brand space.
Sets the laptop’s ram to r.
Returns the pointer to the new laptop.
Example: Laptop *p = newLaptop("Lenovo", 16); should give a laptop with brand “Lenovo” and ram 16.
p->brand is the same as (*p).brand

34. Write freeLaptop for that same struct.

With this typedef, write the malloc line for a new Laptop p.
Laptop is a pointer type here, so sizeof(Laptop) would only give you the size of a pointer.

36. Write laptop.h with header guards.
The function definitions go in laptop.c, and main.c uses #include "laptop.h".

compile laptop.c and main.c separately, link them, and run the program.

38. struct A has a struct B *b member, and struct B has a struct A *a member. What line makes a.h compile?
struct B; // forward declaration
A forward declaration is enough for a pointer member. Put struct A; in b.h too, and use header guards in both files.

→ all 5. It runs right to left: x = (y = (z = 5)).

→ b = 5, a = 10. b -= 3 runs first, then a += 5.

→ *(p++): use the value at p, then move p.
→ *(++p): move p first, then use the value.
→ !(-x): negate x first, then apply !.