[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2012-08-14
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.

Question by:Stephen Kairys
  • 2
LVL 36

Assisted Solution

mccarl earned 1000 total points
ID: 38294455
Yes, you are correct, the two calls to that function are the same.
LVL 13

Accepted Solution

Hugh McCurdy earned 1000 total points
ID: 38294471
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.

Author Comment

by:Stephen Kairys
ID: 38294510
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!
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 38294733
Thanks for the points.  I didn't think the compiler would mind.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

834 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