Pass a pointer to struct and a int to a function in C to implement a stack












0















I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack



This is a partial implementation that is giving me some trouble.



in stack.h



#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
int max_size;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);


in stack.c:



#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif

Stack *stack_create(int n)
{
Stack stack;
Stack *s = &stack;
s->max_size = n;
s->top = 0;
// s->contents = (int *)malloc(sizeof(int) * n);
return s;
}


bool stack_is_empty(Stack *stack)
{
if (stack->top == 0)
{
return true;
}
return false;
}

bool stack_is_full(Stack *stack)
{
if (stack->top == stack->max_size)
{
return true;
}
return false;
}

void stack_push(Stack *stack, int value)
{

if (!stack_is_full(stack))
{
printf("max_size: %dn", stack->max_size);
printf("top: %d (%p)n", stack->top++, &stack->top);
printf("value: %d (%p)n", value, &value);
}
else
{
printf("Can't push. max_size==%d reached.n", stack- >max_size);
exit(EXIT_FAILURE);
}
}


and in main.c:



 #include <stdio.h>
#include <stdlib.h>
#include "stack.h"

#define SIZE 3

int main()
{
Stack *s = stack_create(SIZE);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 100);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 30);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 20);
printf("stack_is_empty: %dn", stack_is_empty(s));

return 0;
}


main produces the following output:



stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0


Why is value's address the same of stack->top?










share|improve this question

























  • You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns.

    – Some programmer dude
    Nov 24 '18 at 16:18








  • 1





    In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:38


















0















I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack



This is a partial implementation that is giving me some trouble.



in stack.h



#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
int max_size;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);


in stack.c:



#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif

Stack *stack_create(int n)
{
Stack stack;
Stack *s = &stack;
s->max_size = n;
s->top = 0;
// s->contents = (int *)malloc(sizeof(int) * n);
return s;
}


bool stack_is_empty(Stack *stack)
{
if (stack->top == 0)
{
return true;
}
return false;
}

bool stack_is_full(Stack *stack)
{
if (stack->top == stack->max_size)
{
return true;
}
return false;
}

void stack_push(Stack *stack, int value)
{

if (!stack_is_full(stack))
{
printf("max_size: %dn", stack->max_size);
printf("top: %d (%p)n", stack->top++, &stack->top);
printf("value: %d (%p)n", value, &value);
}
else
{
printf("Can't push. max_size==%d reached.n", stack- >max_size);
exit(EXIT_FAILURE);
}
}


and in main.c:



 #include <stdio.h>
#include <stdlib.h>
#include "stack.h"

#define SIZE 3

int main()
{
Stack *s = stack_create(SIZE);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 100);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 30);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 20);
printf("stack_is_empty: %dn", stack_is_empty(s));

return 0;
}


main produces the following output:



stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0


Why is value's address the same of stack->top?










share|improve this question

























  • You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns.

    – Some programmer dude
    Nov 24 '18 at 16:18








  • 1





    In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:38
















0












0








0








I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack



This is a partial implementation that is giving me some trouble.



in stack.h



#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
int max_size;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);


in stack.c:



#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif

Stack *stack_create(int n)
{
Stack stack;
Stack *s = &stack;
s->max_size = n;
s->top = 0;
// s->contents = (int *)malloc(sizeof(int) * n);
return s;
}


bool stack_is_empty(Stack *stack)
{
if (stack->top == 0)
{
return true;
}
return false;
}

bool stack_is_full(Stack *stack)
{
if (stack->top == stack->max_size)
{
return true;
}
return false;
}

void stack_push(Stack *stack, int value)
{

if (!stack_is_full(stack))
{
printf("max_size: %dn", stack->max_size);
printf("top: %d (%p)n", stack->top++, &stack->top);
printf("value: %d (%p)n", value, &value);
}
else
{
printf("Can't push. max_size==%d reached.n", stack- >max_size);
exit(EXIT_FAILURE);
}
}


and in main.c:



 #include <stdio.h>
#include <stdlib.h>
#include "stack.h"

#define SIZE 3

int main()
{
Stack *s = stack_create(SIZE);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 100);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 30);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 20);
printf("stack_is_empty: %dn", stack_is_empty(s));

return 0;
}


main produces the following output:



stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0


Why is value's address the same of stack->top?










share|improve this question
















I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack



This is a partial implementation that is giving me some trouble.



in stack.h



#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
int max_size;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);


in stack.c:



#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif

Stack *stack_create(int n)
{
Stack stack;
Stack *s = &stack;
s->max_size = n;
s->top = 0;
// s->contents = (int *)malloc(sizeof(int) * n);
return s;
}


bool stack_is_empty(Stack *stack)
{
if (stack->top == 0)
{
return true;
}
return false;
}

bool stack_is_full(Stack *stack)
{
if (stack->top == stack->max_size)
{
return true;
}
return false;
}

void stack_push(Stack *stack, int value)
{

if (!stack_is_full(stack))
{
printf("max_size: %dn", stack->max_size);
printf("top: %d (%p)n", stack->top++, &stack->top);
printf("value: %d (%p)n", value, &value);
}
else
{
printf("Can't push. max_size==%d reached.n", stack- >max_size);
exit(EXIT_FAILURE);
}
}


and in main.c:



 #include <stdio.h>
#include <stdlib.h>
#include "stack.h"

#define SIZE 3

int main()
{
Stack *s = stack_create(SIZE);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 100);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 30);
printf("stack_is_empty: %dn", stack_is_empty(s));
stack_push(s, 20);
printf("stack_is_empty: %dn", stack_is_empty(s));

return 0;
}


main produces the following output:



stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0


Why is value's address the same of stack->top?







c pointers struct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 1:57









kit

1,1063716




1,1063716










asked Nov 24 '18 at 16:15









stochazesthaistochazesthai

1861315




1861315













  • You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns.

    – Some programmer dude
    Nov 24 '18 at 16:18








  • 1





    In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:38





















  • You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns.

    – Some programmer dude
    Nov 24 '18 at 16:18








  • 1





    In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:38



















You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns.

– Some programmer dude
Nov 24 '18 at 16:18







You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns.

– Some programmer dude
Nov 24 '18 at 16:18






1




1





In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value;

– Paul Ogilvie
Nov 24 '18 at 16:38







In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value;

– Paul Ogilvie
Nov 24 '18 at 16:38














1 Answer
1






active

oldest

votes


















3














Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.



Problem 2 : You are allocating memory only for one instance regardless of value of 'n'



typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}





share|improve this answer
























  • Don't cast the fesult of malloc. void * is compatible with all pointer types.

    – Paul Ogilvie
    Nov 24 '18 at 16:36











  • Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:41











  • @PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

    – stochazesthai
    Nov 24 '18 at 17:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460029%2fpass-a-pointer-to-struct-and-a-int-to-a-function-in-c-to-implement-a-stack%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.



Problem 2 : You are allocating memory only for one instance regardless of value of 'n'



typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}





share|improve this answer
























  • Don't cast the fesult of malloc. void * is compatible with all pointer types.

    – Paul Ogilvie
    Nov 24 '18 at 16:36











  • Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:41











  • @PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

    – stochazesthai
    Nov 24 '18 at 17:04
















3














Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.



Problem 2 : You are allocating memory only for one instance regardless of value of 'n'



typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}





share|improve this answer
























  • Don't cast the fesult of malloc. void * is compatible with all pointer types.

    – Paul Ogilvie
    Nov 24 '18 at 16:36











  • Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:41











  • @PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

    – stochazesthai
    Nov 24 '18 at 17:04














3












3








3







Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.



Problem 2 : You are allocating memory only for one instance regardless of value of 'n'



typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}





share|improve this answer













Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.



Problem 2 : You are allocating memory only for one instance regardless of value of 'n'



typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;

Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 16:33









Dark SorrowDark Sorrow

496212




496212













  • Don't cast the fesult of malloc. void * is compatible with all pointer types.

    – Paul Ogilvie
    Nov 24 '18 at 16:36











  • Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:41











  • @PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

    – stochazesthai
    Nov 24 '18 at 17:04



















  • Don't cast the fesult of malloc. void * is compatible with all pointer types.

    – Paul Ogilvie
    Nov 24 '18 at 16:36











  • Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

    – Paul Ogilvie
    Nov 24 '18 at 16:41











  • @PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

    – stochazesthai
    Nov 24 '18 at 17:04

















Don't cast the fesult of malloc. void * is compatible with all pointer types.

– Paul Ogilvie
Nov 24 '18 at 16:36





Don't cast the fesult of malloc. void * is compatible with all pointer types.

– Paul Ogilvie
Nov 24 '18 at 16:36













Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

– Paul Ogilvie
Nov 24 '18 at 16:41





Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;

– Paul Ogilvie
Nov 24 '18 at 16:41













@PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

– stochazesthai
Nov 24 '18 at 17:04





@PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

– stochazesthai
Nov 24 '18 at 17:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460029%2fpass-a-pointer-to-struct-and-a-int-to-a-function-in-c-to-implement-a-stack%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks