LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2005, 03:01 PM   #1
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Rep: Reputation: 15
struct pointer in C


I have been told that the only way to pass a structure from one class to another is through using a pointer.

Am I doing this correctly


foo.h
Code:
 
typedef struct foo_str *foo;
foo.c
Code:
struct foo_str
{
   int blah
   float blaha
}foo_str;
calc.h
Code:
typedef struct calc_str *calc;
calc.c
Code:
struct calc_str
{
   foo *mypointer;
   int count;
}calc_str;

... later in a function  
food = foo.blah;

thanks again!

Last edited by LuderForChrist; 01-06-2005 at 03:03 PM.
 
Old 01-06-2005, 03:42 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Your code actually has nothing to do with classes, which actually don't even exist in C. I also don't see any definition of food which you're using calc.c

Traditionally, struct definitions go in the header file, and you can include one struct inside another without the use of a pointer. Your typedef'ing is also a bit weird.

For instance, you can do this:
Code:
// foo.h

#ifndef FOO_H_
#define FOO_H_

struct foo_str
{
  int blah;
  float blaha;
};

typedef struct foo_str foo;

#endif // FOO_H_
Code:
// calc.h

#ifndef CALC_H_
#define CALC_H_

#include "foo.h"

struct calc_str
{
  foo foostuff;
  int count;
};

typedef struct calc_str calc;

#endif // CALC_H_
Code:
// calc.c

#include "foo.h"
#include "calc.h"

int main(void)
{
  calc mycalc;

  mycalc.foostuff.blah = 86;

  return 0;
}
 
Old 01-07-2005, 07:44 AM   #3
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks, What if in another file other than the other two, I have a calc myclalc, a calc hiscalc, and a calc hercalc?

Im getting a first defined here error.

EDIT: Fixed nevermind THANKS!!!

Last edited by LuderForChrist; 01-07-2005 at 08:03 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration