Thread: Filling pointer with array value

  1. #1
    Registered User
    Join Date
    Mar 2009

    Filling pointer with array value

    Can someone please explain to me why this does not work?

    #include <iostream>
    using namespace std;
    int main () {    
        int i;   
        int n [] = {3, 2};
        int * p;
        for(int i = 0; i < 2; i++)
        p [i] = n[i];
        return 0;

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Ok, first off, "this doesn't work" is a bit vague. But anyway, you're in luck because I spotted your mistake anyhow.

    int * p; // <-- This declares a pointer to an int. Since you're not initializing this pointer it just points to a RANDOM memory address. This can be a problem.


    p [i] = n[i]; // <-- Here you are dereferencing the same pointer that we've already established is pointing to a RANDOM piece of memory. This is undefined behavior!

    EDIT: So the lesson to take away from this is that you need to point your pointer at something first. For instance, you could point it at a second array (although in your example you might then just as well use that array directly instead of via a pointer).

    int secondArray[ 2 ] = {};
    int* p = secondArray;
    p[ 0 ] = n[ 0 ];
    p[ 1 ] = n[ 1 ];
    Last edited by antred; 05-18-2012 at 11:55 AM.

  3. #3
    Registered User
    Join Date
    May 2012
    Bonn, Germany
    Another thing:
    You first declare "int i" outside the loop and then again in "for(int i".
    This is probably not what you want, as you will have two "i" variables. One will become inaccessible after the for loop has completed.

    Visit my project: Derivative Calculator

  4. #4
    Registered User
    Join Date
    Mar 2009
    OK, thanks. What if I have:

    P = new int [3];
    this, on the one hand, is an initialisation but also not an initialisation in the sense that we are merely initialising a pointer with 3 integers. But exactly what is stored in there is unknown. So, is this considered an initialisation, or I still I have to go further and fill these pointers with values, something like:

    for(int i = 0; i < 3; i++)
    p[i] = i;

  5. #5
    Join Date
    Dec 2007
    After new, you have three pointers to valid locations in memory capable of holding an integer value. What's in those is not known until you actually set the values in those locations as you've actually shown.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filling an array..
    By kromagnon in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 06:53 PM
  2. Filling up a 2D (or 3D) array.
    By omnificient in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 01:22 PM
  3. Filling an array
    By GCNDoug in forum C Programming
    Replies: 18
    Last Post: 04-25-2007, 02:46 PM
  4. Filling an array
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 02:02 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 03:11 PM