Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Print a struct

Reply
Thread Tools

Print a struct

 
 
arnuld
Guest
Posts: n/a
 
      11-07-2008
I am unable to add anything to this struct> I 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.


 
Reply With Quote
 
 
 
 
LoYwEnG
Guest
Posts: n/a
 
      11-07-2008
On Nov 7, 9:28*pm, arnuld <(E-Mail Removed)> wrote:
> I am unable to add anything to this struct> I 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.


 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      11-07-2008
On Fri, 07 Nov 2008 18:28:50 +0500,
arnuld <(E-Mail Removed)> wrote:
> I am unable to add anything to this struct> I 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?
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-07-2008
On November 7, 2008 08:28, in comp.lang.c, arnuld ((E-Mail Removed))
wrote:

> I am unable to add anything to this struct> I 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. ------


 
Reply With Quote
 
santoshsy
Guest
Posts: n/a
 
      11-11-2008
On Nov 7, 6:28*pm, arnuld <(E-Mail Removed)> wrote:
> I am unable to add anything to this struct> I 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments