Pointers 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
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
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?
Genial!
Completion tasa mejorada a 9.09
Pointers and Functions
Desliza para mostrar el menú
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
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.
¡Gracias por tus comentarios!