*& question

Posted on 2003-11-06
Last Modified: 2010-05-18
Hi! Experts,

When I work on ObjectArx which is a library by AutoDesk to program with AutoCad, I met a defination like this:

Acad::ErrorStatus getName(const char*& pName) const;

I don't understand what char*& mean. I did the following:

char name[20];
char* rname = name;
acutPrintf("\nThe block name is : %s", rname);

acutPrintf is similar to printf. It works.

But if I write

acutPrintf("\nThe block name is : %s", name);

It gives me junk chars.

If I write

char name[20];

The compiler gives me an error saying it can not convert char[20] to char*&.

What is the mystic *& mean?

Thanks for your help.

Question by:jzzhang
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
LVL 86

Expert Comment

ID: 9694860
>>I don't understand what char*& mean

This means that the function takes a reference to a pointer. Technically speaking, the result is similar to using a 'char**'

>>if I write
>>acutPrintf("\nThe block name is : %s", name);
>>It gives me junk chars.

Yes, because 'pname' no longer points to 'name'.


strcpy ( name, pname);

before, and it will work.

LVL 86

Expert Comment

ID: 9694875
BTW, the behaviour would be the same if you used

void ChangeThePointer ( char*& p) {

   p = "ChangeThePointer";

char* p = "test";

printf ( "before: %s\n", p);

ChangeThePointer ( p);

printf ( "after: %s\n", p);

Expert Comment

ID: 9694911
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Accepted Solution

Sys_Prog earned 250 total points
ID: 9695530
Here's the explanation

In C, when u need to modify the contents of a variable, u pass the address of the variable and recieve it in a pointer variable of the same type. Using the pointer u can change the contents of your variable.

But if u need to change the contents of the pointer itself, i.e. make the pointer point to something else using a function, then u need to pass the address of the pointer variable, For this case the function would be taking a pointer to a pointer argument
void f ( int ** p ) ;
Inside f(), u would have use the cumbersome syntax for referring to the double pointer

Now C++ makes this easy by letting u pass a pointer by reference and that is what u asked for
getName(const char*& pName)

means pName is a reference to a pointer to a char const

Regarding ---------The compiler gives me an error saying it can not convert char[20] to char*&.====

Any statically declared array [e.g. char a[20]] is by definition a const pointer. Now Your function is trying to pass a reference to the pointer, thus it runs the risk of changing it i.e. making it point to something else, but this is not allowed for a const pointer OR an array.

That's the reason for the error


LVL 86

Expert Comment

ID: 9695551
>>Here's the explanation

Haven't I already posted that?

Author Comment

ID: 9696707

It looks like
getName(const char*& pName)
means pName is a reference to a pointer to a char const

I will test it.

Be back soon.


Author Comment

ID: 9698372

I tested it. It works. So char*& should be interpreted as a reference to a char pointer.

Here is the small test code:

bool AllocStr(char*& szStr)
      szStr = new char[15];
      lstrcpy(szStr, "This is a test");
      return 1;

int main()
      char* psz;

      wsprintf(g_szMsg, "Allocated string (%s) length is %d", psz, lstrlen(psz));
      delete psz;

Then, how to accept your answer?

LVL 10

Expert Comment

ID: 9698789
I guess when u open this question, against each of the answers, u get a button for accepting a particular answer.

U can also give grades to the answer.

read the help for Accepting a Answer
LVL 10

Expert Comment

ID: 9698802
Here's the link for the help

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question