diff --git a/pointer/.vscode/settings.json b/pointer/.vscode/settings.json index 5c0f1a8..ec12a97 100644 --- a/pointer/.vscode/settings.json +++ b/pointer/.vscode/settings.json @@ -1,3 +1,3 @@ { - "markdown.validate.enabled": true + "markdown.validate.enabled": false } \ No newline at end of file diff --git a/pointer/setup/code-runners.ts b/pointer/setup/code-runners.ts new file mode 100644 index 0000000..1e118d6 --- /dev/null +++ b/pointer/setup/code-runners.ts @@ -0,0 +1,57 @@ +import { defineCodeRunnersSetup } from '@slidev/types' + +export default defineCodeRunnersSetup(() => { + return { + async c(code) { + const JUDGE0_API = "http://localhost:2358/submissions?base64_encoded=true&wait=true" + + // Matches "// input: {value}" anywhere in the line + const inputRegex = /\/\/\s*input:\s*([^\r\n]+)/gi + const matches = [...code.matchAll(inputRegex)] + + // Join all found inputs with a newline for Judge0's stdin + const stdin = matches.map(m => m[1].trim()).join('\n') + + const body = { + source_code: btoa(code), + language_id: 50, + stdin: btoa(stdin), + } + + try { + const response = await fetch(JUDGE0_API, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }) + + const result = await response.json() + + // Decode results from Judge0 (Base64) + const output = result.stdout ? atob(result.stdout) : '' + const error = result.stderr ? atob(result.stderr) : '' + const compileErr = result.compile_output ? atob(result.compile_output) : '' + + const text = output || error || compileErr || "Program executed (no output)." + + return { + // Wrap in
tag to ensure newlines are preserved + html: `${escapeHtml(text)}`, + } + } catch (e) { + return { + html: `Error connecting to Judge0: ${escapeHtml(e.message)}`, + } + } + }, + } +}) + +// Helper function to escape HTML to prevent XSS +function escapeHtml(text: string): string { + const div = document.createElement('div') + div.textContent = text + return div.innerHTML +} diff --git a/pointer/slides.md b/pointer/slides.md index eba80c2..15f90ed 100644 --- a/pointer/slides.md +++ b/pointer/slides.md @@ -307,13 +307,309 @@ int main(){ --- -title: pointer to pointer +layout: section --- +# pointer to pointer + +--- +title: pointer to pointer visual +--- + ++ + +```mermaid +flowchart RL + +subgraph 1a[0x1003] +direction TB +1v[x = 5] +1t[int] + +1v ~~~ 1t +end +``` + + + + +```mermaid +flowchart LR + +subgraph 1a[0x1003] +direction TB +1v[x = 5] +1t[int] + +1v ~~~ 1t +end +style 1a stroke:#fab387, stroke-width:3px + +subgraph 2a[0x1002] +direction TB +2v[p = 0x1003] +2t[int*] + +2v ~~~ 2t +end + +1a ~~~ 2a + +2a p3@--> 1a + +p3@{animate: true} +``` + + + + +```mermaid +flowchart LR + +subgraph 1a[0x1003] +direction TB +1v[x = 5] +1t[int] + +1v ~~~ 1t +end +style 1a stroke:#fab387, stroke-width:3px + +subgraph 2a[0x1002] +direction TB +2v[p = 0x1003] +2t[int*] + +2v ~~~ 2t +end + +subgraph 3a[0x1001] +direction TB +3v[q = 0x1002] +3t[int**] + +3v ~~~ 3t +end + +1a ~~~ 2a ~~~ 3a + +3a p2@--> 2a p3@--> 1a + +p2@{animate: true} +p3@{animate: true} +``` + + + + +```mermaid +flowchart RL + +subgraph 1a[0x1003] +direction TB +1v[x = 5] +1t[int] + +1v ~~~ 1t +end +style 1a stroke:#fab387, stroke-width:3px + +subgraph 2a[0x1002] +direction TB +2v[p = 0x1003] +2t[int*] + +2v ~~~ 2t +end + +subgraph 3a[0x1001] +direction TB +3v[q = 0x1002] +3t[int**] + +3v ~~~ 3t +end + +subgraph 4a[0x1004] +direction TB +4v[r = 0x1001] +4t[int***]:::hidden + +4v ~~~ 4t +end + +4a ~~~ 1a ~~~ 2a ~~~ 3a + +3a p2@--> 2a p3@--> 1a + +p2@{animate: true} +p3@{animate: true} +``` + + + +```mermaid +flowchart RL + +subgraph 1a[0x1003] +direction TB +1v[x = 5] +1t[int] + +1v ~~~ 1t +end +style 1a stroke:#fab387, stroke-width:3px + +subgraph 2a[0x1002] +direction TB +2v[p = 0x1003] +2t[int*] + +2v ~~~ 2t +end + +subgraph 3a[0x1001] +direction TB +3v[q = 0x1002] +3t[int**] + +3v ~~~ 3t +end + +subgraph 4a[0x1004] +direction TB +4v[r = 0x1001] +4t[int***] + +4v ~~~ 4t +end + +4a ~~~ 1a ~~~ 2a ~~~ 3a + +4a p1@--> 3a p2@--> 2a p3@--> 1a + +p1@{animate: true} +p2@{animate: true} +p3@{animate: true} +``` + + + +--- +title: example pointer to pointer +--- + +```c {monaco-run} +#include+int main(){ + int x = 5; + int* p = &x; // adress of x -> p + *p = 6; // change value of address inside p to 5 + int** q = &p; // address of p -> q + int*** r = &q; // adress of q -> r +} +``` + + + + --- title: pointer as function argument --- +# pointer as function argument + +- call by value +- call by reference + +````md magic-move +```c +#include +int increment(int a){ + a = a + 1; // 11 +} +int main(){ + int a = 10; + increment(a); // 10 + print("a = %d\n", a); // 10 +} +``` +```c +#include +int increment(int a){ + a = a + 1; // 11 + return a; +} +int main(){ + int a = 10; + increment(a); // 10 + print("a = %d\n", a); // 10 +} +``` +```c +#include +int increment(int a){ + a = a + 1; // 11 + return a; +} +int main(){ + int a = 10; + a = increment(a); // 11 + print("a = %d\n", a); // 11 +} +``` +```c +#include +int increment(int a){ + a = a + 1; // 11 +} +int main(){ + int a = 10; + increment(a); // 10 + print("a = %d\n", a); // 10 +} +``` +```c +#include +int increment(int a){ + a = a + 1; // 11 +} +int main(){ + int a = 10; + increment(&a); // (0x1003) + print("a = %d\n", a); // 10 +} +``` +```c +#include +int increment(int* a){ + *a = *a + 1; // *(0x1003) + 1 +} +int main(){ + int a = 10; + increment(&a); // (0x1003) + print("a = %d\n", a); // 11 +} +``` +```` + + + --- title: pointers and arrays ---