Thread: Two questions about pointers

  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,318
    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
    505
    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.
    https://github.com/MalcolmMcLean


  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 11:56 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, the pointer itself is passed by value.
    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

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, 07:50 PM
  3. Pointers confusing me (Newbie questions)
    By klawson88 in forum C Programming
    Replies: 12
    Last Post: 04-30-2009, 01:14 AM
  4. questions about pointers
    By radxxx in forum C Programming
    Replies: 3
    Last Post: 04-21-2009, 11:38 AM
  5. Some questions about pointers
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 02:25 PM