Structure pointer returning the start byte of member of structure [duplicate]
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
c memory struct segmentation-fault
marked as duplicate by Some programmer dude
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.
|
show 20 more comments
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
c memory struct segmentation-fault
marked as duplicate by Some programmer dude
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 forNODE *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 toprintf("%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
|
show 20 more comments
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
c memory struct segmentation-fault
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
c memory struct segmentation-fault
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
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
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 forNODE *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 toprintf("%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
|
show 20 more comments
2
You are not allocating memory forNODE *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 toprintf("%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
|
show 20 more comments
1 Answer
1
active
oldest
votes
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.
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%xbut%pformat specifier.
– Gerhardh
Nov 20 '18 at 11:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%xbut%pformat specifier.
– Gerhardh
Nov 20 '18 at 11:18
add a comment |
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.
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%xbut%pformat specifier.
– Gerhardh
Nov 20 '18 at 11:18
add a comment |
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.
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.
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%xbut%pformat specifier.
– Gerhardh
Nov 20 '18 at 11:18
add a comment |
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%xbut%pformat 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
add a comment |

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