Java hides a lot of the behind-the-scenes stuff from you. Every object is really a pointer or a reference to the actual object. In C, you don't have objects (at least not in the traditional sense, although you can fake them with function pointers).
All strings are in C are just arrays of chars with a trailing '\0' char. A string can also be represented by a pointer to the first char of such a sequence.
As already mentioned, in C you can't return local arrays because they get destroyed. I believe you can do it in Java because Java arrays are actually objects. The array you would have in a Java function is actually a pointer to that array object (which is dynamically allocated).
The way to allocate objects in Java is through the new keyword. In C, dynamic memory is allocated through the malloc() function. Also of importance, you have to manually deallocate all allocated memory in C. You do this with the free() function.
An example of how to return a string from a function would be something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *getString(void);
int main(int argc, char *argv[])
{
char *szString;
printf("Enter a string: ");
fflush(stdout);
szString = getString();
if(szString != NULL)
{
printf("You entered: %s\n",szString);
free(szString);
}
else puts("Error!");
return 0;
}
char *getString(void)
{
int len;
char *s;
size_t ssize = BUFSIZ*(sizeof(*s));
s = (char *)malloc(ssize);
if(!s)
{
return NULL;
}
if(fgets(s,ssize,stdin))
{
len = strlen(s) - 1;
if(s[len] == '\n') /* *Corrected* Thanks to Mr. Sinkula for finding this error. :) */
{
s[len] = '\0';
}
}
return s;
}