Re: Returning an array -- help needed
This is a MIME GnuPG-signed message. If you see this text, it means that
your E-mail or Usenet software does not support MIME signed messages.
The Internet standard for MIME PGP messages, RFC 2015, was published in 1996.
To open this message correctly you will need to install E-mail or Usenet
software that supports modern Internet standards.
--=_mimegpg-commodore.email-scan.com-28243-1229134413-0001
Content-Type: text/plain; format=flowed; charset="US-ASCII"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Abhayks writes:
Hi,
I am retuning to "C" after a long time and my basics are shatterred.
Not sure where I am making the mistake.
Please help.
#include<stdio.h>
#include<stdlib.h>
void foo( char ** ptr)
{
int i;
*ptr = malloc(255); // allocate some memory
// strcpy( *ptr, "Hello World");
for( i=0; i<5; i++)
{
*ptr[i]='a';
(*ptr)[i]='a';
}
(*ptr)[i]=0;
}
int main()
{
char *ptr = 0;
// call function with a pointer to pointer
foo(&ptr );
printf("%s\n", ptr);
// free up the memory
free(ptr);
return 0;
}
Please advise why this is not working?
1) Operator precedence. [] carries higher precedence than the unary *
operator.
2) The string must be zero-terminated. malloc does not clear allocated
memory.
--=_mimegpg-commodore.email-scan.com-28243-1229134413-0001
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEABECAAYFAklDGk0ACgkQx9p3GYHlUOKEmACfY3oruMVA4qVQ9qoiYe8B2+d0
IHAAniUQxD9jW6NrzQ4OlIAOv3hM5+Sp
=Nb3d
-----END PGP SIGNATURE-----
--=_mimegpg-commodore.email-scan.com-28243-1229134413-0001--