Solved

Passing var to a charpointer with the & prefix

Posted on 2011-02-24
6
267 Views
Last Modified: 2012-05-11
In C (not C++)

If I have a string field within a structure e.g.
char name[35];

and i havea function that takes a charpointer e.g.
void clean_string(char *buff)

Are the following two calls the same (i.e. wil they both work):
clean_string(customer.name);
clean_string(&customer.name);

I have stumbled upon some previously written C code that uses BOTH ways of calling this function and need to know if I have to fix the calls that pass the &customer_name.

Thanks.
0
Comment
Question by:Stephen Kairys
  • 3
  • 3
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 350 total points
ID: 34972002
>> Are the following two calls the same (i.e. wil they both work):
>> clean_string(customer.name);
>> clean_string(&customer.name);

Yes. Both will work. The first is more accurate though.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 34972067
>>Yes. Both will work. The first is more accurate though.

And by "accurate" you mean.....?
Tks
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 350 total points
ID: 34972186
customer.name has the type char [35], and it transparently reverts to a pointer to the first char in the array (ie. a char*) in a context where a char* is expected, like in :

>> clean_string(customer.name);

Taking the address of customer.name though gets you the address of the entire array (ie. a char (*) [35]). The reason it will work, is because the address of the array is the same as the address of the first item in the array. It's just less accurate because of the type confusion.

None of the compilers I know have a problem with either way though.


Note that &(customer.name[0]) is also accurate, and gives the same as the first option above.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 34972240
OK Thanks. I'm leaving that code as is. I'm not aware of any reported problems with it so if it ain't broke... :)

Tks again.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34972627
No need to fix it, no.

Just be aware of the distinction. It can still come back and bite you, when (for some reason) customer.name is changed to be a char* instead of an array of char. When that happens, the second case (using &customer.name) will not work any more.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 34972635
Thanks for the clarification. That's good info to have.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

911 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

25 Experts available now in Live!

Get 1:1 Help Now