ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am attempting to write a program with a function in it that when first called, takes in a string, and then prints out the first word of that string. Then, if sent a NULL as an argument in the next call, it will print out the second word. Doing so until the end of the sentence. If sent a new string, rather than null, it will begin again with the new string, and print out the first word of that string.
My main problem right now is keeping the string sent stored in such a way that it isn't lost after you exit the function, that way, if you send it a NULL the next time you call the function, it still remembers the last string it was sent.
I have tried static character arrays, and dynamic allocation.
If anyone has any ideas or suggestions, I would welcome them.
here is an example of what I am trying to do, hopefully the code isn't too messy:
#include <iostream>
#include <cstdlib>
using namespace std;
void string_eater(char* string);
int main(void)
{
char string[] = "Hello World.";
string_eater(string);
string_eater(NULL);
return 0;
}
void string_eater(char* string)
{
static int count;
static char* stored_string = NULL;
if(string != NULL) //All right I wasn't sent a NULL, so I have to start all over again..
{
count = 0;
stored_string = *string; //There is a new string, time to store it!
}
Make a local static pointer to string. Assign a dynamically allocated new string equal to your incomming, and work on that.
Or
make a static string, longer than you will ever need. Then copy the incoming string to it (strcpy()?), and then work from that stored string.
Copying the incoming string to a local pointer is bad, because there is no storage associated with that pointer, if you dont do something like "stored_string = new char[128];"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.