Thread: Quick Pointer Question

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    Quick Pointer Question

    Hey all quick pointer question that I am not understanding...

    The following is a quick code snippet of my full program.

    Code:
    void main()
    {
    **CODE**
    int array[] = {10,20};
    int i;
    int loc = 0;
    
    
    i = function(array, loc);
    }
    
    int function(int* array, int location)
    {
    
    int number = 1;
    int j = 0;
    
    
    j = array[location] + number;
    
    return(j);
    }
    Again this is not my actual code so there may be syntx errors but the idea of what I want to do is there. I want to pass an array, add it to an internal value and then return that sum. However whenever I do this I am always recalling the memory location the variable is point to and not the value stored at that location. How to I solve this? Thank you for the help!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    your understanding of the code snippet is the exact opposite of what's actually going on

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Toonzaka View Post
    Again this is not my actual code so there may be syntx errors but the idea of what I want to do is there.
    Not wanting or needing to post your original code is a good thing.

    Not bothering to write a simple example of working code which you have tested to eliminate the errors you are aware of is NOT a good thing.

    This code appears to me to do what you want, however, I did not bother to test it either so cannot say for sure what may be wrong, if anything.

    Post code you have tested so that you can demonstrate the problem rather than just saying "in some other piece of code that may be sort of like this I maybe have these problems".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Toonzaka, this is your program, with just the smallest amount of code to print up the returned value from the called function.

    It works, just fine.

    Code:
    #include <stdio.h>
    
    void main()
    {
    //**CODE**
    int array[] = {10,20};
    int i;
    int loc = 0;
    
    
      i = function(array, loc);
      printf("%d", i);
      getchar();
    
    
    }
    
    int function(int* array, int location)
    {
    
    int number = 1;
    int j = 0;
    
    
    j = array[location] + number;
    
    return(j);
    }
    Output is 11. (10 + 1).

    Arrays in C are zero based.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. Quick Pointer question
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2006, 07:36 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 11:36 PM
  5. Declaring a Pointer (quick question)
    By viciousv322 in forum C Programming
    Replies: 4
    Last Post: 12-16-2005, 11:27 AM