Aelphaeis here with another little problem.

In the Code below I attempted to make a generic function which read a line up to a newline character and then returned the string. The program reads as follows

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>

int getstring(char *string,int max);

int main(int argc,char * argv [])
{
    char name[15];
    name[0] = 'A';
    while (stricmp(name,"exit")!= 0)
    {
          getstring(name,15);
          printf("%s\n",name);
    }
    system("pause");
}

int getstring(char *string,int max)
{
     for (int num = 0;num != max;num++)
     {
         string[num] = getchar();
         if (string[num]=='\n')
         {
            string[num] = '\0';
            return num;
         }
     }
}

Now if you looking at this you might ask yourself
"So whats the point of returning your string, the fact is you don't need to return it because a character array is really a pointer which points to a place in memory and what you've effectively done in this program is manipulate the actually data which the pointer is pointing to..."
However if I wanted to past the actually data that the pointer contains how would I go about doing that?

In other words I want to make a gets() function which returns the actual data read from the screen.

with this in mind it would be used in the context

char string[15];
string = getstring(15);

Of course, its not really necessary but it would be a neat trick to use. Anyone have any ideas?

Did you already notice that your function getString() actually is returning a value of type int ?

To return a c-string your function has to return something of type const char * ...

Did you already notice that your function getString() actually is returning a value of type int ?

To return a c-string your function has to return something of type const char * ...

Yes I did notice that it returned a int type, I designed it to be like scanf which returns the number of variable scanned (at least thats what it did last time I checked, I can't seem to remember =[ ).

*Starts Testing tux4life's suggestion*
...
... ...
... ... ...
Ah, I see. Apparently your method fails because of the fact that its an array of characters, but that is a datatype I hadn't thought of before so I will experiment with it

Your dream of

char string[15];
string = getstring(15);

will not work.

You either have to do this (and why not?)

getstring (string,15);

Or this

char *string;
string = getstring(); // getstring allocates string storage
// use string
free (string);

Your dream of

You either have to do this (and why not?)

getstring (string,15);

I actually had assumed that =(. Its pretty obvious why It wouldn't work. however do you think would work if I did something crazy like

char string [] = getstring(15);

though similar to the other one I believe that this actually makes sense and is possible given the return type const char*

>however do you think would work if I did something crazy like
>char string [] = getstring(15);
No, you can't initialize an array with a memory address. That's what pointers are for.

Yes I did notice that it returned a int type, I designed it to be like scanf which returns the number of variable scanned (at least thats what it did last time I checked, I can't seem to remember =[ ).

*Starts Testing tux4life's suggestion*
...
... ...
... ... ...
Ah, I see. Apparently your method fails because of the fact that its an array of characters, but that is a datatype I hadn't thought of before so I will experiment with it

It fails because you implemented it in the wrong way ...
Maybe the following thread might be useful for you: http://www.daniweb.com/forums/thread56000.html

I've also included an example:

#include<iostream>

using namespace std;

char * strRet();

int main()
{
	char * string;
	string = strRet();
	cout << string << endl;
	delete[] string;
	return 0;
}
char * strRet()
{
	char * sth = new char[15];
	strcpy(sth, "Hello World!");
	return sth;
}
#include<iostream>

using namespace std;

char * strRet();

int main()
{
	char * string;
	string = strRet();
	cout << string << endl;
	delete[] string;
	return 0;
}
char * strRet()
{
	char * sth = new char[15];
	strcpy(sth, "Hello World!");
	return sth;
}

Your Example is cute and all (and a bonus, it works =]) but firstly its in C++ and while I like C++ I want to do it in C hence why its in the C forum ^_^. secondly this doesn't do what I want it to do.

Until you translate that to C we can't move any further with that code.

Note: If you where to write a piece of Source Code with C++ syntax but C keywords and principles your C++ compiler will just get the job done. If your using Dev - C++ but compiler as a .c file your compiler will actually insult you more times than not. I've tried it, so Object C won't work either.

Your Example is cute and all (and a bonus, it works =]) but firstly its in C++ and while I like C++ I want to do it in C hence why its in the C forum ^_^. secondly this doesn't do what I want it to do.

Until you translate that to C we can't move any further with that code.

Oh, sorry for that I didn't notice I was posting in the C forum ...
In that case you'll have to take a look at the link I posted ...

Until you translate that to C we can't move any further with that code.

why, exactly, are we reinventing the wheel?

i'm trying to locate my motivation for "moving further", and not having much luck....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.