Thread: Pointers: Call by value

  1. #16
    Quote Originally Posted by Beast()
    Boy, did my teacher ever bypass that explanation.
    Please don't try to deceive us and don't blame your teacher. We all know your records.

    Just do your best and we will help you.
    Emmanuel Delahaye

    "C is a sharp tool"

  2. #17
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by Emmanuel Delaha
    Please don't try to deceive us and don't blame your teacher. We all know your records.

    Just do your best and we will help you.
    I appreciate you saying to give me a second chance Emmanuel.

    But I am not trying to deceive anyone. My teacher was a crappy one and one of the reasons why I left (besides the fact that I was a damn bum). In fact, he used to use the void main () that you all so abhor fairly often.

    However I'm sure if I would've stuck with it, I would've learned this along the way.

    But at this point, thanks to my actual hard work, and thanks to you kind and knowledgable people who have answered my questions, I have covered everything we did in that class and am now surpassing it.

    -edited to say I used to be a damn bum
    Last edited by Beast(); 08-07-2004 at 03:40 AM.

  3. #18
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by quzah
    Probably every time you ever have a question. You see, it wasn't me who skipped out on your classes and didn't pay attention. That was you. As such, I see it as a good idea to make an example of you for all the other would-be dumbasses out there. Perhaps they'll learn from your mistakes.

    Quzah.
    Fair enough.

    I won't be asking beginner questions forever though.

  4. #19
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by quzah
    Code:
    #include <stdio.h>
    
    void foo( int *a )
    {
            printf("a is    : %p\n", (void *)a );
            printf("&a is   : %p\n", (void *)&a );
            printf("&a[0] is: %p\n", (void *)&a[0] );
    }
    
    void bar( int a[] )
    {
            printf("a is    : %p\n", (void *)a );
            printf("&a is   : %p\n", (void *)&a );
            printf("&a[0] is: %p\n", (void *)&a[0] );
    }
    
    int main ( void )
    {
            int a[5] = {0}, *b = a;
    
            printf("In main:\n");
            printf("a is    : %p\n", (void *)a );
            printf("&a is   : %p\n", (void *)&a );
            printf("&a[0] is: %p\n", (void *)&a[0] );
            printf("\n");
    
            printf("In foo(a):\n");
            foo( a );
            printf("\n");
    
            printf("In bar(a):\n");
            bar( a );
            printf("\n");
    
            printf("In foo(b):\n");
            foo( b );
            printf("\n");
    
            printf("In bar(b):\n");
            bar( b );
            printf("\n");
    
            return 0;
    }
    
    
    In main:
    a is    : 0xbffffce0
    &a is   : 0xbffffce0
    &a[0] is: 0xbffffce0
    
    In foo(a):
    a is    : 0xbffffce0
    &a is   : 0xbffffcc0
    &a[0] is: 0xbffffce0
    
    In bar(a):
    a is    : 0xbffffce0
    &a is   : 0xbffffcc0
    &a[0] is: 0xbffffce0
    
    In foo(b):
    a is    : 0xbffffce0
    &a is   : 0xbffffcc0
    &a[0] is: 0xbffffce0
    
    In bar(b):
    a is    : 0xbffffce0
    &a is   : 0xbffffcc0
    &a[0] is: 0xbffffce0
    
    The name of an array is in effect a pointer to its first element. You'll see that in every example here. What changes is the address of the array or pointer itself. When passed to a function, "array[]" is changed to "*array", or, a pointer to that type. This is illustrated by the functions foo and bar.

    The only thing different in the output is as it should be: The address of the pointer itself is different then what you get inside main. The reason is, it's a different pointer. If you'll recall, if you can, a pointer is a variable that stores an address. Thus, the pointer itself has an address. Since it is an actual different variable, it has a (shock!) a different address. This you'll see as the second line in the output of functions foo and bar.

    Quzah.
    Thank you for giving the code and explanation.

    I understand the second portion of the post, that the reason for a different address is because it's a different variable. But why is it the same in the main?

    You said the name of an array is in effect a pointer to its first element, so it's already a pointer in main. So why is the address of the pointer, holding the same address of it's variable/address it points to? Does it point to itself?

    The address changes in the function because the pointer that is the array's name is passed by value, but why it's the same in main I don't understand.

    Thanks again, from a dumbass.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Beast()
    You said the name of an array is in effect a pointer to its first element, so it's already a pointer in main. So why is the address of the pointer, holding the same address of it's variable/address it points to? Does it point to itself?

    The address changes in the function because the pointer that is the array's name is passed by value, but why it's the same in main I don't understand.
    Perhaps some reading of this FAQ will help you. The array name isn't actually a pointer. If it were, you could do:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
            int arrayname[2] = { 1, 2 };
    
            printf("%d\n", *arrayname ); /* print "1" */
            arrayname++; /* if this were really a pointer, you could do this... */
            printf("%d\n", *arrayname ); /* print "2" */
    
            return 0;
    }
    But, since it really isn't a pointer, this won't compile.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    The FAQ was excellent. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Pointers trouble
    By saahmed in forum C Programming
    Replies: 39
    Last Post: 03-24-2006, 04:08 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM