Print a struct

A

arnuld

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

}
}
 
L

LoYwEnG

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

Martien Verbruggen

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
 
L

Lew Pitcher

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

santoshsy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top