computing in c

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/40

encourage image

There's no tags or description

Looks like no tags are added yet.

41 Terms

1
New cards

Write a prototype and definition for min2 that returns the smaller of two ints.


<p></p>
2
New cards
<p></p>


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

3
New cards
<p></p>


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

4
New cards
<p>then rewrite it so it don't send the copy of the values </p>

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


<p></p>
5
New cards
<p></p>


→ -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.

6
New cards
<p></p>


33 42

7
New cards
<p></p>


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

8
New cards
<p></p>


x=5 y=12 z=17

9
New cards
<p></p>


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

10
New cards
<p></p>


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

11
New cards
<p></p>


(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.

12
New cards
<p></p>


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.

13
New cards

Read N, malloc an int array of size N, read N values, print them in reverse, then free it.


<p></p>
14
New cards
<p></p>


-1 2 4 6 8 -1 -1 -1 -1 -1 Missing elements start at 0, and p and A share the same memory.

15
New cards

Rewrite arr[k] and &arr[k] using pointer arithmetic.

arr[k] == *(arr + k) &arr[k] == arr + k

16
New cards
<p></p>


p+1 = 0x…c4 (moves sizeof(int) = 4 bytes), c+1 = 0x…c1 (moves 1 byte). p+k moves k * sizeof(type).

17
New cards

Write int *makeArray(int n) that returns a new int array filled with 0s, or NULL if malloc fails.


<p></p>
18
New cards
<p></p>


(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.

19
New cards
<p></p>


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

20
New cards
<p>Start with <code>A = {10, 20}</code> and <code>p = A</code>, reset before each one. What value is printed, and what are <code>*p</code> and <code>A</code> afterward?</p>

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.

<p><code>++</code> binds tighter than <code>*</code>, so <code>*p++</code> moves the <strong>pointer</strong>, not the value.</p>
21
New cards
<p>21. Same setup (<code>A = {10, 20}</code>, <code>p = A</code>, reset each time).</p><p>What value is printed, and what are <code>*p</code> and <code>A</code> afterward?</p>

21. Same setup (A = {10, 20}, p = A, reset each time).

What value is printed, and what are *p and A afterward?


<p></p>
22
New cards

Write void printStr(char *s) that prints a string using only pointer arithmetic (no []).

or : while (*s) printf("%c", *s++);

<p>or : <span>while</span> <span>(</span>*s<span>)</span> <span>printf("%c",</span> *s++<span>);</span></p>
23
New cards
<p></p>


→ !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.

24
New cards
<p></p>


→ 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
New cards

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 name

With typedef, you name it once at the bottom:

c

typedef struct _Laptop {
   char brand[20];
   int ram;
} Laptop;            // nickname

Laptop my;           // short name

struct _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>
26
New cards

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}};


<p></p>
27
New cards

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 &


<p>you noramlly put &amp; when using a scanf</p><p></p><p>array of chars dont need &amp;</p><p></p>
28
New cards
<p></p>


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

29
New cards
<p></p>


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


That specific example = 24. Memorize it

30
New cards
<p></p>


48 32


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

31
New cards
<p></p>


→ 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

<p>→ <code>21 21 42 42</code>. <code>p-&gt;member</code> is the same as <code>(*p).member</code>. <code>*s.pointer</code> means <code>*(s.pointer)</code> because <code>.</code> binds tighter than <code>*</code>.</p><p></p><p>because p is a pointer if you did p.member first that itsn’t a thing</p>
32
New cards
<p></p>


→ 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” → Matrix *a

Using a variable

&

“give me its address” → f(&m)


<p>→ <strong>Seg fault.</strong> Passing by value copies about 4 MB onto the stack. Fix it by passing a pointer: <code>void f(Matrix *a)</code> and <code>f(&amp;m)</code>.</p><p></p><table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Declaring</strong> a variable/parameter</p></td><td colspan="1" rowspan="1"><p><code>*</code></p></td><td colspan="1" rowspan="1"><p>“this is a pointer” → <code>Matrix *a</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Using</strong> a variable</p></td><td colspan="1" rowspan="1"><p><code>&amp;</code></p></td><td colspan="1" rowspan="1"><p>“give me its address” → <code>f(&amp;m)</code></p></td></tr></tbody></table><p></p>
33
New cards
<p>Modularization</p><p><strong>33. Write </strong><code>newLaptop</code><strong> for a Laptop whose </strong><code>brand</code><strong> is a </strong><code>char *</code><strong>.</strong></p><p></p><p>Laptop *p = <span>newLaptop("Lenovo",</span> <span>16);</span> <span> // b = lenovo a = 16</span></p><p></p><p>Write a function <code>Laptop *newLaptop(const char *b, int r)</code> that:</p><ol><li><p>Mallocs space for <strong>one Laptop</strong> struct. If that fails, return <code>NULL</code>.</p></li><li><p>Mallocs space for the <strong>brand string</strong>: enough for every letter in <code>b</code> <strong>plus 1</strong> for the <code>'\0'</code>. If that fails, free the struct from step 1 and return <code>NULL</code>.</p></li><li><p>Copies the string <code>b</code> into the new brand space.</p></li><li><p>Sets the laptop’s <code>ram</code> to <code>r</code>.</p></li><li><p>Returns the pointer to the new laptop.</p></li></ol><p>Example: <code>Laptop *p = newLaptop("Lenovo", 16);</code> should give a laptop with brand “Lenovo” and ram 16.</p>

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:

  1. Mallocs space for one Laptop struct. If that fails, return NULL.

  2. 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.

  3. Copies the string b into the new brand space.

  4. Sets the laptop’s ram to r.

  5. 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

<p>p-&gt;brand is the same <span>as</span> <span>(</span>*<span>p)</span>.brand</p>
34
New cards

34. Write freeLaptop for that same struct.


<p></p>
35
New cards

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.

<p><code>Laptop</code> is a <strong>pointer</strong> type here, so <code>sizeof(Laptop)</code> would only give you the size of a pointer.</p>
36
New cards

36. Write laptop.h with header guards.

The function definitions go in laptop.c, and main.c uses #include "laptop.h".


<p>The function definitions go in <code>laptop.c</code>, and <code>main.c</code> uses <code>#include "laptop.h"</code>.</p><p></p>
37
New cards

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


<p></p>
38
New cards

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.

39
New cards
term image

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

40
New cards
term image

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

41
New cards
term image

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