Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Pointers and Functions | Pointers in Arrays and Functions
C Pointers Mastery

bookPointers and Functions

When you pass a variable to a function in C, you can do so either by value or by reference. Passing by value means the function receives a copy of the variable, so changes inside the function don't affect the original.

void update(int *p) {
    *p = 100;
}

Passing by reference means sending the variable's address using a pointer. This allows the function to directly modify the original variable’s value.

main.c

main.c

copy
12345678910111213141516
#include <stdio.h> // Function that modifies the value of an integer using a pointer void setToTen(int *numPtr) { *numPtr = 10; // Dereference pointer and assign new value } int main() { int value = 5; printf("Before: %d\n", value); setToTen(&value); // Pass address of value to function printf("After: %d\n", value); return 0; }

In this example, the function setToTen takes a pointer to an integer as its parameter. Inside the function, the pointer is dereferenced using the * operator, allowing direct access to the memory location of the original variable. By assigning *numPtr = 10;, you update the value stored at that address. In main, the address of value is passed to setToTen using the & operator. As a result, after the function call, the variable value is changed from 5 to 10. This demonstrates how passing a pointer enables a function to modify the original variable, rather than just a local copy.

question mark

Which statement best describes how to allow a C function to modify the value of a variable defined in another function?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookPointers and Functions

Swipe um das Menü anzuzeigen

When you pass a variable to a function in C, you can do so either by value or by reference. Passing by value means the function receives a copy of the variable, so changes inside the function don't affect the original.

void update(int *p) {
    *p = 100;
}

Passing by reference means sending the variable's address using a pointer. This allows the function to directly modify the original variable’s value.

main.c

main.c

copy
12345678910111213141516
#include <stdio.h> // Function that modifies the value of an integer using a pointer void setToTen(int *numPtr) { *numPtr = 10; // Dereference pointer and assign new value } int main() { int value = 5; printf("Before: %d\n", value); setToTen(&value); // Pass address of value to function printf("After: %d\n", value); return 0; }

In this example, the function setToTen takes a pointer to an integer as its parameter. Inside the function, the pointer is dereferenced using the * operator, allowing direct access to the memory location of the original variable. By assigning *numPtr = 10;, you update the value stored at that address. In main, the address of value is passed to setToTen using the & operator. As a result, after the function call, the variable value is changed from 5 to 10. This demonstrates how passing a pointer enables a function to modify the original variable, rather than just a local copy.

question mark

Which statement best describes how to allow a C function to modify the value of a variable defined in another function?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
some-alt