?
Solved

Passing var to a charpointer with the & prefix

Posted on 2011-02-24
6
Medium Priority
?
300 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
[X]
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
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1400 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 1400 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

Technology Partners: 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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

777 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