Call by Value vs. Call by Reference in C
Feature | Call by Value | Call by Reference (nur über Zeiger) |
---|---|---|
Übergebenes Argument | Kopie des Wertes | Adresse der Variable |
Effekt auf Original | Keine Änderung am Original | Original kann verändert werden |
Speicherverbrauch | Mehr (jede Kopie braucht Speicher) | Weniger (nur Adresse wird übergeben) |
Sicherheit | Sicher, keine externen Änderungen | Vorsicht, da Original geändert werden kann |
- Hinweis: C unterstützt kein echtes Call by Reference. Änderungen am Original sind nur möglich, indem man einen Zeiger (Pointer) auf die Variable übergibt.
Beispiele
Call by Value
#include <stdio.h>
void modifyValue(int x) {
x = 20; // Ändert nur die Kopie, Original bleibt unverändert
}
int main() {
int a = 10;
modifyValue(a);
printf("Wert von a: %d\n", a); // Ausgabe: 10
return 0;
}
Call by Reference (über Zeiger)
#include <stdio.h>
void modifyValue(int *x) {
*x = 20; // Ändert das Original durch den Zeiger
}
int main() {
int a = 10;
modifyValue(&a);
printf("Wert von a: %d\n", a); // Ausgabe: 20
return 0;
}
- Call by Value: Funktion arbeitet mit einer Kopie; keine Änderung am Original.
- Call by Reference (über Zeiger): Funktion arbeitet mit einer Adresse; erlaubt Änderung am Original.
<!-- DISQUS SCRIPT COMMENT START -->
<!-- DISQUS RECOMMENDATION START -->
<div id="disqus_recommendations"></div>
<script>
(function() { // REQUIRED CONFIGURATION VARIABLE: EDIT THE SHORTNAME BELOW
var d = document, s = d.createElement('script'); // IMPORTANT: Replace EXAMPLE with your forum shortname!
s.src = 'https://myuninotes.disqus.com/recommendations.js'; s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>
Please enable JavaScript to view the
<a href="https://disqus.com/?ref_noscript" rel="nofollow">
comments powered by Disqus.
</a>
</noscript>
<!-- DISQUS RECOMMENDATION END -->
<hr style="border: none; height: 2px; background: linear-gradient(to right, #f0f0f0, #ccc, #f0f0f0); margin-top: 4rem; margin-bottom: 5rem;">
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables */
/*
var disqus_config = function () {
this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://myuninotes.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<!-- DISQUS SCRIPT COMMENT END -->
<!-- KO-FI WIDGET START -->
<script>
(function() {
let kofiContainerId = null;
let currentScript = null;
function initializeKofi() {
// Cleanup previous instances
if (kofiContainerId) {
const oldContainer = document.getElementById(kofiContainerId);
if (oldContainer) oldContainer.remove();
}
if (currentScript) {
currentScript.remove();
}
// Create new container
kofiContainerId = 'kofi-container-' + Date.now();
const container = document.createElement('div');
container.id = kofiContainerId;
document.body.appendChild(container);
// Load fresh script
currentScript = document.createElement('script');
currentScript.src = 'https://storage.ko-fi.com/cdn/scripts/overlay-widget.js';
currentScript.onload = () => {
kofiWidgetOverlay.draw('myuninotes', {
'type': 'floating-chat',
'floating-chat.donateButton.text': 'Support me',
'floating-chat.donateButton.background-color': '#00b9fe',
'floating-chat.donateButton.text-color': '#fff'
}, kofiContainerId);
};
document.head.appendChild(currentScript);
}
// Initial load
initializeKofi();
// SPA navigation handlers
const spaNavigationEvents = ['load', 'hashchange', 'popstate'];
spaNavigationEvents.forEach(event => {
window.addEventListener(event, initializeKofi);
});
// Modern SPA history handling
const originalPushState = history.pushState;
history.pushState = function() {
originalPushState.apply(this, arguments);
initializeKofi();
};
const originalReplaceState = history.replaceState;
history.replaceState = function() {
originalReplaceState.apply(this, arguments);
initializeKofi();
};
})();
</script>
<!-- KO-FI WIDGET END -->
<!-- Modal START -->
<div id="myuniModalScope">
<style>
/* Prefix alles mit #myuniModalScope, um Konflikte zu vermeiden */
#myuniModalScope .myuni-modal {
display: none; /* Hidden by default */
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.6);
}
#myuniModalScope .myuni-modal.myuni-show {
display: block;
}
#myuniModalScope .myuni-modal-content {
background-color: #fff;
margin: 10% auto;
padding: 20px;
border-radius: 8px;
width: 80%;
max-width: 500px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
position: relative;
animation: myuni-fadeIn 0.5s;
}
#myuniModalScope .myuni-close {
position: absolute;
top: 10px;
right: 15px;
font-size: 24px;
font-weight: bold;
color: #aaa;
cursor: pointer;
}
#myuniModalScope .myuni-close:hover {
color: #000;
}
#myuniModalScope .myuni-modal-text {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 16px;
margin-bottom: 15px;
color: #333;
line-height: 1.5;
}
#myuniModalScope .myuni-modal-highlight {
font-weight: bold;
color: #555;
}
#myuniModalScope .myuni-ko-fi-widget-container {
text-align: center;
margin-top: 20px;
}
@keyframes myuni-fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
<!-- HTML-Gerüst mit eigenen Klassen/IDs bleibt -->
<div id="myModal" class="myuni-modal">
<div class="myuni-modal-content">
<span id="closeModal" class="myuni-close">×</span>
<p class="myuni-modal-text">
<span class="myuni-modal-highlight">
MyUniNotes is a free, non-profit project to make education accessible for everyone.
</span>
If it has helped you, consider giving back! Even a small donation makes a difference.
</p>
<div class="myuni-ko-fi-widget-container">
<script type="text/javascript" src="https://storage.ko-fi.com/cdn/widget/Widget_2.js"></script>
<script type="text/javascript">
kofiwidget2.init('Support my efforts', '#72a4f2', 'E1E219YH74');
kofiwidget2.draw();
</script>
</div>
<p class="myuni-modal-text">
These are my personal notes. While I strive for accuracy, I’m still a student myself. Thanks for being part of this journey!
</p>
</div>
</div>
<script>
// JavaScript zum Anzeigen des Modals
document.addEventListener('DOMContentLoaded', function() {
// Show the modal after a short delay
setTimeout(function() {
const modal = document.querySelector('#myuniModalScope #myModal');
if (modal) {
modal.classList.add('myuni-show');
}
}, 1000); // Wartezeit hier anpassen
// Schließen, wenn X geklickt
const closeModal = document.querySelector('#myuniModalScope #closeModal');
if (closeModal) {
closeModal.addEventListener('click', function() {
const modal = document.querySelector('#myuniModalScope #myModal');
if (modal) {
modal.classList.remove('myuni-show');
}
});
}
});
</script>
</div>
<!-- Modal END -->