Pointers in functions
Sep 12, 2014 at 4:00pm UTC
How do I pass a char pointer to a function and write it out. Somethings wrong with this code and I dont know why. Help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
using namespace std;
// Function Decalarations
void Hello(char *);
// welcomeMessage - prints a welcoming message
char * myName;
int main()
{
cin >> myName;
Hello(myName);
return 0;
}
// Function Definitions
void Hello(char * myName)
{
cout << "HelloWorld! " << myName << endl;
}
Sep 12, 2014 at 4:11pm UTC
Why is *myName global?
Where did you allocate memory for that pointer?
Why are you trying to pass myName into that function, when myName is global?
Why are you trying to pass a char* instead of just using std::string instead?
Sep 12, 2014 at 4:17pm UTC
$ g++ -W{all,extra,pedantic} foo.cpp -ggdb
$ gdb ./a.out
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
(gdb) backtrace
#0 0x00007ffff7b44248 in std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) ()
from /usr/lib/libstdc++.so.6
#1 0x000000000040091e in main () at foo.cpp:13
(gdb) frame 1
13 cin >> myName;
(gdb) print myName
$1 = 0x0
`myName' points to NULL. In the reading operation you are trying to dereference a NULL pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
void hello(const std::string &name);
int main(){
std::string myName;
std::cin >> myName; //it would not read spaces
hello(myName);
}
void hello(const std::string &name){
std::cout << "HelloWorld! " << name << std::endl;
}
Or if you do want to use char
1 2 3 4 5 6 7
void Hello(const char *name);
int main(){
const int a_big_enough_number = 42;
char myName[a_big_enough_number];
std::cin >> myName;
Hello(myName);
}
Sep 12, 2014 at 4:22pm UTC
But if you do use the above solution with the char be sure to limit the number of characters that will be retrieved to avoid buffer overflow. And remember the extraction operator stops processing when it encounters as white space character.
1 2 3
#include <iomanip>
...
std::cin >> setw(a_big_enough_number - 1) >> myName;
Sep 13, 2014 at 4:21am UTC
The problem is you are creating a pointer to a char that is not pointing to anything...
Allocate memory in the heap by using this ---> char* myName = new char;
You have to initialize it.
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
#include <iostream>
using namespace std;
// Function Decalarations
void Hello(char * );
// welcomeMessage - prints a welcoming message
int main()
{
char * myName = new char ;
cout << "Enter Your name " << endl;
cin >> myName;
Hello(myName);
return 0;
}
// Function Definitions
void Hello(char * myName)
{
cout << "Your Name: " << myName << endl;
}
Last edited on Sep 13, 2014 at 5:28am UTC
Sep 13, 2014 at 9:25am UTC
Thanx! Now I understand the code!
Regards / Patrik
Sep 13, 2014 at 9:29am UTC
1 2 3
char * myName = new char ; //just 1 character
cout << "Enter Your name " << endl;
cin >> myName; //trying to read an string
remember that c-strings are zero terminated, ¿how big could be the string that you can read with that code?
Sep 14, 2014 at 3:25am UTC
Don't forget that you're only allocating space for one character.
Use this:
1 2 3 4 5 6
char * str = new char [size_of_string + 1];
// Do what you need to do
// ........
delete [] str; // Free memory after you're done using it
Topic archived. No new replies allowed.