Why array of struct lets me index to out of boundary array [duplicate]
This question already has an answer here:
How dangerous is it to access an array out of bounds?
10 answers
Sorry for the bad title and i am new to the C but here is the part i don't understand.
I have a simple struct;
struct st{
int a;
int b;
};
and i am creating array of struct and indexing values to variable 'a' in main;
int main(){
struct st st_arr[2];
st_arr[0].a = 5;
st_arr[1].a = 10;
st_arr[4].a = 20;
printf("%d %d %dn", st_arr[0].a, st_arr[1].a, st_arr[4].a);
}
i have assigned 2 array of structs but it lets me index 4th of the struct why is that? Isn't it suppose to give me an error?
the output is:
5 10 20
c arrays
marked as duplicate by Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 10:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How dangerous is it to access an array out of bounds?
10 answers
Sorry for the bad title and i am new to the C but here is the part i don't understand.
I have a simple struct;
struct st{
int a;
int b;
};
and i am creating array of struct and indexing values to variable 'a' in main;
int main(){
struct st st_arr[2];
st_arr[0].a = 5;
st_arr[1].a = 10;
st_arr[4].a = 20;
printf("%d %d %dn", st_arr[0].a, st_arr[1].a, st_arr[4].a);
}
i have assigned 2 array of structs but it lets me index 4th of the struct why is that? Isn't it suppose to give me an error?
the output is:
5 10 20
c arrays
marked as duplicate by Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 10:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
Because there is nobody checking that you go out of bounds,,,,
– Paul Ogilvie
Nov 20 '18 at 10:20
2
It is called undefined behavior meaning it can go wrong, it can go right, your program may abort or anything else can happen, either immediately or at a later stage in your program.
– Paul Ogilvie
Nov 20 '18 at 10:22
1
I know you wanted to be sure but it's important to understand thatst_arr[2]
is already out of bounds, despite its resemblance to the array definition.
– Peter A. Schneider
Nov 20 '18 at 10:31
thank you so much for your answers
– M. O. Karaköz
Nov 20 '18 at 10:44
add a comment |
This question already has an answer here:
How dangerous is it to access an array out of bounds?
10 answers
Sorry for the bad title and i am new to the C but here is the part i don't understand.
I have a simple struct;
struct st{
int a;
int b;
};
and i am creating array of struct and indexing values to variable 'a' in main;
int main(){
struct st st_arr[2];
st_arr[0].a = 5;
st_arr[1].a = 10;
st_arr[4].a = 20;
printf("%d %d %dn", st_arr[0].a, st_arr[1].a, st_arr[4].a);
}
i have assigned 2 array of structs but it lets me index 4th of the struct why is that? Isn't it suppose to give me an error?
the output is:
5 10 20
c arrays
This question already has an answer here:
How dangerous is it to access an array out of bounds?
10 answers
Sorry for the bad title and i am new to the C but here is the part i don't understand.
I have a simple struct;
struct st{
int a;
int b;
};
and i am creating array of struct and indexing values to variable 'a' in main;
int main(){
struct st st_arr[2];
st_arr[0].a = 5;
st_arr[1].a = 10;
st_arr[4].a = 20;
printf("%d %d %dn", st_arr[0].a, st_arr[1].a, st_arr[4].a);
}
i have assigned 2 array of structs but it lets me index 4th of the struct why is that? Isn't it suppose to give me an error?
the output is:
5 10 20
This question already has an answer here:
How dangerous is it to access an array out of bounds?
10 answers
c arrays
c arrays
asked Nov 20 '18 at 10:20
M. O. KaraközM. O. Karaköz
44
44
marked as duplicate by Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 10:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 10:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
Because there is nobody checking that you go out of bounds,,,,
– Paul Ogilvie
Nov 20 '18 at 10:20
2
It is called undefined behavior meaning it can go wrong, it can go right, your program may abort or anything else can happen, either immediately or at a later stage in your program.
– Paul Ogilvie
Nov 20 '18 at 10:22
1
I know you wanted to be sure but it's important to understand thatst_arr[2]
is already out of bounds, despite its resemblance to the array definition.
– Peter A. Schneider
Nov 20 '18 at 10:31
thank you so much for your answers
– M. O. Karaköz
Nov 20 '18 at 10:44
add a comment |
4
Because there is nobody checking that you go out of bounds,,,,
– Paul Ogilvie
Nov 20 '18 at 10:20
2
It is called undefined behavior meaning it can go wrong, it can go right, your program may abort or anything else can happen, either immediately or at a later stage in your program.
– Paul Ogilvie
Nov 20 '18 at 10:22
1
I know you wanted to be sure but it's important to understand thatst_arr[2]
is already out of bounds, despite its resemblance to the array definition.
– Peter A. Schneider
Nov 20 '18 at 10:31
thank you so much for your answers
– M. O. Karaköz
Nov 20 '18 at 10:44
4
4
Because there is nobody checking that you go out of bounds,,,,
– Paul Ogilvie
Nov 20 '18 at 10:20
Because there is nobody checking that you go out of bounds,,,,
– Paul Ogilvie
Nov 20 '18 at 10:20
2
2
It is called undefined behavior meaning it can go wrong, it can go right, your program may abort or anything else can happen, either immediately or at a later stage in your program.
– Paul Ogilvie
Nov 20 '18 at 10:22
It is called undefined behavior meaning it can go wrong, it can go right, your program may abort or anything else can happen, either immediately or at a later stage in your program.
– Paul Ogilvie
Nov 20 '18 at 10:22
1
1
I know you wanted to be sure but it's important to understand that
st_arr[2]
is already out of bounds, despite its resemblance to the array definition.– Peter A. Schneider
Nov 20 '18 at 10:31
I know you wanted to be sure but it's important to understand that
st_arr[2]
is already out of bounds, despite its resemblance to the array definition.– Peter A. Schneider
Nov 20 '18 at 10:31
thank you so much for your answers
– M. O. Karaköz
Nov 20 '18 at 10:44
thank you so much for your answers
– M. O. Karaköz
Nov 20 '18 at 10:44
add a comment |
1 Answer
1
active
oldest
votes
C does not provide a(ny) bound checking by default.
Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
C does not provide a(ny) bound checking by default.
Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
add a comment |
C does not provide a(ny) bound checking by default.
Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
add a comment |
C does not provide a(ny) bound checking by default.
Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.
C does not provide a(ny) bound checking by default.
Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.
answered Nov 20 '18 at 10:23
Sourav GhoshSourav Ghosh
109k14129188
109k14129188
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
add a comment |
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
Maybe you should change "defined" to "specified" so that I can get back out of the rabbit hole ;-)
– Peter A. Schneider
Nov 20 '18 at 10:28
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
@PeterA.Schneider That was intentional. :)
– Sourav Ghosh
Nov 20 '18 at 10:30
add a comment |
4
Because there is nobody checking that you go out of bounds,,,,
– Paul Ogilvie
Nov 20 '18 at 10:20
2
It is called undefined behavior meaning it can go wrong, it can go right, your program may abort or anything else can happen, either immediately or at a later stage in your program.
– Paul Ogilvie
Nov 20 '18 at 10:22
1
I know you wanted to be sure but it's important to understand that
st_arr[2]
is already out of bounds, despite its resemblance to the array definition.– Peter A. Schneider
Nov 20 '18 at 10:31
thank you so much for your answers
– M. O. Karaköz
Nov 20 '18 at 10:44