Jump to content

So I have to do this as a practice assignment for school

 

1. In main(), create two arrays of the same size. Fill one with integer data values, leave the other one without values. 

2. Write a function copyArray() that has 3 parameters. The first 2 parameters should be pointers to integer. The third parameter should be an int, for the number of elements in the array. The function should use pointers to copy from one array to the other, incrementing the pointers as you go. See the printArray() example that uses pointers like this  in the slides. 

3. In main(), call copyArray(), passing it the addresses of the two arrays. In main()print the contents of the second array after calling copyArray().

 and so far I've come up with what I can only describe as a really poor excuse for code down below

#include <iostream>using namespace std;void copyArray(int *, int *b, int n);int main (){ 	const int ARRAY1 = 3; 	int var1 [ARRAY1]; 	const int ARRAY2 = 3; 	int var2 [ARRAY2]; 	int n = ARRAY1; 	int i = 0; 	 	for(i = 0; i < ARRAY1; i++) 	{cout << "Please input your number\n"; 	cin >> var1 [i]; 	} 	cout << copyArray(*var1, *var2, n); 	void copyArray (int *a, int *b, int n){	int *theend = a + n;		while (a < theend)			  *b++ = *a++}

I get the point (no pun ) of pointers but I'm really not understanding how to implement them or how to use them properly and honestly it's making me lose my hair. Could someone here try to help me. I'm looking for someone to basically teach me how to use pointers properly because right now I can't.

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/
Share on other sites

Link to post
Share on other sites

Couple of things:

1) you cannot use the call to a void function when printing something to cout, you'll have to call the function and then loop again to print the results (like you have done to ask for input)
2) you got most things right. the only problem is that var1 and var2 are already pointers to the first positions of each array, so you can pass them directly to your copyArray function.

 

Also, if you have an "object" and want the reference (pointer) to it you use & instead of * to get that reference, * is used to dereference (going from the pointer to the actual value).

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126215
Share on other sites

Link to post
Share on other sites

A pointer is a memory address.

You use "&" to get the address where a variable is stored and "*" to get the value stored on a specified address.

About the code (adding to what the above user said):

I would use a for loop instead of a while which would remove the need for an extra variable with a sum of mixed types (a pointer and an int).

Also, vectors are pointers and pointers are vectors, which means pointers can be indexed.

When you declare a vector you are declaring a special type of pointer, which cannot be modified itself (you can modify the values it contains).

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126230
Share on other sites

Link to post
Share on other sites

#include <iostream>using namespace std;int copyArray(int *a, int *b, int n);int main (){ 	const int ARRAY1 = 3; 	int *a [ARRAY1]; 	const int ARRAY2 = 3; 	int *b [ARRAY2]; 	int n = ARRAY1; 	int i = 0; 	 	for(i = 0; i < ARRAY1; i++) 	{cout << "Please input your number\n"; 	cin >> *a [i]; 	} 	copyArray(*a, *b, n); 	 	for (i = 0; i < ARRAY1; i++) 		cout << a [i] << "  " << b [i]; 	 	return 0; }int copyArray (int *a, int *b, int n){	int *theend = a + n;		while (a < theend)			  *b++ = *a++;}

So I came up with that and got it to compile, which is more then I was getting before, but now the program crashes after I enter the first number. And I don't know how I would switch the for loop to a while loop in this situation. I'm confused I guess :(

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126266
Share on other sites

Link to post
Share on other sites

I added some comments and fixed some things. If there's something you don't understand feel free to ask.

 #include <iostream>using namespace std;void copyArray(int *a, int *b, int n);int main (){    const int ARRAY1 = 3;    int a[ARRAY1]; //If you add * here you'll be declaring a vector of pointers to int.    const int ARRAY2 = 3;    int b[ARRAY2];    int i = 0;        for(i = 0; i < ARRAY1; i++)    {        cout << "Please input your number\n";        cin >> a [i]; //Here you don't need to put the * because when you do a[i] you're doing *(a+i)    }        copyArray(a, b, ARRAY1); //a and b are already vectors, if you do *a and *b you're actually passing the first value of each vector. Also you can use ARRAY1 instead of n (there's no need to declare an extra variable)        for(i = 0; i < ARRAY1; i++) //this prints each element of the b vector        cout << b[i];    cout << '\n';        return 0;} void copyArray(int *a, int *b, int n){    int i;    for(i = 0; i < n; i++)        *b++ = *a++;}/* another way of implementing this function would be the following *//*void copyArray(int *a, int *b, int n){    while(n-- > 0)        *b++ = *a++;}*/ // Also, you dont need ARRAY1 and ARRAY2. You'll be copying the contents of the first to the second, so they'll have the same size. You could use more meaningfull names for constants, for example, ARRAY_SIZE

This compiles, runs and produces the expected output.

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126290
Share on other sites

Link to post
Share on other sites

Actually, you have complicated thing even more!

 

Notice that now a and b are pointers to arrays. This means that these variables point to a place that points to the first element of the array.

Notice also that [] has precedence over *. This means that the first time the loop runs it will get the first position of "a" (but a only has one position because it is a pointer to an array) and assign the input to that memory place (which happens to be the right place, the first position of the array). However, on the second iteration of the loop you try to access a[1], which does not exist. The compiler did not complain because the types on the left and right of the assignment are correct (both int) but at runtime you are trying to access a position that is not yours.

 

The way you had before was better because val1 pointed directly to the first position of the array. Regarding the first code the only change that needed to be made was to remove the * in the function call (besides the printing loop).

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126294
Share on other sites

Link to post
Share on other sites

I added some comments and fixed some things. If there's something you don't understand feel free to ask.

 #include <iostream>using namespace std;void copyArray(int *a, int *b, int n);int main (){    const int ARRAY1 = 3;    int a[ARRAY1]; //If you add * here you'll be declaring a vector of pointers to int.    const int ARRAY2 = 3;    int b[ARRAY2];    int i = 0;        for(i = 0; i < ARRAY1; i++)    {        cout << "Please input your number\n";        cin >> a [i]; //Here you don't need to put the * because when you do a[i] you're doing *(a+i)    }        copyArray(a, b, ARRAY1); //a and b are already vectors, if you do *a and *b you're actually passing the first value of each vector. Also you can use ARRAY1 instead of n (there's no need to declare an extra variable)        for(i = 0; i < ARRAY1; i++) //this prints each element of the b vector        cout << b[i];    cout << '\n';        return 0;} void copyArray(int *a, int *b, int n){    int i;    for(i = 0; i < n; i++)        *b++ = *a++;}/* another way of implementing this function would be the following *//*void copyArray(int *a, int *b, int n){    while(n-- > 0)        *b++ = *a++;}*/ // Also, you dont need ARRAY1 and ARRAY2. You'll be copying the contents of the first to the second, so they'll have the same size. You could use more meaningfull names for constants, for example, ARRAY_SIZE

This compiles, runs and produces the expected output.

I just went over this  few times and I THINK I'm understanding it. My biggest problem was just not know how to use the "*" symbol properly. I'm gonna look over it a few more times and reference it in the future. Thank you so much for the help

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126365
Share on other sites

Link to post
Share on other sites

I just went over this  few times and I THINK I'm understanding it. My biggest problem was just not know how to use the "*" symbol properly. I'm gonna look over it a few more times and reference it in the future. Thank you so much for the help

You're welcome. Pointers are not a simple thing to learn, but once you fully understand it, you'll love it :D

Link to comment
https://linustechtips.com/topic/11835-damn-pointers/#findComment-126434
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×