Solved

Passing var to a charpointer with the & prefix

Posted on 2011-02-24
6
290 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
Independent Software Vendors: 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 174
How to copy documents folder to network folder automatically as backup? 15 95
smtp c source code 7 105
IIS Log files on Exchange 2013 server 6 236
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

735 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