We help IT Professionals succeed at work.

C: Passing array  to function vs. passing a POINTER to the same array

Stephen Kairys
Medium Priority
Last Modified: 2012-08-14
Hey everyone,
I'm telling myself the following MUST be OK but have a nagging doubt so I want to be sure. :)

In C:

1. SupposeI have a structure: CUSTOMER
and an array: (Global to my C file)
CUSTOMER  People[100];

2. I then have (local to function in the same C file):
CUSTOMER *PtrPeople = People;

3. Now I have a called functon whose header is:
void ProcessStuff (PEOPLE *ppl)

4. My exis ting code (which someone else wrote) has the following calls:
ProcessStuff(People);  // call #1, using the array
ProcessStuff(PtrPeople); // call #2 using the ptr to the array.

From what I recall, these should be one and the same since I'm dealing with an ARRAY,
but would appreciate either confirmation or correcting me if I'm wrong.

Watch Question

mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015
Yes, you are correct, the two calls to that function are the same.
Yes, I think it's OK but to be sure I would try this and see what the compiler says.

ProcessStuff ( &People );  // for call #1.

The reason I think it's OK is because People, by itself, is the address of the array.  By contrast People[0] is the first structure in the array.  If you were to try
ProcessStuff ( People[0] ) that would be an attempt to call the function by value and the compiler would want to put that structure on the stack.  Of course, the compiler would recognize that the function defintion wants an address, not a structure.

In short, if this program compiles (assuming there isn't some strange casting going on), you should be OK.
Stephen KairysTechnical Writer - Consultant


mccarl - Thank you. As I suspected.

hmccurdy - Thanks also. I tried the call with the &People
ProcessStuff ( &People );  // for call #1.
and the compiler did not mind! :)

Points to you both....
Tks again. EE rocks!
Thanks for the points.  I didn't think the compiler would mind.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.