By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Print a struct

P: n/a
I am unable to add anything to this structI can't understand why I am
getting a Segfault:

#include <stdio.h>
struct macd
{
int price;
};
void print_combo( struct macd* );
int main(void)
{
struct macd* my_combo = NULL;

my_combo->price = 100;
return 0;
}
void print_combo( struct macd* mc )
{
if( mc )
{
printf("Price = %d\n", mc->price);

}
}

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Nov 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 7, 9:28*pm, arnuld <sunr...@invalid.addresswrote:
I am unable to add anything to this structI can't understand why I am
getting a Segfault:

#include <stdio.h>

struct macd
{
* int price;

};

void print_combo( struct macd* );

int main(void)
{
* struct macd* my_combo = NULL;
Maybe because of this statement.
>
* my_combo->price = 100;

* return 0;

}

void print_combo( struct macd* mc )
{
* if( mc )
* * {
* * * printf("Price = %d\n", mc->price);

* * }

}

--www.lispmachine.wordpress.com
my email is @ the above blog.
Nov 7 '08 #2

P: n/a
On Fri, 07 Nov 2008 18:28:50 +0500,
arnuld <su*****@invalid.addresswrote:
I am unable to add anything to this structI can't understand why I am
getting a Segfault:
struct macd
{
int price;
};
int main(void)
{
struct macd* my_combo = NULL;
This is not a struct. It's a pointer to a struct. And it doesn't point
to anywhere where there could be a struct.
my_combo->price = 100;
You're trying to dereference the pointer that you just set to NULL
yourself. You can't do that. Either allocate memory with malloc, or use
a struct.
struct macd *my_combo ;
macd = malloc(sizeof *macd);
my_combo->price = 100;

OR

struct macd my_combo;
my_combo.price = 100;

Just like any other pointer, a struct pointer needs to point to
some memory.

Martien
--
|
Martien Verbruggen | Since light travels faster than sound, is
| that why some people appear bright until you
| hear them speak?
Nov 7 '08 #3

P: n/a
On November 7, 2008 08:28, in comp.lang.c, arnuld (su*****@invalid.address)
wrote:
I am unable to add anything to this structI can't understand why I am
getting a Segfault:

#include <stdio.h>
struct macd
{
int price;
};
void print_combo( struct macd* );
int main(void)
{
struct macd* my_combo = NULL;
my_combo is a pointer to a structure. The pointer is set to NULL. There is
no space allocated to the structure.
my_combo->price = 100;
Here, you put a value into the structure. Where did this value wind up?
Remember, at this point, the structure only exists in potential, and /not/
as an object that can be modified. So, what did you modify?

Remember also that my_combo is a pointer, which (at the moment) points to
(the official) nowhere. So, again, where did your value of 100 go?
>
return 0;
}
[snip]

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Nov 7 '08 #4

P: n/a
On Nov 7, 6:28*pm, arnuld <sunr...@invalid.addresswrote:
I am unable to add anything to this structI can't understand why I am
getting a Segfault:

#include <stdio.h>

struct macd
{
* int price;

};

void print_combo( struct macd* );

int main(void)
{
* struct macd* my_combo = NULL;

my_combo = malloc(sizeof(struct macd)); // Allocating Memory

This should work fine.

* my_combo->price = 100;

* return 0;

}

void print_combo( struct macd* mc )
{
* if( mc )
* * {
* * * printf("Price = %d\n", mc->price);

* * }

}

--www.lispmachine.wordpress.com
my email is @ the above blog.
Nov 11 '08 #5

P: n/a
On 11 Nov, 05:54, santoshsy <santos...@gmail.comwrote:
* * my_combo = malloc(sizeof(struct macd)); * // Allocating Memory
Better is:

my_combo = malloc( sizeof *my_combo );

Although in this case it is probably best to declare
an actual struct:

struct macd foo;
struct macd *my_combo;
my_combo = &foo;

Or even just do:

struct macd my_combo[ 1 ];

Nov 11 '08 #6

P: n/a
On 7 Nov, 13:59, Lew Pitcher <lpitc...@teksavvy.comwrote:
On November 7, 2008 08:28, in comp.lang.c, arnuld (sunr...@invalid.address)
wrote:
* struct macd* my_combo = NULL;
* my_combo->price = 100;
<snip>
>
Remember also that my_combo is a pointer, which (at the moment) points to
(the official) nowhere. So, again, where did your value of 100 go?
It went to nowhere plus offsetof( struct macd, price ), of course! :)
Nov 11 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.