Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning a char array problem

Status
Not open for further replies.

grexroad

Programmer
Jan 30, 2008
7
US
I am trying to figure out what the problem is with the code I wrote for returning a char array from a function. It prints out ascii characters instead of the word that was typed in. Would someone look at my code and tell me what I need to change to make it work? This is for a class at school and I must use printf and gets(). I've been several hours on this and just at my wits end with it.


#include "stdafx.h"
#include <iostream>
#include <stdio.h>

using namespace std;

//prototype
char *get_c_String();

int main()
{

char *p;

p = get_c_String();

printf("\n\tThe name is %s\n\n", p);

system("pause");

return 0;

}// End of main

char *get_c_String()
{

char word[2][20];

printf("Enter a word: ");

gets(word[0]);

return word[0];

}
 
Two choices
Code:
char *get_c_String()
{
    static char word[20];
    printf("Enter a word: ");
    fflush (stdout);
    gets(word);
    return word;
}
or
Code:
char *get_c_String()
{
    char* word = new char[20];
    printf("Enter a word: ");
    fflush (stdout);
    gets(word);
    return word;
}
In the latter choice, you need to free memory.
 
Static did the trick. Thank you X. That is the first time that I used static. I understand what is happening now. Thank you very much my friend.

g
 
You should really be using something better than gets(), a totally unsafe function, and char arrays in a C++ program.

Use a std::string and the getline() method.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Note that using static in that way prevents your function from being multi-thread safe. So if you're righting a multi-threaded app, you'll need a different solution or use some carefully placed thread locks.
 
It also prevents you from calling the function more than once in a single threaded application and using both return values at the same time.

This is for a class at school and I must use printf and gets().
I'd be very skeptical about what you are learning in this class. You are being taught bad habits when there are alternate choices that are readily available and much better.
 
This is for a class at school and I must use printf and gets().
Oh, I didn't even see that...
What are you taking? A C+ programming course? :p
 
This is a c++ course. My third in the series. Each coures I'm being taught something different. The first course I was taught ConsoleReadline / ConsoleWriteline. The second I was taught cin / cout. This one is gets / printf. Could someone please make up their minds. Come on 3 different ways to say "Hello Freakin World". Cut me a break. And I'm paying money for this too. But I'm learning what I set out to do and one day I'm be able to go to the forums and help people out as ya'll have done for me. Your efforts, comments and ideas are all appriciated.

g
 
ConsoleReadline etc are Windows functions.

cout/cin/cerr are C++

printf etc are C

The problem is ease of use and formatting. ConsoleReadline doesn't do anything: it will just print a string you give it. They missed something. You could have used puts and gets which are C equivalents.

If you just want to print a value for debug, cout is easier. eg cout << "Fred=" << fred << endl;

If you want stuff spaced out correctly in different bases, printf is easier. Compare
cout << fill('0') << setw(4) << hex << x << "hex=" << dec << x << endl
printf ("%04x=%d\n", x, x);

I'm not even sure if the cout version one is correct. Some people will stick to their guns and go C++ all the way.
 
When you get a job, you might end up working for a company that wants cross platform code, so the ConsoleReadline/ ConsoleWriteline won't be available. Your boss might prefer cin/cout. You might be doing a lot of formatted i/o, which means you need to do scanf/printf.

The more tools you have in your toolbox, the better prepared you'll be!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top