Filling pointer with array value

This is a discussion on Filling pointer with array value within the C++ Programming forums, part of the General Programming Boards category; Can someone please explain to me why this does not work? Code: #include <iostream> using namespace std; int main () ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Filling pointer with array value

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

    Code:
    #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
    Location
    Germany
    Posts
    245
    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
    Location
    Bonn, Germany
    Posts
    16
    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
    Posts
    102
    OK, thanks. What if I have:

    Code:
    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:

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

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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, 05:53 PM
  2. Filling up a 2D (or 3D) array.
    By omnificient in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 12: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, 01:02 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 02:11 PM

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