Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Pointers and Functions | Pointers in Arrays and Functions
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you show me the full code example for `setToTen` and how it's used in `main`?

Can you explain the difference between passing by value and passing by reference with a simple analogy?

What happens if I pass a variable by value but try to modify it inside the function?

bookPointers and Functions

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
some-alt