Thread: Passing Pointers

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Passing Pointers

    Hi, I'm just wondering if this is the correct way to pass a pointer in a for loop:

    say I have the following variables. (don't worry about functions not having prototypes etc, this is only an example).

    Code:
    #include <stdio.h>
    
    int *firstPointer;
    int *secondPointer;
    int x = 0;
    int array[5];
    int aFunctionCall();
    
    int main()
    {
    firstPointer = &x; // firstPointer now points to x, x holds the value 0.
    aFunctionCall();
    if (x == 2)
       {
            array[(*firstPointer)-1] = 5; // basically says 'array[2-1] = 5'; aka 'array[1] = 5'
    }
    
    int aFunctionCall()
    for (*firstPointer=1; *firstPointer <= 3; *firstPointer++) // sets the value of x to 1 etc?
    {
         if (*firstPointer == 2) // basically saying if the value of x is 2?
         {
              return 0;
         }
    }

    So basically at the end of this program array[1] will be holding the value 5, if i'm correct :P
    so yeah, hope i didn't confuse everyone too much -_- thanks.
    Last edited by pobri19; 05-26-2008 at 11:10 PM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Where did 'a' come from?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it is not good since it unnecessarily relies on global variables. Also, a is never declared. Honestly, if you want to give a code example, give one that works, or that you did your best to try and get it to work.
    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

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Uh I just wrote that from the top of my head, and it's not really necessary because i'm not asking if the code is right, i'm asking if the idea is right.. sorry if i left something out :>

    EDIT: gah sorry, i got a bit confused, a is meant to be *firstPointer, i'm trying to compare this off something i've already written and it's using different variable names etc. ill fix it up.

    I think it's all fixed now, sorry.
    Last edited by pobri19; 05-26-2008 at 11:11 PM.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    53
    Then yes, at the end of your program array[1] will be set to 5. What exactly is your question..?

    --
    Computer Programming: An Introduction for the Scientifically Inclined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 10:14 AM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  4. Passing pointers
    By cyberCLoWn in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2004, 12:17 PM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM