Skip to content

Instantly share code, notes, and snippets.

@faustoct1
Created July 25, 2022 13:40
Show Gist options
  • Save faustoct1/0cd3c7e3f921fe2380d220bb6c260082 to your computer and use it in GitHub Desktop.
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
/*
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;
}
@faustoct1
Copy link
Author

naoAlteraValor usa uma cópia do valor da variável enquanto alteraValor usa a referência da variável em memória por esse motivo uma é alterada e a outra não.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment