Thread: Basic pointer in function question

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Basic pointer in function question

    Hi everybody, I am pretty new to C programming, I do know Java, but now I've started with programming PIC processors. So Obviously I need to get familiar with pointers, which arent used in Java.

    It's a fairly simple question, I needed a program to return an array, and I found the way how to do it on the internet. Of course most ppl would stop here and just be happy that it works, but I'd rather know how it works, seeing as it doesnt make sence to me.

    In the following function, I give an array (int i) but the function asks for a pointer (int *i). Now I just don't get how it works? Could someone please explain me the way this works? Thank you.

    Code:
    #include <stdio.h>
    
    int Functie(int *i);
    
    main() {
     int i[2];
     Functie(i);
     printf("%d   %d", i[0], i[1]);
    }
    
    int Functie(int * i){
    /* Do something with i, for example: */
     i[0] = 2;
     i[1] = 3;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you pass the array to the function, the language automatically converts your argument into a pointer to the first element of the array. This is true for most places where you would place the name of an array on the right-hand side of a statement (eg a = b where b is an array).
    Hence you get a pointer to the first element which you can dereference and modify.

    You may also wish to look at SourceForge.net: Implicit main - cpwiki
    And for security purposes, to avoid a buffer overrun, you may wish to pass along the size of your array and place checks so that you do not write or read beyond the array bounds. C will not stop you from doing so, unlike Java, but it's undefined behavior and a security issue (see link).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The variable denoting an array is also a pointer to its first element, hence when you call Functie(i) you are passing a pointer to i[0] (i.e. type int *).

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Ah yes I get it now, thank you! And elysia, thank you, but seeing as its a function which will always returns 2 value's for the array, I don't think this will be a problem

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could say that i with an index is specific about which int is referred. i on it's own is not, on the other hand, I'd say it makes more sense to think about it as the address where the array starts than any specific index of the array since none is given. If you do this:

    Code:
    printf("%p, %p\n", i, &i);
    Then i and &i is equivalent.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by claudiu View Post
    The variable denoting an array is also a pointer to its first element, hence when you call Functie(i) you are passing a pointer to i[0] (i.e. type int *).
    No, an array is not a pointer to its first element. You can verify this by printing sizeof(name_of_array). Similarly, if you do it with a pointer to the array, you will probably get 4 or 8 depending on system.

    Quote Originally Posted by Subsonics View Post
    You could say that i with an index is specific about which int is referred. i on it's own is not, on the other hand, I'd say it makes more sense to think about it as the address where the array starts than any specific index of the array since none is given. If you do this:

    Code:
    printf("%p, %p\n", i, &i);
    Then i and &i is equivalent.
    Yes, but the types are different.
    The first us a pointer to the first element of the array, and the second is a pointer to an array of n elements.

    Origin: Mayhap this be true for this particular functions, but once you get to writing real functions, that will not be so certain. Always be safe than sorry.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    Yes, but the types are different.
    The first us a pointer to the first element of the array, and the second is a pointer to an array of n elements.
    But the first is also an array of n elements, and the second is also a pointer to the first element.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did I get you right?
    Code:
    int arr[n];
    printf("%p", arr); // Type of arr is int*.
    printf("%p", &arr); // Type of &arr is (int*)[n]
    They will have the same address, but the type is very different.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, but both are pointers and n in this scenario is element zero. I realize that you would have to specify the element if you did intend to pass it to a function though.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, n is the number of elements.
    If you dereference the first, you get the first element of the array.
    If you dereference the second, you get the array itself (not an element).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Subsonics View Post
    Yes, but both are pointers and n in this scenario is element zero. I realize that you would have to specify the element if you did intend to pass it to a function though.
    try to see

    i+1

    and

    &i + 1

    maybe you understand the difference
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Origin
    but seeing as its a function which will always returns 2 value's for the array, I don't think this will be a problem
    True, if you are always careful, but it is more common to either have the pointer point to a lone element, or to the first element in some sequence of elements with the number of elements provided (or an end pointer provided, or a special terminating value used). Have you considered using a struct instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    No, n is the number of elements.
    If you dereference the first, you get the first element of the array.
    If you dereference the second, you get the array itself (not an element).
    Yes but when I said in this scenario I meant:

    Code:
    int arr[n];
    printf("%p", &arr); // Type of &arr is (int*)[n]
    I also said that you would have to specify the element if you intended to pass element one to a function as in:

    Code:
    function(&i[0]);
    i is a pointer, that is all I'm trying to illustrate since i gives up it address with %p in the same way that &i does. I could also have added this I suppose.

    Code:
    int i[10];
    int *k = i;

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Quote Originally Posted by laserlight View Post
    True, if you are always careful, but it is more common to either have the pointer point to a lone element, or to the first element in some sequence of elements with the number of elements provided (or an end pointer provided, or a special terminating value used). Have you considered using a struct instead?
    Hmm no? I did see some examples, when I looked it up how to pass arrays, using structs. But seeing as they seemed longer in lines, and you dont have structs in Java, I just skipped those examples.

    Is there an advantage to using structures?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you find yourself in a situation where you need to pass a lot of data around, a struct is great. Consider information about a book. You could group it into a struct that describes it and pass it around.
    Consider a book-keeping system. You might want a structure that contains relevant information about a specific individual or whatever the system is book-keeping.
    Basically, they are classes without member functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  3. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM