Why is in the following similar codes one giving runtime error and other is not
up vote
0
down vote
favorite
Well this is really weird
Consider the following two codes :-
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
AND
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;
}
The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .
c++ runtime
|
show 10 more comments
up vote
0
down vote
favorite
Well this is really weird
Consider the following two codes :-
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
AND
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;
}
The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .
c++ runtime
1
int* ptr[5];
doesn't seem right to me... Shouldn't it beint ptr[5];
? Well if you really like to stick with pointers then you SHOULD do*(ptr[i])
etc. etc...
– Ruks
22 hours ago
@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago
what's the error message?
– rsjaffe
22 hours ago
1
Simple answer, You have not allocated the pointer... Soptr[i] = &a[i]
is the correct answer...
– Ruks
21 hours ago
1
Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago
|
show 10 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Well this is really weird
Consider the following two codes :-
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
AND
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;
}
The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .
c++ runtime
Well this is really weird
Consider the following two codes :-
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
AND
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;
}
The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .
c++ runtime
c++ runtime
asked 22 hours ago
Ayush Mangal
94
94
1
int* ptr[5];
doesn't seem right to me... Shouldn't it beint ptr[5];
? Well if you really like to stick with pointers then you SHOULD do*(ptr[i])
etc. etc...
– Ruks
22 hours ago
@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago
what's the error message?
– rsjaffe
22 hours ago
1
Simple answer, You have not allocated the pointer... Soptr[i] = &a[i]
is the correct answer...
– Ruks
21 hours ago
1
Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago
|
show 10 more comments
1
int* ptr[5];
doesn't seem right to me... Shouldn't it beint ptr[5];
? Well if you really like to stick with pointers then you SHOULD do*(ptr[i])
etc. etc...
– Ruks
22 hours ago
@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago
what's the error message?
– rsjaffe
22 hours ago
1
Simple answer, You have not allocated the pointer... Soptr[i] = &a[i]
is the correct answer...
– Ruks
21 hours ago
1
Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago
1
1
int* ptr[5];
doesn't seem right to me... Shouldn't it be int ptr[5];
? Well if you really like to stick with pointers then you SHOULD do *(ptr[i])
etc. etc...– Ruks
22 hours ago
int* ptr[5];
doesn't seem right to me... Shouldn't it be int ptr[5];
? Well if you really like to stick with pointers then you SHOULD do *(ptr[i])
etc. etc...– Ruks
22 hours ago
@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago
@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago
what's the error message?
– rsjaffe
22 hours ago
what's the error message?
– rsjaffe
22 hours ago
1
1
Simple answer, You have not allocated the pointer... So
ptr[i] = &a[i]
is the correct answer...– Ruks
21 hours ago
Simple answer, You have not allocated the pointer... So
ptr[i] = &a[i]
is the correct answer...– Ruks
21 hours ago
1
1
Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago
Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago
|
show 10 more comments
3 Answers
3
active
oldest
votes
up vote
0
down vote
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
}
the above code runs fine and gives output 1, 2
but
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
}
this code gives runtime error on codeblocks(gcc) , I am just getting more confused
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
|
show 4 more comments
up vote
0
down vote
I have both runtime errors.
The result of recoding with the pointer variable below.
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
New contributor
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
add a comment |
up vote
0
down vote
It is already established that
*ptr[0] = a[0];
with unallocated pointer is undefined behavior.
The solution
ptr[0] = &a[0];
is also already given. This works, but it is limited because now, the ptr
array is tied to a
, which you not always want. In that case you might want
ptr[0] = new int(a[0]);
This initializes the array with values from a
but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).
#include <memory>
#include <vector>
#include <iostream>
int main() {
int a={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;
for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}
for(auto& i: ptrs)
{
std::cout << *i;
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
}
the above code runs fine and gives output 1, 2
but
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
}
this code gives runtime error on codeblocks(gcc) , I am just getting more confused
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
|
show 4 more comments
up vote
0
down vote
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
}
the above code runs fine and gives output 1, 2
but
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
}
this code gives runtime error on codeblocks(gcc) , I am just getting more confused
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
|
show 4 more comments
up vote
0
down vote
up vote
0
down vote
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
}
the above code runs fine and gives output 1, 2
but
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
}
this code gives runtime error on codeblocks(gcc) , I am just getting more confused
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
}
the above code runs fine and gives output 1, 2
but
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
}
this code gives runtime error on codeblocks(gcc) , I am just getting more confused
answered 21 hours ago
Ayush Mangal
94
94
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
|
show 4 more comments
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
– Ruks
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
– Ayush Mangal
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
– Ruks
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
its showing gcc 5.1.0
– Ayush Mangal
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
– Ruks
21 hours ago
|
show 4 more comments
up vote
0
down vote
I have both runtime errors.
The result of recoding with the pointer variable below.
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
New contributor
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
add a comment |
up vote
0
down vote
I have both runtime errors.
The result of recoding with the pointer variable below.
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
New contributor
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I have both runtime errors.
The result of recoding with the pointer variable below.
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
New contributor
I have both runtime errors.
The result of recoding with the pointer variable below.
#include <iostream>
using namespace std;
int main() {
int a={1,2,3,4,5};
int* ptr[5];
for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}
for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}
New contributor
edited 21 hours ago
New contributor
answered 21 hours ago
JY Lee
112
112
New contributor
New contributor
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
add a comment |
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
From the question, it looks like the OP wants an array of pointers...
– Ruks
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
yes I do want an array of pointers
– Ayush Mangal
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
I modified to pointer array
– JY Lee
21 hours ago
add a comment |
up vote
0
down vote
It is already established that
*ptr[0] = a[0];
with unallocated pointer is undefined behavior.
The solution
ptr[0] = &a[0];
is also already given. This works, but it is limited because now, the ptr
array is tied to a
, which you not always want. In that case you might want
ptr[0] = new int(a[0]);
This initializes the array with values from a
but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).
#include <memory>
#include <vector>
#include <iostream>
int main() {
int a={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;
for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}
for(auto& i: ptrs)
{
std::cout << *i;
}
}
add a comment |
up vote
0
down vote
It is already established that
*ptr[0] = a[0];
with unallocated pointer is undefined behavior.
The solution
ptr[0] = &a[0];
is also already given. This works, but it is limited because now, the ptr
array is tied to a
, which you not always want. In that case you might want
ptr[0] = new int(a[0]);
This initializes the array with values from a
but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).
#include <memory>
#include <vector>
#include <iostream>
int main() {
int a={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;
for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}
for(auto& i: ptrs)
{
std::cout << *i;
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
It is already established that
*ptr[0] = a[0];
with unallocated pointer is undefined behavior.
The solution
ptr[0] = &a[0];
is also already given. This works, but it is limited because now, the ptr
array is tied to a
, which you not always want. In that case you might want
ptr[0] = new int(a[0]);
This initializes the array with values from a
but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).
#include <memory>
#include <vector>
#include <iostream>
int main() {
int a={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;
for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}
for(auto& i: ptrs)
{
std::cout << *i;
}
}
It is already established that
*ptr[0] = a[0];
with unallocated pointer is undefined behavior.
The solution
ptr[0] = &a[0];
is also already given. This works, but it is limited because now, the ptr
array is tied to a
, which you not always want. In that case you might want
ptr[0] = new int(a[0]);
This initializes the array with values from a
but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).
#include <memory>
#include <vector>
#include <iostream>
int main() {
int a={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;
for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}
for(auto& i: ptrs)
{
std::cout << *i;
}
}
answered 18 hours ago
stefaanv
10.8k12339
10.8k12339
add a comment |
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183951%2fwhy-is-in-the-following-similar-codes-one-giving-runtime-error-and-other-is-not%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
1
int* ptr[5];
doesn't seem right to me... Shouldn't it beint ptr[5];
? Well if you really like to stick with pointers then you SHOULD do*(ptr[i])
etc. etc...– Ruks
22 hours ago
@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago
what's the error message?
– rsjaffe
22 hours ago
1
Simple answer, You have not allocated the pointer... So
ptr[i] = &a[i]
is the correct answer...– Ruks
21 hours ago
1
Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago