Solved

Passing var to a charpointer with the & prefix

Posted on 2011-02-24
6
259 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

707 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

13 Experts available now in Live!

Get 1:1 Help Now