Structure pointer returning the start byte of member of structure [duplicate]












-3
















This question already has an answer here:




  • Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer

    4 answers




Structure pointer returning the start byte of member of structure
.I wondered how it works. PLease some one help me to get know about this.



 #include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
NODE * m;
printf("%u",&m->mem3);
return 0;
}


Output is 16










share|improve this question















marked as duplicate by Some programmer dude c
Users with the  c badge can single-handedly close c 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 9:57


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.











  • 2





    You are not allocating memory for NODE *m. You are invoking undefined behavior.

    – Micha Wiedenmann
    Nov 20 '18 at 9:55













  • Can you elaborate on your problem?

    – Sourav Ghosh
    Nov 20 '18 at 9:56











  • More or less the exact same question as this one. Please tell the rest of your class to not post duplicates.

    – Some programmer dude
    Nov 20 '18 at 9:58













  • Also, change the print to printf("%c", m->mem3);. Note how you don't need the &, you're already dereferencing with the -> operator.

    – Blaze
    Nov 20 '18 at 9:59











  • Its is returning the starting byte of structure member here.(i.e) mem-0, mem2-8 , mem3 -16, mem4 -20 ,mem5-24 , mem6 - 25. How it returning the starting byte number. @ Micha Wiedenmann @ Sourav Ghosh @Blaze

    – AADHI A.R
    Nov 20 '18 at 10:03
















-3
















This question already has an answer here:




  • Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer

    4 answers




Structure pointer returning the start byte of member of structure
.I wondered how it works. PLease some one help me to get know about this.



 #include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
NODE * m;
printf("%u",&m->mem3);
return 0;
}


Output is 16










share|improve this question















marked as duplicate by Some programmer dude c
Users with the  c badge can single-handedly close c 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 9:57


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.











  • 2





    You are not allocating memory for NODE *m. You are invoking undefined behavior.

    – Micha Wiedenmann
    Nov 20 '18 at 9:55













  • Can you elaborate on your problem?

    – Sourav Ghosh
    Nov 20 '18 at 9:56











  • More or less the exact same question as this one. Please tell the rest of your class to not post duplicates.

    – Some programmer dude
    Nov 20 '18 at 9:58













  • Also, change the print to printf("%c", m->mem3);. Note how you don't need the &, you're already dereferencing with the -> operator.

    – Blaze
    Nov 20 '18 at 9:59











  • Its is returning the starting byte of structure member here.(i.e) mem-0, mem2-8 , mem3 -16, mem4 -20 ,mem5-24 , mem6 - 25. How it returning the starting byte number. @ Micha Wiedenmann @ Sourav Ghosh @Blaze

    – AADHI A.R
    Nov 20 '18 at 10:03














-3












-3








-3









This question already has an answer here:




  • Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer

    4 answers




Structure pointer returning the start byte of member of structure
.I wondered how it works. PLease some one help me to get know about this.



 #include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
NODE * m;
printf("%u",&m->mem3);
return 0;
}


Output is 16










share|improve this question

















This question already has an answer here:




  • Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer

    4 answers




Structure pointer returning the start byte of member of structure
.I wondered how it works. PLease some one help me to get know about this.



 #include <stdio.h>

typedef struct node {
char mem;
double mem2;
char mem3;
int mem4;
char mem5;
char mem6;
}NODE;
int main()
{
NODE * m;
printf("%u",&m->mem3);
return 0;
}


Output is 16





This question already has an answer here:




  • Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer

    4 answers








c memory struct segmentation-fault






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 10:37







AADHI A.R

















asked Nov 20 '18 at 9:53









AADHI A.RAADHI A.R

155




155




marked as duplicate by Some programmer dude c
Users with the  c badge can single-handedly close c 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 9:57


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 Some programmer dude c
Users with the  c badge can single-handedly close c 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 9:57


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.










  • 2





    You are not allocating memory for NODE *m. You are invoking undefined behavior.

    – Micha Wiedenmann
    Nov 20 '18 at 9:55













  • Can you elaborate on your problem?

    – Sourav Ghosh
    Nov 20 '18 at 9:56











  • More or less the exact same question as this one. Please tell the rest of your class to not post duplicates.

    – Some programmer dude
    Nov 20 '18 at 9:58













  • Also, change the print to printf("%c", m->mem3);. Note how you don't need the &, you're already dereferencing with the -> operator.

    – Blaze
    Nov 20 '18 at 9:59











  • Its is returning the starting byte of structure member here.(i.e) mem-0, mem2-8 , mem3 -16, mem4 -20 ,mem5-24 , mem6 - 25. How it returning the starting byte number. @ Micha Wiedenmann @ Sourav Ghosh @Blaze

    – AADHI A.R
    Nov 20 '18 at 10:03














  • 2





    You are not allocating memory for NODE *m. You are invoking undefined behavior.

    – Micha Wiedenmann
    Nov 20 '18 at 9:55













  • Can you elaborate on your problem?

    – Sourav Ghosh
    Nov 20 '18 at 9:56











  • More or less the exact same question as this one. Please tell the rest of your class to not post duplicates.

    – Some programmer dude
    Nov 20 '18 at 9:58













  • Also, change the print to printf("%c", m->mem3);. Note how you don't need the &, you're already dereferencing with the -> operator.

    – Blaze
    Nov 20 '18 at 9:59











  • Its is returning the starting byte of structure member here.(i.e) mem-0, mem2-8 , mem3 -16, mem4 -20 ,mem5-24 , mem6 - 25. How it returning the starting byte number. @ Micha Wiedenmann @ Sourav Ghosh @Blaze

    – AADHI A.R
    Nov 20 '18 at 10:03








2




2





You are not allocating memory for NODE *m. You are invoking undefined behavior.

– Micha Wiedenmann
Nov 20 '18 at 9:55







You are not allocating memory for NODE *m. You are invoking undefined behavior.

– Micha Wiedenmann
Nov 20 '18 at 9:55















Can you elaborate on your problem?

– Sourav Ghosh
Nov 20 '18 at 9:56





Can you elaborate on your problem?

– Sourav Ghosh
Nov 20 '18 at 9:56













More or less the exact same question as this one. Please tell the rest of your class to not post duplicates.

– Some programmer dude
Nov 20 '18 at 9:58







More or less the exact same question as this one. Please tell the rest of your class to not post duplicates.

– Some programmer dude
Nov 20 '18 at 9:58















Also, change the print to printf("%c", m->mem3);. Note how you don't need the &, you're already dereferencing with the -> operator.

– Blaze
Nov 20 '18 at 9:59





Also, change the print to printf("%c", m->mem3);. Note how you don't need the &, you're already dereferencing with the -> operator.

– Blaze
Nov 20 '18 at 9:59













Its is returning the starting byte of structure member here.(i.e) mem-0, mem2-8 , mem3 -16, mem4 -20 ,mem5-24 , mem6 - 25. How it returning the starting byte number. @ Micha Wiedenmann @ Sourav Ghosh @Blaze

– AADHI A.R
Nov 20 '18 at 10:03





Its is returning the starting byte of structure member here.(i.e) mem-0, mem2-8 , mem3 -16, mem4 -20 ,mem5-24 , mem6 - 25. How it returning the starting byte number. @ Micha Wiedenmann @ Sourav Ghosh @Blaze

– AADHI A.R
Nov 20 '18 at 10:03












1 Answer
1






active

oldest

votes


















0














The program actually should crash because pointer m is not pointing valid memory location. Below code can give the result.



NODE M;
NODE *ptr = &m;
Printf(“%x”, &ptr->mem3);


%x will give hexadecimal address value.






share|improve this answer
























  • It is not crashing you can compile and check also.

    – AADHI A.R
    Nov 20 '18 at 10:08











  • For pointers you should not use %x but %p format specifier.

    – Gerhardh
    Nov 20 '18 at 11:18


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The program actually should crash because pointer m is not pointing valid memory location. Below code can give the result.



NODE M;
NODE *ptr = &m;
Printf(“%x”, &ptr->mem3);


%x will give hexadecimal address value.






share|improve this answer
























  • It is not crashing you can compile and check also.

    – AADHI A.R
    Nov 20 '18 at 10:08











  • For pointers you should not use %x but %p format specifier.

    – Gerhardh
    Nov 20 '18 at 11:18
















0














The program actually should crash because pointer m is not pointing valid memory location. Below code can give the result.



NODE M;
NODE *ptr = &m;
Printf(“%x”, &ptr->mem3);


%x will give hexadecimal address value.






share|improve this answer
























  • It is not crashing you can compile and check also.

    – AADHI A.R
    Nov 20 '18 at 10:08











  • For pointers you should not use %x but %p format specifier.

    – Gerhardh
    Nov 20 '18 at 11:18














0












0








0







The program actually should crash because pointer m is not pointing valid memory location. Below code can give the result.



NODE M;
NODE *ptr = &m;
Printf(“%x”, &ptr->mem3);


%x will give hexadecimal address value.






share|improve this answer













The program actually should crash because pointer m is not pointing valid memory location. Below code can give the result.



NODE M;
NODE *ptr = &m;
Printf(“%x”, &ptr->mem3);


%x will give hexadecimal address value.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 10:02









anandanand

1375




1375













  • It is not crashing you can compile and check also.

    – AADHI A.R
    Nov 20 '18 at 10:08











  • For pointers you should not use %x but %p format specifier.

    – Gerhardh
    Nov 20 '18 at 11:18



















  • It is not crashing you can compile and check also.

    – AADHI A.R
    Nov 20 '18 at 10:08











  • For pointers you should not use %x but %p format specifier.

    – Gerhardh
    Nov 20 '18 at 11:18

















It is not crashing you can compile and check also.

– AADHI A.R
Nov 20 '18 at 10:08





It is not crashing you can compile and check also.

– AADHI A.R
Nov 20 '18 at 10:08













For pointers you should not use %x but %p format specifier.

– Gerhardh
Nov 20 '18 at 11:18





For pointers you should not use %x but %p format specifier.

– Gerhardh
Nov 20 '18 at 11:18



Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith