On 02 Apr 2007 10:36:38 -0700, satya dev <[EMAIL PROTECTED]> wrote:
>
> I made changes in the code main (I had problem with this code )
> ignore previooous mail
> ---------------------------------------------------
> The purpose of the code is to return array of strings variable to main
> I am giving input 3 strings in function call()
> these strings are stored in a variable "str"
> then that is returned to main, I want to print them in main..
> if i give input as
> ---------------------------
> aaa
> bbb
> ccc
> ------------------------------
> output is
> ------------------------
> aaa
> garbage value
> garbage value
> ---------------------------------
> I want to know why garbage values are being printed..............
> ----------------------------------here is the code ----------------------
> #include<stdio.h>
>
> char* call()
> {
> int k=3,h=0;
> char *str[3];
You're creating a LOCAL string here with space for only TWO letters.
for(h=0;h<k;h++)
> {
> printf("Enter the String");
> scanf("%s",str+h);
If you enter more than 2 letters here, the results are undefined.
}
> return *str;
Here you return the pointer to the local string, which is then promptly
'destroyed' - referencing this address after this point results in undefined
behaviour.
}
> int main()
> {
> int i=3,j=0;
> char *name[i];
>
*name=call();
The pointer returned from call() is not useable.
for(j=0;j<i;j++)
> {
> printf("The string %d",j);
What string are you expecting at address 0?
printf("%s\n",name+j);
}
>
> return 0;
> }
>
--
PJH
Aio, quantitas magna frumentorum est
[Non-text portions of this message have been removed]