Pointer to pointer in C

Hi Experts,

When using pointer to pointer would the following is valid?
char *value;
int ret = function(&value);

where as function is declared as 
int function(char **val);

Open in new window


Can we get the address of an uninitialized pointer?
ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ozoCommented:
yes
0
ambuliAuthor Commented:
Thank you.
0
evilrixSenior Software Engineer (Avast)Commented:
An uninitialized pointer still has storage, it's just the storage contains an arbitrary value until you initialise it. There is nothing magic about a pointe, it's just a type, just like int is just a type. If you create an int without initializing it storage is still allocated for it; so it is for pointers, too.
0
sarabandeCommented:
to add to above answers:

a pointer to pointer as an argument to a function is the c way to return a pointer to the calling function.

int function(char ** ppval)
{
     *ppval = new char[256];
     strcpy(*ppval, "hello world");
     return (int)strlen(*ppval);
}

int main()
{
       char * p = NULL;
       function(&p);
       if (p != NULL)
            std::cout << p << std::endl;

Open in new window


in c++ you would/could use a reference to pointer instead (or use reference to string class object):

int function(char *& pval)
{
     pval = new char[256];
     strcpy(pval, "hello world");
     return (int)strlen(pval);
}

int main()
{
       char * p = NULL;
       function(p);
       if (p != NULL)
            std::cout << p << std::endl;

Open in new window


Sara
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
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.