0

I have been going through this chapter on Pointers and came across this sentence:

You will use pointers to deal with variables that have been allocated on the heap. (You can use pointers to deal with stack variables, but in most cases this just isn't necessary).

I know how pointers can be used to deal with variables that have been allocated on heap. But I cannot image how it can be used to deal with stack variables. Since stack variables are automatically deallocated when the function exits (i.e variables are pushed out of the stack), how can the same be done with pointers (as the above quoted text implies) without using something like free system call ? Is that possible ?

Sibi
  • 47,472
  • 16
  • 95
  • 163
  • What is "same" that you want to do with stack variables? – MikeCAT Oct 18 '15 at 12:53
  • 1
    "using pointers" != "calling `free`" – Oliver Charlesworth Oct 18 '15 at 12:53
  • It isn't clear what you're having trouble with. But you can just make a pointer point to a stack variable. Pointers are orthogonal to the type of storage of the object they point to. – juanchopanza Oct 18 '15 at 12:54
  • I think the author is talking about using pointers with local variables in general, not just for returning pointers from functions. – cadaniluk Oct 18 '15 at 12:54
  • 1
    A variable allocated on the stack is allocated in memory (although statically and not dynamically), since the stack itself is located in memory. So this variable has a memory address which can be accessed via pointer. For example: declare `int a, *p = &a`, and assign `*p = 5`. – barak manos Oct 18 '15 at 12:54
  • It is not unknown for stack vars to exist, (and so pointers to them remain valid), for the lifetime of an app. vars declared at the top of main(), for example, or at the top of thread functions before the infinite loop that performs the bulk of the functionality. – Martin James Oct 18 '15 at 13:18

5 Answers5

1

You can take the address of a local variable (allocated on the stack) by using the address-of operator, &. This address can then be stored in a pointer, and used like it is pointing to a variable on the heap. However, you should not free() the stored address, as this constitutes undefined behavior.

owacoder
  • 4,815
  • 20
  • 47
1

Simple example:

int i;
scanf("%d", &i);

A pointer to the stack variable i is passed to scanf() in order to store the results there.

Ulrich Eckhardt
  • 662
  • 6
  • 14
1

Common example of using pointers with stack variables: modifying value of local variables from another function.

#include <stdio.h>

void hoge(int *a) {
    *a = *a + 10;
}

int main(void) {
    int foo = 5;
    printf("%d\n", foo);
    hoge(&foo);
    printf("%d\n", foo);
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

Inside of a function you can can use pointer arithmetics and make references to variables on stack. Variables are pushed on stack at the beginning of a function and popped at the end.

In this example you can access b through the address of a(bear in mind order of parameters pushed on stack What is argument push order).

void fnc(unsigned int a, unsigned int b) {
    unsigned int *pb = (unsigned int *)((long)&a - (long)sizeof(int));
    printf("%d\n", *pb);
}
Community
  • 1
  • 1
0

A very popular and known method to swap two variable's values is yet another example.

#include <stdio.h>

void Swap(char *x,char *y);

int main(void)
{
    char character1 = 'a';

    char character2 = 'z';

    Swap(&character1,&character2);

    printf("character1 now is : %c\n",character1);

    printf("character2 now is : %c\n",character2);
}

void Swap(char *x,char *y)
{
    char tmp;

    tmp = *x;

    *x = *y;

    *y = tmp;
}
machine_1
  • 4,266
  • 2
  • 21
  • 42