Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
Why would we want to reference a variable address? What are its advantages?

I've tried googling for an answer with no luck. My programming teacher just mentions it as a possibility that you can use, but I can't see the use cases, I think I understand how it works, so my theory is that it's faster to reference &var than using var. If that's the case is it just for that purpose? Is there something I'm missing? Am I completely wrong? EDIT: Sorry I meant - It's faster to reference a variable by using its address than just using the variable

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

To give other software references to variables:

double x0, y0;
double x1, y1;
double x2, y2;

/*  Convert polar coordinates to rectangular coordinates.
    This code needs two results from each call, but a function can only
    return one thing.  And we do not want the function to set the same
    variables each time, so we cannot use fixed global variables.  In
    each call, we pass the function pointers to two variables we want
    it to put the results in.
*/
ConvertPolarToRectangular(&x0, &y0, 3,  .17);
ConvertPolarToRectangular(&x1, &y1, 4,  .23);
ConvertPolarToRectangular(&x2, &y2, 5, -.03);

To work with data when we do not know how much we need when we compile the program:

//  Get number of items needed.
scanf("%d", &N);

/*  Allocate memory for records.  This makes Records point to enough
    memory for N records (if the malloc succeeds).
*/
struct Record *Records = malloc(N * sizeof *Records);

To create linked lists, trees, and other data structures that connect to each other:

struct ListItem
{
    struct ListItem *Next;
    int Data;
}

struct ListItem *FirstItem = malloc(sizeof *FirstItem);
struct ListItem *SecondItem = malloc(sizeof *SecondItem);
FirstItem->Next = SecondItem;
SecondItem->Next = NULL;

/*  Now we can keep allocating memory for as many items as we need
    and keep linking them together into a list.  When we want to
    delete one item, we can take it out of the chain without moving
    the other items in memory:
*/
FirstItem->Next = ThirdItem;
free(SecondItem);
over 4 years ago · Santiago Trujillo Relatório

0

Besides performance, there is the ability to change the referenced variable.. If pass a variable to a function as an argument, the value of the variable is copied. If the function changes its argument, the original variable does not change. If you pass the address of a variable, however, then the function has the ability to change the original variable’s value. Sometimes that’s bad; sometimes that’s great. If you need that ability, then this is a crucial feature.

over 4 years ago · Santiago Trujillo Relatório

0

One common reason to take the address of a variable would be to allow a variable defined in one function to be changed in another.

In C, all parameters to functions are passed by value. So if you want a function to change the value of a variable defined in some other function, you need to pass its address.

For example:

void increment(int *x)
{
    *x = *x + 1;
}

int main()
{
    int num = 4;
    increment(&num);
    printf("num=%d\n", num);
}

Here, the address of num is passed to the increment function. This function can then dereference this pointer to read and write the value stored at that address. This code will output "num=5".

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda