TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [TCLUG:10364] returning a value from an array
On Mon, Nov 22, 1999 at 03:52:37PM -0600, bliss wrote:
> ok now i need this function to return a value that is in the array
> char *array[30] = {0};
> char *server_c;
> char item_chossen[5];
> int temp;
>
> /* my fgets that reads from a file and puts the strings into the array goes
> here */
> printf("which on do you want\n");
> 1 string1
> 2 string2
> .....
> scanf("%s", &item_chossen);
> temp = atoi(item_chossen);
> /* i want to return the value in arry[temp - 1] to the calling function */
> server_c = &array[(temp - 1)];
> return server_c;
> this does not work; i get warning: assignment from incompatible pointer w/o
> a cast
> warning: return makes integer from pointer w/o a cast
> the function is set
> char funtion();
> so i hope this is enough to show you where i am missing the boat.....
>
> i have just never thought to return a value in an array before.
> TIA
The obvious (but wrong solution) is to do this:
char *function()
{
.....
scanf("%d", temp);
return(array[temp]);
}
It's wrong for two reasons. One, if people type a number that's
out of range, you're likely to get a core dump somewhere in your
program. Also, you're causing a memory leak.
This should give you a clue as to what the right answer is
though.
Have fun (if at all possible),
--
Its name is Public Opinion. It is held in reverence. It settles everything.
Some think it is the voice of God. Loyalty to petrified opinion never yet
broke a chain or freed a human soul. ---Mark Twain
-- Eric Hopper (hopper@omnifarious.mn.org
http://ehopper-host105.dsl.visi.com/~hopper) --