Copying one array to another using pointers - c++

I have to use pointers to copy values of one array to another. The problem is I'm not allowed to use'[ ]' operators, which makes this more difficult for me. Here is my attempt:
#include <iostream>
using namespace std;
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
int *x = p2;
for(int i=0 ; i<length ; i++){
p2 = x;
p2 = p2 + i;
p2 = p1 + i;
}
for(int i=0; i<5; ++i){
cout << p2[i] << endl;
}
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
}
An easier way to do it would be to put p2[i] = p1[i] in the loop, but I cant do that. Any help is appreciated.

The standard way of implementing your function is as follow:
for(int i = 0; i < length; ++i)
*new_array++ = *old_array++;
To be a bit more explicit, it's the same as:
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
for(int i=0 ; i<length ; i++){
*(p2+i) = *(p1+i);
// another way: *(p2++) = *(p1++);
}
}

In real code, you would use std::copy before even thinking about rewriting such a simple thing yourself.
Here is a complete example:
#include <iostream>
#include <algorithm>
void cpyia(int old_array[],int new_array[],int length){
std::copy(old_array, old_array + length, new_array);
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
// test results:
for (int index = 0; index < 5; ++index)
{
std::cout << a[index] << " <-> " << b[index] << "\n";
}
}
However, your question says that you are "not allowed to use" something, which sounds a lot like a homework assignment. In that case, you could look at possible implementations of std::copy to get an idea of how to do it. Here is one way:
void cpyia(int old_array[],int new_array[],int length){
int* first = old_array;
int* last = old_array + length;
int* d_first = new_array;
while (first != last) {
*d_first++ = *first++;
}
}

#include<iostream>
using namespace std;
int main() {
const int size = 5;
int arr1[size] = { 4,21,43,9,77 };
int arr2[size];
int *ptr_a = arr1;
int *ptr_b = arr2;
for (int i = 0; i < size; i++)
{
*(ptr_b + i) = *(ptr_a + i);
cout << *(ptr_b + i) << " ";
}
}

Related

Function which copies an array of integers from one array to another using pointers in C++

The purpose of the Copy function in the following code is to copy an array of integers from one array to another using C++ but the output seems wrong.
What could be the problem here?
#include <iostream>
using namespace std;
void Copy(int old_array[],int new_array[],int length)
{
int *ptr1 = old_array;
int *ptr2 = new_array;
int i = 0;
for(int i=0 ; i<length ; i++)
{
*(ptr2++) = *(ptr1++);
}
for (int i = 0; i <2; i++)
{
cout<<*(ptr2 + i)<<endl;
}
}
int main()
{
int a[2]={0,1};
int b[2];
Copy(a,b,2);
}
This is the output:
ptr2 is one past the end of the array when your print loop runs.
Try this:
void Copy(int old_array[], int new_array[], int length)
{
int* ptr1 = old_array;
int* ptr2 = new_array;
int i = 0;
for (int i = 0; i < length; i++)
{
*(ptr2++) = *(ptr1++);
}
ptr2 = new_array;
for (int i = 0; i < 2; i++)
{
cout << *(ptr2 + i) << endl;
}
}
Your ptr2 is pointing to the element b[2] (which is out-of-bound access) at the time you are printing it in the second for loop.
You can fix it by subtracting the length from the ptr2 in the second for loop like below.
#include <iostream>
using namespace std;
void Copy(int old_array[],int new_array[],int length)
{
int *ptr1 = old_array;
int *ptr2 = new_array;
int i = 0;
for(int i=0 ; i<length ; i++)
{
*(ptr2++) = *(ptr1++);
}
for (int i = 0; i <2; i++)
{
cout<<*(ptr2 + i - length)<<endl;
}
}
int main()
{
int a[2]={0,1};
int b[2];
Copy(a,b,2);
}
The copy seems fine but the second for is accessing ptr2 which was incremented in the first for and is point to some invalid memory position. You could use new_array in this second loop.
I suppose this second loop is only for debug and will be better located in the main using, in you case, the variable b.

I cant seem to assign a pointer an array and then change the contents of the array

I couldn't figure out how to make a function return an array so instead I decided to try and pass an empty array (of the correct size) into my function and than reassign the address to a different array of the same size. Is this at all a way to do things??? Can someone show me what to do? if this is wrong can you fill me in on how to do this?
here is my code:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray,int s, int f){
int *ptrarray = &earray;
int prenum_size = std::abs(s) + f - 1;
int pre_num[prenum_size];
for(int x=s;x<f;x++){
pre_num[x+std::abs(s)] = x;
}
*ptrarray = pre_num;
}
int Main(){
int first = -10;
int second = 15;
int temp[abs(first) + abs(second)];
ArrayFiller(temp, first, second);
int n = sizeof(temp)/sizeof(temp[0]);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
return 0;
}
I think you're looking for something like this:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray[],int s, int f){
for(int x=s;x<f;x++){
earray[x+(std::abs(s))] = x;
}
}
int main(){
int first = -10;
int second = 15;
int n = abs(first)+abs(second);
int* temp = new int[n];
ArrayFiller(temp, first, second);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
delete [] temp;
return 0;
}

What are the rules for function returning pointer in c++ did I miss something in my code?

I want to create a function which generates an array(filled with random numbers) of the size I give as an input and the function returns the address of the first element of the generated array. I wrote the code as best as possible without any errors or warning. But at the runtime, the program crashes. I try to debug it but the debugger also froze and do nothing. I think the problem is in returning the pointer. Please help.
#include<iostream>
#include<cstdlib>
using namespace std;
int** the_gen(int num)
{
srand(1000);
int *ptr= new int(num);
int** const dptr=&ptr;
for(int i=0;i<num;i++)
{
*ptr= rand();
ptr++;
}
return dptr;
}
int main()
{
cout<<"Size of array:"<<endl;
int size_of_array;
cin>>size_of_array;
int **a;
a=the_gen(size_of_array);
for(int i=0;i<size_of_array;i++)
{
cout<<**a<<",";
a++;
}
}
you were using int** unnecessarily. only need to use that if you're creating an array of int pointers or a 2d array of int's:
the following code does what you're after i think:
#include<iostream>
#include<cstdlib>
using namespace std;
int* the_gen(int num)
{
srand(1000);
//edit
int *ptr = new int[num];
int* const dptr = ptr;
for (int i = 0; i < num; i++)
{
*ptr = rand();
ptr++;
}
return dptr;
}
int main()
{
cout << "Size of array:" << endl;
int size_of_array;
cin >> size_of_array;
int *a;
a = the_gen(size_of_array);
for (int i = 0; i < size_of_array; i++)
{
cout << *a << ",";
a++;
}
}
I think returning pointer is always bad idea, we should take memory pointers as a parameter as the follow
void the_gen(int num, int** arry)
{
srand(1000);
int *ptr = new int[num];
*arry = ptr;
for (int i = 0; i < num; i++)
{
ptr[i] = rand();
}
}
int main()
{
cout << "Size of array:" << endl;
int size_of_array;
cin >> size_of_array;
int *a;
the_gen(size_of_array, &a);
for (int i = 0; i < size_of_array; i++)
{
cout << a[i] << ",";
}
}

C++ allocating memory

Can someone explain me why when i back form function i lost my data from tabOfOffsets. I did the same thing twice and program crash only with the second array.
I printed values of this array on the end of function and everything is clear and correct. Maybe i make mistake somewhere with delete?
Below it is the code.
#include<iostream>
#include <algorithm>
using std::cout;
using std::endl;
void changeSizeOfVector(int *tabValue, int *tabOffsets, int &oldSize, int
newSize) {
int temp = std::min(oldSize, newSize);
int *newTabOfValues = new int [newSize] {0};
int *newTabOfOffsets = new int [newSize] {0};
for (int i = 0; i < temp; i++) {
newTabOfValues[i] = tabValue[i];
newTabOfOffsets[i] = tabOffsets[i];
}
delete[] tabValue;
delete[] tabOffsets;
tabValue = new int [newSize] {0};
tabOffsets = new int [newSize] {0};
for (int i = 0; i < newSize; i++) {
tabValue[i] = newTabOfValues[i];
tabOffsets[i] = newTabOfOffsets[i];
std::cout << tabOffsets[i] << tabValue[i] << endl;
}
oldSize = newSize;
delete[] newTabOfValues;
delete[] newTabOfOffsets;
for (int i = 0; i < newSize; i++) {
std::cout << tabOffsets[i] << tabValue[i] << endl;
}
}
int main() {
int SIZE = 10;
int * tabOfOffsets = new int[SIZE];
int * tabOfValues = new int[SIZE];
for (int i = 0; i < SIZE; i++)
{
tabOfValues[i] = i;
tabOfOffsets[i] = i;
cout << tabOfValues[i] << " : " << tabOfOffsets[i] << endl;
}
changeSizeOfVector(tabOfValues, tabOfOffsets, SIZE, 12);
for (int i = 0; i < SIZE; i++) {
cout << tabOfOffsets[i] << " : " << tabOfValues[i] << endl;
}
delete[] tabOfOffsets;
delete[] tabOfValues;
}
This function declaration is wrong:
void changeSizeOfVector(int *tabValue, int *tabOffsets, int &oldSize, int
newSize);
it means you can change the values of tabOffsets but not the pointer itself in order to make it behave correctly you should declare it as follows:
void changeSizeOfVector(int *tabValue, int **tabOffsets, int &oldSize, int
newSize);
This way you can change the pointer itself and assign a newly allocated array to it.

comparison between pointer and int C++ ellipse

#include <iostream>
using namespace std;
char problem5(char alc[], char a, int *n);
int main() {
char aloc1[]={ 'g','g','c','g','a','g','g','g','t','g'};
int size=sizeof(aloc1)/sizeof(aloc1[0]);
cout << aloc1 << endl;
int nalc = problem5(aloc1, 'g' ,&size);
cout << nalc << endl;
return 0;
}
char problem5(char alc[], char c, int *n){
int a = 0;
for(int i = 0; i < n; i++){
if(alc[0]!=c){
a++;
}
}
int nalc[a];
int b=0;
for(int j = 0; j < n; j++){
if(alc[0]!=c){
nalc[b]=alc[j];
b++;
}
}
*n=&a;
return nalc;
}
why do I keep having errors at the two for loops of the problem5?
it says something like comparison between pointer and int.
how can i fix that while the argument n remains pointer.
Use *n to access the value pointed by n:
for(int i = 0; i < *n; i++){
And to change the value pointed by n to be equal to a:
*n = a;
You can't have an address in a for-loop. Try dereferencing the pointer. and also post your exact error please.