[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Returning an array -- help needed

Abhayks

12/13/2008 2:03:00 AM

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';
}

}

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?
4 Answers

Sam

12/13/2008 2:14:00 AM

0

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.


Abhayks

12/13/2008 2:58:00 AM

0

On Dec 13, 10:13 am, Sam <s...@email-scan.com> wrote:
> 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.
>
>  application_pgp-signature_part
> < 1KViewDownload

God! how can I miss it.
I think 2 years of separation did the trick.
Thanks mate.
You really are a life saver.

Juha Nieminen

12/13/2008 4:40:00 PM

0

Abhayks wrote:
> I am retuning to "C" after a long time and my basics are shatterred.

This is a C++ group. Different language.

Vidar Hasfjord

12/14/2008 12:12:00 AM

0

On Dec 13, 2:57 am, Abhayks <abhaysa...@gmail.com> wrote:
> On Dec 13, 10:13 am, Sam <s...@email-scan.com> wrote:
> > Abhayks writes:
> > [...]
> > > 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.
>
> God! how can I miss it.

Maybe it would be just as well. There are better ways in C++ than
using pointers and manual memory handling. If you really did want to
return an array, as your subject title says, you can do it safely like
this:

typedef tr1::array <char, 255> Array;

Array foo () {
Array a = {'a', 'a', 'a', 'a', 'a'};
//strcpy (a.begin (), "Hello World");
return a;
}

int main () {
Array a = foo ();
cout << a.begin () << endl;
return 0;
}

But, of course, your example calls for std::string:

string foo () {
return "aaaaa"; // "Hello World";
}

int main () {
string s = foo ();
cout << s << endl;
return 0;
}

Regards,
Vidar Hasfjord