Created
July 25, 2022 13:40
-
-
Save faustoct1/0cd3c7e3f921fe2380d220bb6c260082 to your computer and use it in GitHub Desktop.
Diferença entre cópia e referência de variáveis em C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Compilar: gcc -o copy copy.c | |
| Executar: ./copy | |
| */ | |
| #import <stdio.h> | |
| void naoAlteraValor(int x){ | |
| x=0; | |
| } | |
| void alteraValor(int *y){ | |
| *y=10; | |
| } | |
| int main(){ | |
| int x=10,y=0; | |
| naoAlteraValor(x); | |
| alteraValor(&y); | |
| printf("x não alterou valor=%d, y alterou valor=%d",x,y); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
naoAlteraValorusa uma cópia do valor da variável enquantoalteraValorusa a referência da variável em memória por esse motivo uma é alterada e a outra não.