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?