Thread: Passing a pointer and passing an adress, I am really confused...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Unhappy Passing a pointer and passing an adress, I am really confused...

    Ok... I know this is simple, but It bothers me, always has...
    if I were to have a function which passed:
    test(int & one);
    and another which passed
    test2(int * two);

    what does it mean exactly, how would I use the data being passed in? what would the data be representing?

    I know this is simple... I just have issues with pointers.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I suggest you first read the tutorial on pointers we have here on this site.
    Cprogramming.com Tutorial: Pointers
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It should be noted that unless you've been reading C++, the biggest difference between the two is that the second one is possible and the first one isn't (assuming these are function prototypes).

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Code:
    test(int & one);
    In C++ (not C), this is a reference variable. It functions similarly to a pointer except they do not need to be explicitely dereferenced. Take this pointer operation for example.

    Code:
    int some_variable;
    int *ptr = &some_variable;
    *ptr = 10;
    Using a reference, this could be done more simply, e.g.

    Code:
    int some_variable;
    int &ref = some_variable;
    ref = 10;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. pointer passing
    By stautze in forum C Programming
    Replies: 1
    Last Post: 07-15-2002, 12:44 AM