Raaz444
Raaz444

Reputation: 85

Pointers for int and Arrays

Hello i'm a noob in programming, i have a small doubt regarding pointers

#include<iostream>
using namespace std;

int main()
{
    int myAge = 16;
    int* agePtr = &myAge;

    cout << "address of pointer" << agePtr << "Data at memory address" << *agePtr << endl;

    int badNums[5] = {4, 13, 14, 24, 34};
    int* numArrayPtr = badNums;

    cout<< "address" << numArrayPtr << "value" << *numArrayPtr << endl;

    numArrayPtr++;

    cout<< "address" << numArrayPtr << "value" << *numArrayPtr << endl;

    return 0;
}

Upvotes: 2

Views: 109

Answers (3)

user4113344
user4113344

Reputation:

I have a small doubt regarding pointers

Just like i in badnums[i] is an index within array badnums[], a pointer is an index to something stored in memory. Since any variable is stored in memory, you can access the contents of a variable with a pointer to whatever it contains (which is what languages implicitly do when using variables in your code).

The difference is that with pointers you must know the type of what the pointer designates, while an index uses the known type of the indexed elements of the variable it points to... because sooner than later you will iterate the contents of some known variable using a pointer.

Upvotes: 0

user5927908
user5927908

Reputation:

In the first case, using &myAge refers to the address of that integer value. The reason why you must use badNums instead of &badNums when doing assignment to the integer pointer is because badNums is already an integer pointer. Arrays implicitly decay into pointers, so using &badNums in that assignment would work if you were doing:

    int **numArrayPtr = &badNums;

which is just a pointer to a pointer to the address of badNums. So,

    int *numArrayPtr = badNums;

just means that we have a pointer to the address of badNums.

When we have an integer pointer like this, you can increment the value of each integer in the array by doing this:

    for (int i = 0; i < 5; i++){
          numArrayPtr[i]++;
    }

or, we can do the same thing without using array notation:

    for (int *i = numArrPtr; i != numArrPtr + 5; i++){
          (*numArrPtr)++; 
    }

I hope that answers your questions fully.

Upvotes: 2

Fran&#231;ois Andrieux
Fran&#231;ois Andrieux

Reputation: 29022

Arrays implicitly decay to pointers, as per the rules of c++. There are many implicit conversions in c++, this is one of them. When assigning an array to a pointer, it provides you with a pointer to the first element in the array.

Taking the address of an array (&badNums) will yield a pointer to the array, not to the first element. Array pointers are slightly more complicated and encode the size of the array in the type. The correct type for that assignment would be int (*numArrayPtr)[5] = &badNums; where numArrayPtr is a pointer to an array of 5 ints.

To increment a value pointer to by a pointer, you must first dereference that pointer using operator * just like if you wanted to read from or write to that value. It would look like (*numArrayPtr)++;.

Upvotes: 2

Related Questions