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 ?

6
  • What is "same" that you want to do with stack variables? Commented Oct 18, 2015 at 12:53
  • 1
    "using pointers" != "calling free" Commented Oct 18, 2015 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. Commented Oct 18, 2015 at 12:54
  • I think the author is talking about using pointers with local variables in general, not just for returning pointers from functions. Commented Oct 18, 2015 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. Commented Oct 18, 2015 at 12:54

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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;
}

Comments

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);
}

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.