Reputation:
I'm trying to understand pointers in C++ by writing some examples. I tried creating a pointer array and when I was trying to add integers to it does not work properly. I want to add integers from 0 to 9 to pointer array and print it.
int *array;
array = new int[10];
for(int i=0; i<10; i++){
*array[i] = i;
cout<<*array<<endl;
}
Upvotes: 1
Views: 33716
Reputation: 30926
Allocate something in array
otherwise how do you expect it to hold something.(unless you point it to some already allocated memory).
Or assign array=pInt
and then you can use it to hold values. array[i]=i
An example:
#include <iostream>
using namespace std;
int main()
{
int *a;
// int *b;
// b= new int[10]; we can simply allocate it to 'a' directly
// a = b;
a = new int[10];
for(int i=0;i<5;i++)
a[i]=i;
for(int i=0;i<5;i++)
cout<<a[i];
delete[] a;
return 0;
}
The pointer variable is nothing but address holder. So here
a
is not holding anything significant before initializing. But once you initialize it either by allocatingnew
or assign it to something similar it just doesn't point to nowhere. And once you do that you have some chunk of memory which you can access by that pointer which is what i did here. What if I didn't initialize and tried to usea
. it's Undefined behavior.
Don't use bits/stdc++.h
Link. It might help you write small code but it should never be used in development or production level code.
Upvotes: 1
Reputation: 11
You should declare a variable as
int *array=new int;
try this
int *array=new int;
int *pInt = new int[10];
for(int i=0; i<10; i++){
*array = i;
cout<<*array<<endl;
}
and if you want to display array from 0 to 9
try this
int *array=new int[10];
int *pInt = new int[10];
for(int i=0; i<10; i++){
array[i] = i;
cout<<array[i]<<endl;
}
for Adding of array try this
int *array=new int;
int *pInt = new int[10];
for(int i=0; i<10; i++){
pInt[i]=i;
cout<<pInt[i]<<endl;
*array=*array+pInt[i];
}
cout<<"Addition ="<<*array;
return 0;
Upvotes: 0
Reputation: 46
You allocated memory for 10 integers for pInt but do not allocate any memory for array. Since array doesn't have any memory allocated it can not hold data. One solution is to just use your pInt variable. On that note where you have *array[i] = i;
there is no need to dereference with * as having the brackets [] dereference the pointer. So you can replace that line with pInt[i] = i;
then if you call cout << *pInt;
you would get the first value in the array. You can cout each in a for loop like cout << pInt[i];
One final thing that you may already know but just incase, whenever you use pointers and allocate memory by using new
make sure you deallocate that memory when you are done to prevent memory leaks. For this you would do delete[] pInt;
Upvotes: 2
Reputation: 382
The following will do what you've described:
#include <iostream>
int main()
{
int* array = new int[10];
for (int i = 0; i < 10; ++i)
{
array[i] = i;
std::cout << array[i] << std::endl;
}
delete [] array;
return 0;
}
However in modern C++ the idomatic solution would be something like this:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i);
std::cout << v[i] << std::endl;
}
return 0;
}
Upvotes: 3