Find the answer to your Linux question:
Results 1 to 6 of 6
I've been learning about pointers in C++. There is an example program that is meant to help me understand them: /* Counts up from 0 to 100 using pointers. */ ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38

    C++ pointers?


    I've been learning about pointers in C++. There is an example program that is meant to help me understand them:

    /*

    Counts up from 0 to 100 using pointers.

    */

    #include <iostream>
    using namespace std;

    int main() {

    int *i , j[10] , x;
    double *f , g[10];

    i = j;
    f = g;

    for( x = 0; x < 10; x++)
    cout << i+x << ' ' << f+x << '\n';

    return 0;

    }

    I get an output, but my main problem is with the program itself.

    1). j[10] is a row vector with 10 values. Before the i=j step, what are the entries of j[10]? Up until this point, I have been inputting values into arrays so it's confusing when this doesn't happen.

    2). At the i=j step, what is j? As far as I can see, we haven't specified what j is, only some row vector j[10]. When I change j to j[10], I get an error message.

    3). Also on the i=j step, does this mean "set i to be a row vector of 10 elements"?

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    You are doing very well to have asked these questions! They are very good observations. Let's start with what an "array" actually is.

    When you declare an array like int j[10], what actually happens is that ten int-sizes worth of memory get allocated, and "j" is a pointer to the first one. Yes, I said pointer! In C and C++, arrays are just pointers. You can treat them (almost) exactly the same.

    So when you declare int j[10], you get some memory that you're allowed to access. However, it is filled with random values! When I executed your program, here is what I got:
    Code:
    alex@niamh:~/test/c++$ ./a.out 
    0x7fffdc247cc0 0x7fffdc247c70
    0x7fffdc247cc4 0x7fffdc247c78
    0x7fffdc247cc8 0x7fffdc247c80
    0x7fffdc247ccc 0x7fffdc247c88
    0x7fffdc247cd0 0x7fffdc247c90
    0x7fffdc247cd4 0x7fffdc247c98
    0x7fffdc247cd8 0x7fffdc247ca0
    0x7fffdc247cdc 0x7fffdc247ca8
    0x7fffdc247ce0 0x7fffdc247cb0
    0x7fffdc247ce4 0x7fffdc247cb8
    This obviously is not 0-100. That's because j was filled with whatever random data happened to be in that memory when you created it. So to answer question 1: it is completely random. You should always set memory to a known value before using it.

    Question 2 should be at least slightly answered by the above explanation: j is a pointer to the beginning of an array! Also, when you say "int j[10]", it means "an array j of 10 ints". When you say "i = j[10]", it means "set i equal to the 11th element of j". But j only has ten elements! Hence the error.

    Question 3 should be at least slightly answered by the above explanation: pointers and arrays are the same!

  3. #3
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,599
    In answer to question 3: i=j means set the pointer to int i to the address of the 0th element of the array j

    @Cabhan, my understanding is that conceptually pointers and arrays are not the same. An array is a collection of related things and a pointer is a scalar that holds a memory address as it's data. Under the hood however, arrays are implemented as a pointer to series of addresses which means that both notations can be used interchangeably to access array elements; for example j[2] and *(j+2) should point at the same data, if I haven't made any typos.
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    elija: I don't necessarily believe that this is always true. The most obvious example that comes to my mind is a dynamically-sized array. This is not legal C code:
    Code:
    int i = 10;
    int array[i];
    The only way to do a dynamically-sized array like this is to use a pointer:
    Code:
    int i = 10;
    int *array = malloc(i * sizeof(int));
    Furthermore, arrays are a common way of defining buffers or other constant-sized sections of memory (e.g. char buf[1024]), where you're really just looking to have an area of memory of a certain size.

  5. #5
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,599
    Quote Originally Posted by Cabhan View Post
    elija: I don't necessarily believe that this is always true. The most obvious example that comes to my mind is a dynamically-sized array. This is not legal C code:
    Code:
    int i = 10;
    int array[i];
    The only way to do a dynamically-sized array like this is to use a pointer:
    Code:
    int i = 10;
    int *array = malloc(i * sizeof(int));
    Furthermore, arrays are a common way of defining buffers or other constant-sized sections of memory (e.g. char buf[1024]), where you're really just looking to have an area of memory of a certain size.
    It's a weird thing but I while I know they are basically the same, conceptually I see those as different things; it seems to help with my understanding. And yeah the array handling in c is limited indeed to someone who is used to the insane power and flexibility of PHP arrays!

    A pointer to an an area of memory big enough to hold 10 integers
    Code:
    int i = 10;
    int *array = malloc(i * sizeof(int));
    A collection of 1024 characters
    Code:
    char buf[1024]
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38
    Thanks guys, I have a better understanding of what was going on.

    Elija, I may have to look at PHP at some point to see what you mean by "flexibility of PHP arrays". I think i'll do that after messing around with C++ some more!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •