Jump to content

Make a function that returns Char Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
Hello CodeCall, I MISSED YOU ALL. My uncle got married a couple of days ago so I couldn't visit CC these days. Talked too much, back on thread.

I was learning something about functions in C++ and I wanted to try something myself. I wanted to create a function that would return a char array but I got tons of errors. Could anyone please tell me how can I do that in an other way. Use CString?

Looking forward for reply :D

Wish you all the best,
Dren

PS: My Daddy was about to kill me when he heard that I wanted to fire an AK on my Uncles wedding lol :p

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
How about a std::string?

#3
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
I would also say that a C++ string (std::string) is the best choice, especially now when you're working with C++ anyway.

#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Check this out.

#include <iostream>
#include <conio.h>

using namespace std;

char * Return_Phrase(char * array);

int main() {
    char * return_value;
    char phrase[300];

    cout << "Enter a phrase: ";
    cin.getline(phrase, 299);
    
    return_value = Return_Phrase(phrase);
    cout << endl << return_value;
    getch();

    return 0;
}

char * Return_Phrase(char * array) {
     return array;
}

This should give you an idea of how to pass an array to a function, and then return it. You are going to have to use pointers in order to do the job.

And yes I used <conio.h>, because although not standardized it is still a popular library.
I will also take a direct quote from this site in one of the stickied threads for anyone who has a problem with me using it.

From the C/C++ Resources thread...

Quote

Is it okay to use conio.h? ...
... and questions alike regarding platform-dependent libraries are not really questions for us, but more for yourself.

So yeah, apparently it is okay to use conio.h on this site, well if you would have read stickied threads you would have known that.

Anyways hopefully this code will help you out.

#5
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
Thank you all, especialy you MeTh0Dz|Reb0rn, your post helped me so much. I still dont know how to use std::string, that std:: looks so weird indeed.

Thanks again,

CodeCall #1,
Dren

#6
LogicKills

LogicKills

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

Dren said:

Thank you all, especialy you MeTh0Dz|Reb0rn, your post helped me so much. I still dont know how to use std::string, that std:: looks so weird indeed.

Thanks again,

CodeCall #1,
Dren

Well you don't have to use it as std::string, that is only if you don't declare 'namespace std' globally..
Like:




#include <iostream>


int main()

{

std::cout << "Hello World" << std::endl;

return 0;

}



or

#include <iostream>

using std::cout;

using std::endl;


int main()

{

cout << "Hello World << endl;

return 0;

}


or

#include <iostream>

using namespace std; // globally declare the std or standard library


int main()

{

cout << "Hello World" << endl;

return 0;

}


http://logickills.org
Science - Math - Hacking - Tech

#7
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Many dudes prefer NOT doing the "using namespace std" thing globally (that polluting namespaces etc), so it could be a nice habit to put std:: in front of those functions (cout etc) that need it, and not putting "using namespace..." on the top. To use strings, #include <cstring>, and you have a data type named "string" to use:
#include <cstring>

std::string DoSomethingWithThatString(std::string str); // Function declaration

std::string myDearString = ""; // Global, initialized string. Notice "std::" if you're not having "using namespace.."

/* MAIN */
int main()
{
  using namespace std; // Now this is OK, but will only affect stuff within function
 
  string mySecondString; // Local, uninitialized string. Notice, no "std::", because of "using namespace.."

  mySecondString = DoSomethingWithThatString(myDearString); // Yup you probably get it :D

  return (EXIT_SUCCESS); // Has nothing to do with strings
}

/* FUNCTION RETURNING STRING */
std::string DoSomethingWithThatString(std::string str)
{
  // Do something with the string object (str) here if ya want...
  return str; // Returns input
}

The string variable is an object, having its own methodz (yeah...) and stuff, like myString.length etc...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#8
directoryxd

directoryxd

    Newbie

  • Members
  • Pip
  • 3 posts
I am very weak in this issue
medifast weight lossLas vegas hotels

#9
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts

directoryxd said:

I am very weak in this issue

Okay? Go read a tutorial online. Or study the code I posted above.

#10
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Don't worry, there are even free e-books on the subject!
"Learn to program with C++" (John Smiley), for example... Or "Thinking in C++" 1 and 2...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Here's a site with just about everything you need (and for free, too)

Freebyte's Guide to C/C++

#12
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Hey, that's really not bad! Quite a lot of links there, eh....? :)
Although I prefer the learn-stuff-from-a-book-thing, this might get useful...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa