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

x
?
Solved

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

Posted on 2012-08-14
4
Medium Priority
?
493 Views
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
and
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.

Thanks
Steve
0
Comment
Question by:Stephen Kairys
  • 2
4 Comments
 
LVL 36

Assisted Solution

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

Accepted Solution

by:
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.
0
 
LVL 4

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!
0
 
LVL 13

Expert Comment

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

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