Why array of struct lets me index to out of boundary array [duplicate]












-1
















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










share|improve this question













marked as duplicate by Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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 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
















-1
















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










share|improve this question













marked as duplicate by Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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 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














-1












-1








-1









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










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 10:20









M. O. KaraközM. O. Karaköz

44




44




marked as duplicate by Lundin arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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 arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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 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














  • 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








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












1 Answer
1






active

oldest

votes


















1














C does not provide a(ny) bound checking by default.



Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.






share|improve this answer
























  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














C does not provide a(ny) bound checking by default.



Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.






share|improve this answer
























  • 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
















1














C does not provide a(ny) bound checking by default.



Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.






share|improve this answer
























  • 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














1












1








1







C does not provide a(ny) bound checking by default.



Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.






share|improve this answer













C does not provide a(ny) bound checking by default.



Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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



Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]