Solved

*& question

Posted on 2003-11-06
9
532 Views
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;
pBlockTableRecord->getName(rname);
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];
pBlockTableRecord->getName(name);

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

What is the mystic *& mean?

Thanks for your help.

Jzzhang
0
Comment
Question by:jzzhang
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:jkr
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'.

Use

strcpy ( name, pname);

before, and it will work.


0
 
LVL 86

Expert Comment

by:jkr
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);
0
 
LVL 1

Expert Comment

by:fsign21
ID: 9694911
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 10

Accepted Solution

by:
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
Example
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

HTH

Amit
0
 
LVL 86

Expert Comment

by:jkr
ID: 9695551
>>Here's the explanation

Haven't I already posted that?
0
 
LVL 2

Author Comment

by:jzzhang
ID: 9696707
Amit:

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.

Jzzhang
0
 
LVL 2

Author Comment

by:jzzhang
ID: 9698372
Amit,

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;

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

Then, how to accept your answer?

Jzzhang
0
 
LVL 10

Expert Comment

by:Sys_Prog
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
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9698802
Here's the link for the help

http://www.experts-exchange.com/help.jsp#hi68
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now