• C

Pointer allocation in function

Hi! I need some help with C pointers...

I try do something like that:

void func(char **f)
{

//      *f = (char **)malloc(3 * sizeof(char *));

        f = (char **)malloc(3 * sizeof(char *));
        f[0] = (char *)malloc(10 * sizeof(char));
        f[1] = (char *)malloc(10 * sizeof(char));
        f[2] = (char *)malloc(10 * sizeof(char));
        strcpy(f[0], "aaaaa");
        strcpy(f[1], "bbbbb");
        strcpy(f[2], "ccccc");
        printf("%s - %s - %s\n", f[0], f[1], f[2]);
        }
       
int main()
{
        char **f;

//      f = (char **)malloc(3 * sizeof(char *));

        func(f);
        printf("%s - %s - %s\n", f[0], f[1], f[2]);
        return 0;
        }

When I discomment one of two commented lines its work fine.

I know... this is a simple concept in pointers, but I'am desperate...

Thanks
ragysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
This question is the same as:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21213626.html

Another alternative is:

void func(char ***g)    /* triple asterisc needed */
{
        char **f;  

        f = (char **)malloc(3 * sizeof(char *));

        f[0] = (char *)malloc(10 * sizeof(char));
        f[1] = (char *)malloc(10 * sizeof(char));
        f[2] = (char *)malloc(10 * sizeof(char));
        strcpy(f[0], "aaaaa");
        strcpy(f[1], "bbbbb");
        strcpy(f[2], "ccccc");
        printf("%s - %s - %s\n", f[0], f[1], f[2]);
        *g = f;        
}
       
int main()
{
        char **f;

        func(&f);    /* notice the ampersand */
        printf("%s - %s - %s\n", f[0], f[1], f[2]);
        return 0;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dennis_georgeCommented:
Repeated Question !!!!!

Repeated Answer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

:-)
Its the simple concept of "Pass by Value" which you are missing here...... whatever you pass to a function it will be passed as pass by value....

So.............

char **f  = NULL; // pointer to pointer

func(f) ; // Here you are passing the value of pointer to pointer i.e. NULL

And in your function you are allocating memory and assiging it to f .... And since it will be a local copy of the funciotn 'func' it won't be reflected in main().......

i.e. after function call 'f' will be NULL .... whatever you do in func.....

cheers
Dennis
0
ragysAuthor Commented:
Sorry Jaime, but in another question I accept the answer before I test the solution...

But, its a good solution too...


Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.