Code:
#include <iostream>
#include <stdio.h>
using namespace std;
char* GetFunction1()
{
char *pointer; //declare pointer to int[arry]
char Input[5] ; //declare normal int[arry]
pointer = &Input[5]; //assign address of Input to pointer
You are assigning the address of the 6th element of the array Input to the pointer. This is past the end of the array and an invalid index and therefore pointer is now invalid (points to nothing usefull).
Code:
cout << "Enter Input" << endl;
cin >> *pointer; // input value to address pointed to by pointer
You are dereferencing the invalid pointer and this is simply the wrong way to accept user input using a pointer. You shouldn't dereference the pointer.
Code:
cout << *pointer << endl;
cout << "worked" << endl;
cin.get();
return pointer;
}
You are trying to (had it been pointing to a valid address to begin with) return the address of a local (local to the function it is declared in) variable. When the function exits, this local array is popped off the stack and therefore even if pointer had been assigned correctly in the first place, it would still be pointing to an invalid address.
Code:
char GetFunction2(char *pointer)
{
char func2;
func2 = *pointer;
cout << func2 << endl;
cout << "Second Function" << endl;
cin.get();
}
int main ()
{
char *pointer = GetFunction1();
GetFunction2(pointer);
cin.get();
}
There are several things you can do:
1) You can declare an array in your main function and pass it into both of your functions as a parameter which in turn will populate said array and then display the contents.
2) You could declare the array in GetFunction1 as static which mean the memory will not be popped off the stack once the function exits. This is not a recommended approach.
3) You can stop using pointers and arrays and such. This is the C++ forum, learn to use a string container instead.
[edit]
4) You can dynamically allocate space in the GetFunction1 function and return a pointer to that memory.
[/edit]