简体   繁体   中英

Pointers in memory

So I'm currently learning C and I've tried to implement my own array based stack. Here's the code :

#include <stdio.h>
#include <stdbool.h>

#define MAX_STACK_SIZE 255

typedef struct
{
    int array[MAX_STACK_SIZE];
    unsigned int count;
    unsigned int max;
} Stack;

void initialize(Stack*);
void push(Stack*, int value);
int* pop(Stack*);
bool isEmpty(Stack*);
bool isFull(Stack*);

int main()
{
    Stack s1;
    initialize(&s1);
    for(int i = 0; i < 7; i++)
    {
        push(&s1, i);
    }
    pop(&s1);
    push(&s1, 88);
    push(&s1, 6);
    int *top;
    while((top = pop(&s1)) != NULL)
    {
        printf("Popping %d from the top of the stack.\n", top);
    }
    return 0;
}

void initialize(Stack *p)
{
    p->count = 0;
    p->max = MAX_STACK_SIZE;
}

void push(Stack *p, int value)
{
    if(!isFull(p))
    {
        p->array[p->count] = value;
        p->count++;
    }
}

int* pop(Stack *p)
{
    if (!isEmpty(p))
    {
        p->count--;
        return p->array + p->count;
    }
    return NULL;
}

bool isEmpty(Stack *p)
{
    return p->count == 0;
}

bool isFull(Stack *p)
{
    return p->count == p->max;
}

So after reviewing my code a question popped into my mind regarding to something that happens in the pop function. In the pop function we return the pointer to the value in the top of the stack. Now the way I do it is :

return p->array + p->count;

where counter is the number of elements in the stack and of-course I decrement the counter value before returning the pointer because counter starts counting elements from 1 not from 0. So the question that came into my mind after reviewing my code is this : Say we pushed 2 numbers to the stack 0 and then 1. Now the counter is 2 so before popping the first element we decrement counter to 1 and then return this :

return p->array + p->count;

in my mind when I see it, I see it returns a pointer to the address of the first integer in the stack + 1 more address. So let's say the first integer in the array address is 20 (talking in decimals) so in my mind I think, ok this function will give me the pointer to the address (20 + 1) so a pointer to the address 21. Now, correct me if I'm wrong but from my understanding every byte has an address and if we return a pointer to address 21 we returning a pointer to address which is a /part/ of the first integer in the stack. Now that's what I think happens but in reality when running the program and printing the address of the value of the top of the stack (see the while loop, I print the address instead of the value) I see a decrement by 4 in each printing (4 bytes for taken by each int variable). So why is that exactly ? How when I write

return p->array + p->count;

instead of getting a pointer to address 21 I get a pointer to the variable in memory ? Is it something that the language is doing by itself ? Here's an illustration I made to visualize the example I gave you : 在此输入图像描述

That's just basic pointer arithmetic. When you add an integer value to a pointer, the result of the addition is the address of the pointer incremented by the size of the type it is pointing to multiplied by the integer value.

So adding 1 to a pointer of type int* will increment the address by sizeof(int) bytes (4 bytes on your machine). Adding 2 to a pointer of type char* will advance the pointer by 2 bytes ( sizeof(char) == 1 )).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM