Solved

Passing var to a charpointer with the & prefix

Posted on 2011-02-24
6
277 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
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…
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 how to create, access, and change arrays in the C programming language.

776 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