C++ Stack Defining and Reading

I need to create a stack from the Element300 type of size 3 and print it out in the function print. I'm having trouble defining the stack and passing it through to the print function though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
 using namespace std;

 typedef float Element300[80];

 void print (Element300 test, int * top);

 int main ()
 {

     Element300 test;
     int * top;
     int size = 0;


     for (int i = 0; i < 3; i++)
     {
         test[i] = 1;
         size = size + 1;
     }

     top = &test[size];

     print(test, * top);
 }

 void print (Element300 test, int * top)
 {
     Element300 temp;


 }
Last edited on
When you declared a Element300 pointer you haven't actually allocated any memory for the array yet. So unless you intend 'stackArray' to be a pointer pointing to a array of floats, unexpected things will happen. Note that declaring arrays requires you to know the maximum amount of items beforehand so if you're making a stack I suggest you use a dynamic array like a 'vector' or 'list' Also, the variable 'top' should be a pointer as well since it appears you are storing the address of the current element of the stack array in it.

Disclaimer : I'm a new programmer so anything I've said may or may not have been completely correct, I have researched it though.
Last edited on
Topic archived. No new replies allowed.