Two questions about pointers

This is a discussion on Two questions about pointers within the C Programming forums, part of the General Programming Boards category; I have a question about pointers. What do they enable passing VARIABLES to FUNCTIONS by (ex. value, reference)? ------------------------------------------------------------------------------- I ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    11

    Two questions about pointers

    I have a question about pointers. What do they enable passing VARIABLES to FUNCTIONS by (ex. value, reference)?

    -------------------------------------------------------------------------------

    I also have a code below that looks right, but is wrong (numbers are correct, but code doesn't work).

    Code:
    int *int_ptr;
    int primes[] = {2,3,5,7};
    int composite[] = {4,6,8,9};
    
    int_ptr = primes;
    composite = int_ptr;
    Is it that both "primes" and "composite" cannot both be assigned to int_ptr?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    For the first question, what do you think the answer is? What conclusion does the process of elimination lead you to?

    #2: You cannot assign to an array.
    You can assign stuff to the array elements, but you cannot assign to the array itself.
    If you want to copy stuff in then you would generally use a loop.
    If you want to point to either of the two arrays, then you have the last assignment statement back-to-front, and you'd probably want some if-else logic.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by Dynesclan View Post
    I have a question about pointers. What do they enable passing VARIABLES to FUNCTIONS by (ex. value, reference)?
    .

    C doesn't allow passing by reference. Parameters are pushed onto a stack, which is popped when the function returns. But what you can do is pass a pointer to a variable that is declared in caller. Then the fucntion can write throught the pointer to it.
    This is how you return more than one value from a function.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    11
    Quote Originally Posted by Malcolm McLean View Post
    .

    C doesn't allow passing by reference. Parameters are pushed onto a stack, which is popped when the function returns. But what you can do is pass a pointer to a variable that is declared in caller. Then the fucntion can write throught the pointer to it.
    This is how you return more than one value from a function.
    Then pointers do pass by value instead of by reference in caller?
    Last edited by Dynesclan; 06-10-2012 at 12:56 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Yes, the pointer itself is passed by value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A few questions on pointers
    By dgs012 in forum C Programming
    Replies: 1
    Last Post: 12-01-2010, 09:53 AM
  2. Questions about pointers and malloc
    By H`eya in forum C Programming
    Replies: 4
    Last Post: 04-19-2010, 08:50 PM
  3. Pointers confusing me (Newbie questions)
    By klawson88 in forum C Programming
    Replies: 12
    Last Post: 04-30-2009, 02:14 AM
  4. questions about pointers
    By radxxx in forum C Programming
    Replies: 3
    Last Post: 04-21-2009, 12:38 PM
  5. Some questions about pointers
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 03:25 PM

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