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

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

    Assisted Solution

    Yes, you are correct, the two calls to that function are the same.
    LVL 13

    Accepted Solution

    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.
    LVL 4

    Author Comment

    by:Stephen Kairys
    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
    Thanks for the points.  I didn't think the compiler would mind.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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 ( They will have you believe that Unicode requires you to use…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    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.

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now