Assigning values to all the elements of a dynamically allocated pointer array?
up vote
4
down vote
favorite
I have a pointer array (ptrArr1
) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.
int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work
deleteptrArr1;
return 0;
}
c++ arrays pointers dynamic-memory-allocation
add a comment |
up vote
4
down vote
favorite
I have a pointer array (ptrArr1
) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.
int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work
deleteptrArr1;
return 0;
}
c++ arrays pointers dynamic-memory-allocation
1
When you doptrArr1 = &one
it's similar to doingone = two
and expecting the value ofone
to still be1
.
– Some programmer dude
Nov 20 at 7:59
1
Furthermore,ptrArr[x]
is anint
and not anint*
(which e.g.&one
is).
– Some programmer dude
Nov 20 at 7:59
1
ptrArr[1] = &two;
tries to assign an address (int*
) to anint
-> type mismatch.ptrArr1 + 1 = &two;
is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as1 = 0;
.
– Scheff
Nov 20 at 7:59
Also you haveint *ptrArr1 = new int[2];
and right afterptrArr1 = &one;
which overwritesptrArr1
. It's kind of like writingfoo = 2; foo = 42;
.
– Jabberwocky
Nov 20 at 8:27
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have a pointer array (ptrArr1
) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.
int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work
deleteptrArr1;
return 0;
}
c++ arrays pointers dynamic-memory-allocation
I have a pointer array (ptrArr1
) with two elements. I want the pointer array to be dynamically allocated. I can assign an address to the first element of the pointer array just fine, but I do not know how to assign an address to the second element of the pointer array. I do not wish to use any STLs or other precoded functions. I'm am doing this exercise to enhance my understanding of pointers. Thank you.
int main()
{
int one = 1;
int two = 2;
int *ptrArr1 = new int[2];
ptrArr1 = &one;
ptrArr1[1] = &two; //does not work
ptrArr1 + 1 = &two; // does not work
deleteptrArr1;
return 0;
}
c++ arrays pointers dynamic-memory-allocation
c++ arrays pointers dynamic-memory-allocation
edited Nov 20 at 8:14
Micha Wiedenmann
10.1k1364102
10.1k1364102
asked Nov 20 at 7:55
Carlos Robin Alvarenga
376
376
1
When you doptrArr1 = &one
it's similar to doingone = two
and expecting the value ofone
to still be1
.
– Some programmer dude
Nov 20 at 7:59
1
Furthermore,ptrArr[x]
is anint
and not anint*
(which e.g.&one
is).
– Some programmer dude
Nov 20 at 7:59
1
ptrArr[1] = &two;
tries to assign an address (int*
) to anint
-> type mismatch.ptrArr1 + 1 = &two;
is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as1 = 0;
.
– Scheff
Nov 20 at 7:59
Also you haveint *ptrArr1 = new int[2];
and right afterptrArr1 = &one;
which overwritesptrArr1
. It's kind of like writingfoo = 2; foo = 42;
.
– Jabberwocky
Nov 20 at 8:27
add a comment |
1
When you doptrArr1 = &one
it's similar to doingone = two
and expecting the value ofone
to still be1
.
– Some programmer dude
Nov 20 at 7:59
1
Furthermore,ptrArr[x]
is anint
and not anint*
(which e.g.&one
is).
– Some programmer dude
Nov 20 at 7:59
1
ptrArr[1] = &two;
tries to assign an address (int*
) to anint
-> type mismatch.ptrArr1 + 1 = &two;
is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as1 = 0;
.
– Scheff
Nov 20 at 7:59
Also you haveint *ptrArr1 = new int[2];
and right afterptrArr1 = &one;
which overwritesptrArr1
. It's kind of like writingfoo = 2; foo = 42;
.
– Jabberwocky
Nov 20 at 8:27
1
1
When you do
ptrArr1 = &one
it's similar to doing one = two
and expecting the value of one
to still be 1
.– Some programmer dude
Nov 20 at 7:59
When you do
ptrArr1 = &one
it's similar to doing one = two
and expecting the value of one
to still be 1
.– Some programmer dude
Nov 20 at 7:59
1
1
Furthermore,
ptrArr[x]
is an int
and not an int*
(which e.g. &one
is).– Some programmer dude
Nov 20 at 7:59
Furthermore,
ptrArr[x]
is an int
and not an int*
(which e.g. &one
is).– Some programmer dude
Nov 20 at 7:59
1
1
ptrArr[1] = &two;
tries to assign an address (int*
) to an int
-> type mismatch. ptrArr1 + 1 = &two;
is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;
.– Scheff
Nov 20 at 7:59
ptrArr[1] = &two;
tries to assign an address (int*
) to an int
-> type mismatch. ptrArr1 + 1 = &two;
is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as 1 = 0;
.– Scheff
Nov 20 at 7:59
Also you have
int *ptrArr1 = new int[2];
and right after ptrArr1 = &one;
which overwrites ptrArr1
. It's kind of like writing foo = 2; foo = 42;
.– Jabberwocky
Nov 20 at 8:27
Also you have
int *ptrArr1 = new int[2];
and right after ptrArr1 = &one;
which overwrites ptrArr1
. It's kind of like writing foo = 2; foo = 42;
.– Jabberwocky
Nov 20 at 8:27
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You have an int array, not a pointer array. You can create a pointer array using
int **ptrArr1 = new int*[2];
and then assign the pointers to each pointer in the array:
ptrArr1[0] = &one;
ptrArr1[1] = &two;
2
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
add a comment |
up vote
5
down vote
There is a difference between an array of integers and an array of pointers to int
. In your case ptrArr1
is a pointer to an array of integers with space for two integers. Therefore you can only assign an int
to ptrArr1[1] = 2
but not an address. Compare
int xs = { 1, 2, 3 }; // an array of integers
int y0 = 42;
int *ys = { &y0, &y0 }; // an array of pointers to integers
Now you could also have pointers pointing to the first element of xs
resp. ys
:
int *ptr_xs = &xs[0];
int **ptr_ys = &ys[0];
// which can be simplified to:
int *ptr_xs = xs;
int **ptr_ys = ys;
For the simplification step you should look into What is array decaying?
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You have an int array, not a pointer array. You can create a pointer array using
int **ptrArr1 = new int*[2];
and then assign the pointers to each pointer in the array:
ptrArr1[0] = &one;
ptrArr1[1] = &two;
2
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
add a comment |
up vote
3
down vote
accepted
You have an int array, not a pointer array. You can create a pointer array using
int **ptrArr1 = new int*[2];
and then assign the pointers to each pointer in the array:
ptrArr1[0] = &one;
ptrArr1[1] = &two;
2
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You have an int array, not a pointer array. You can create a pointer array using
int **ptrArr1 = new int*[2];
and then assign the pointers to each pointer in the array:
ptrArr1[0] = &one;
ptrArr1[1] = &two;
You have an int array, not a pointer array. You can create a pointer array using
int **ptrArr1 = new int*[2];
and then assign the pointers to each pointer in the array:
ptrArr1[0] = &one;
ptrArr1[1] = &two;
answered Nov 20 at 7:59
eozd
8941515
8941515
2
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
add a comment |
2
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
2
2
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
Thanks eozd! The use of '**' is completely new to me, but it lets me do what I wanted to accomplish.
– Carlos Robin Alvarenga
Nov 20 at 8:09
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
@CarlosRobinAlvarenga If eozd answered your question (IMHO, she/he did), please, don't forget to accept it.
– Scheff
Nov 20 at 8:50
add a comment |
up vote
5
down vote
There is a difference between an array of integers and an array of pointers to int
. In your case ptrArr1
is a pointer to an array of integers with space for two integers. Therefore you can only assign an int
to ptrArr1[1] = 2
but not an address. Compare
int xs = { 1, 2, 3 }; // an array of integers
int y0 = 42;
int *ys = { &y0, &y0 }; // an array of pointers to integers
Now you could also have pointers pointing to the first element of xs
resp. ys
:
int *ptr_xs = &xs[0];
int **ptr_ys = &ys[0];
// which can be simplified to:
int *ptr_xs = xs;
int **ptr_ys = ys;
For the simplification step you should look into What is array decaying?
add a comment |
up vote
5
down vote
There is a difference between an array of integers and an array of pointers to int
. In your case ptrArr1
is a pointer to an array of integers with space for two integers. Therefore you can only assign an int
to ptrArr1[1] = 2
but not an address. Compare
int xs = { 1, 2, 3 }; // an array of integers
int y0 = 42;
int *ys = { &y0, &y0 }; // an array of pointers to integers
Now you could also have pointers pointing to the first element of xs
resp. ys
:
int *ptr_xs = &xs[0];
int **ptr_ys = &ys[0];
// which can be simplified to:
int *ptr_xs = xs;
int **ptr_ys = ys;
For the simplification step you should look into What is array decaying?
add a comment |
up vote
5
down vote
up vote
5
down vote
There is a difference between an array of integers and an array of pointers to int
. In your case ptrArr1
is a pointer to an array of integers with space for two integers. Therefore you can only assign an int
to ptrArr1[1] = 2
but not an address. Compare
int xs = { 1, 2, 3 }; // an array of integers
int y0 = 42;
int *ys = { &y0, &y0 }; // an array of pointers to integers
Now you could also have pointers pointing to the first element of xs
resp. ys
:
int *ptr_xs = &xs[0];
int **ptr_ys = &ys[0];
// which can be simplified to:
int *ptr_xs = xs;
int **ptr_ys = ys;
For the simplification step you should look into What is array decaying?
There is a difference between an array of integers and an array of pointers to int
. In your case ptrArr1
is a pointer to an array of integers with space for two integers. Therefore you can only assign an int
to ptrArr1[1] = 2
but not an address. Compare
int xs = { 1, 2, 3 }; // an array of integers
int y0 = 42;
int *ys = { &y0, &y0 }; // an array of pointers to integers
Now you could also have pointers pointing to the first element of xs
resp. ys
:
int *ptr_xs = &xs[0];
int **ptr_ys = &ys[0];
// which can be simplified to:
int *ptr_xs = xs;
int **ptr_ys = ys;
For the simplification step you should look into What is array decaying?
edited Nov 20 at 8:21
answered Nov 20 at 7:59
Micha Wiedenmann
10.1k1364102
10.1k1364102
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388495%2fassigning-values-to-all-the-elements-of-a-dynamically-allocated-pointer-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
When you do
ptrArr1 = &one
it's similar to doingone = two
and expecting the value ofone
to still be1
.– Some programmer dude
Nov 20 at 7:59
1
Furthermore,
ptrArr[x]
is anint
and not anint*
(which e.g.&one
is).– Some programmer dude
Nov 20 at 7:59
1
ptrArr[1] = &two;
tries to assign an address (int*
) to anint
-> type mismatch.ptrArr1 + 1 = &two;
is not a type mismatch but tries to assign an address to a non-lvalue. Strongly simplified, this is as wrong as1 = 0;
.– Scheff
Nov 20 at 7:59
Also you have
int *ptrArr1 = new int[2];
and right afterptrArr1 = &one;
which overwritesptrArr1
. It's kind of like writingfoo = 2; foo = 42;
.– Jabberwocky
Nov 20 at 8:27