Pointer Terminology Question

This is a discussion on Pointer Terminology Question within the C Programming forums, part of the General Programming Boards category; Hi, I'm not 100% confident on the following terms. I've seen them used alot and never knew exactly knew what ...

  1. #1
    .........
    Join Date
    Nov 2002
    Posts
    303

    Pointer Terminology Question

    Hi, I'm not 100% confident on the following terms. I've seen them
    used alot and never knew exactly knew what they meant,
    so I continued to think of things like "point to this",
    "follow the pointer to that" etc. But now that I have a better
    understanding of how pointers work I'd like to know what exactly
    it means when these terms are used.
    Passing by value.
    Passing by reference.
    Dereference.
    Direct Reference.
    Indirect Reference.


    Here is some sample code, are my
    comments correct? Any help would be great.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void func1(int *pointer);
    void func2(int array[]);
    void func3(int number);
    
    int
    main(void)
    {
    	int my_array[1000];
    	int variable = 10;
    	int *x;
    
    	x = malloc(sizeof(int));
    
    	/* Is this considered dereferencing *x? */
    	*x = 99; 
    
    	free(x);
    
    	/* Passing by reference? */
    	func1(&variable);
    	func2(my_array);  
    
    	/* Passing by Value?     */
    	func3(variable);  
    	
    	return (0);
    }
    
    void func1(int *pointer)
    {
    	int value = 5;
    
    	/* Dereferencing *pointer? */
    	*pointer = value; 
    }
    
    void func2(int array[])
    {
    	int subscript = 0;
    
    	/* Dereferencing array? */
    	array[subscript] = 15; 
    }
    
    void func3(int number)
    {
    	/* Is this a direct reference to number? */
    	number = 10; 
    }
    Also am I correct in assuming that an indirect reference to a
    variable is the same thing as dereferencing the pointer that
    points to its address? Like...
    Code:
    int *x;
    int y = 10;
    
    x = &y;
    
    /*Dereferencing *x and also an indirect reference to y? */
    *x = 15;
    Again any help would be great, the book I'm reading doesn't
    use any of these terms, these are just terms
    I've heard used on IRC and on the boards sometimes.
    Last edited by SourceCode; 03-06-2003 at 11:09 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Passing by value.
    This is how C passes function arguments, the value of the argument is copied and placed in the variable supplied by the definition:
    Code:
    int a = 10;
    
    f ( a ); /* The value, 10, is passed to f */
    .
    .
    .
    void f ( int var ) /* The value, 10, is given to var */
    {
      ...
    }
    >Passing by reference.
    Passing by reference is when you give the function not a copy of the value, but another name that is the same as the original object, a synonym if you will. So changing it changes the original. C does not have this feature, so I'll use C++:
    Code:
    int a = 10;
    
    f ( a ); // A reference, a different name that effects a, is passed
    .
    .
    .
    void f ( int& var ) // var is a different variable, but it's the same as a
    {
      ...
    }
    >Dereference.
    Used for pointers, dereferencing is when you follow a pointer to the address at which it points and obtain the value at that location.

    >Direct Reference.
    C does not have this feature, so once again I'll use references from C++. Direct reference is when you have a variable of another name that can be taken as a synonym for the original, much like a typedef:
    Code:
    typedef int my_int; // my_int is now int, but int still exists
    
    int& my_a = a; // my_a is now a, but a still exists
    >Indirect Reference.
    This is what C uses, pointers to an object are indirect references to that object. The manner in which C simulates passing by references is to pass the value of a pointer (the address of the original object) to a function, then that address is assigned to another pointer by value but when dereferenced, the original object can still be accessed.

    -Prelude
    My best code is written with the delete key.

  3. #3
    .........
    Join Date
    Nov 2002
    Posts
    303
    Thank you very very much Prelude, you have cleared up alot for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21