LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-24-2011, 06:29 PM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
help with structs and pointers in C++


Ok this is my situation I have this header file:
Code:
struct dir
{
	char name[3];
	int root_dir;
	int has_children;
	int num_children;
	int offset_to_children[2];
	int offset_to_files[16];
};
struct files
{
	char name[5];
	int size;
	int offset_to_beginning;
	int has_fragment;
	int next_fragment;
};
struct fat
{
	int files; 
	int dirs; 
	struct dir *dir_array; 
	struct files *file_array; 
};
In my program if I want to access the struct fat member *dir_array or *file_array, given that this member are pointers to another struct how can I access this members from my main program? This is what I'm doing and it compiles:

Code:
	fat *pfat;
	pfat->dir_array->root_dir=0;
My doubt is if I'm doing it right. Can anybody clarify my doubt and point my to the right direction. Thanks!!!
 
Old 05-24-2011, 08:01 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You need to allocate a struct fat and a struct dir before you can actually use it; thus your code should be:
Code:
Fat* fat = new Fat;
fat->dir_array = new Dir[10];

fat->dir_array[0].root_dir = 0;
You could also consider declaring/implementing constructors for your structs.

Code:
#include <cstddef>   // for size_t

struct Dir
{
   Dir() : root_dir(0) {}

   int root_dir;
};

struct Fat
{
   Fat(const size_t numDirs) : dir_array(new Dir[numDirs]) {}

   Dir* dir_array;
};

...

Fat* fat = new Fat(10);
...

P.S. It is typical to use an uppercase letter at the beginning of a struct or class name. Hungarian Notation is also looked at with disdain.

P.S. #2 In C++, a struct and a class are identical in every way except on one issue; with a struct, all data members and methods are by default classified as publicly accessible, whereas with a class, by default everything is considered private.

Last edited by dwhitney67; 05-24-2011 at 08:05 PM.
 
Old 05-24-2011, 08:37 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by darkangel29 View Post
In my program if I want to access the struct fat member *dir_array or *file_array, given that this member are pointers to another struct how can I access this members from my main program?

Code:
    pfat->dir_array->root_dir=0;
My doubt is if I'm doing it right. Can anybody clarify my doubt and point my to the right direction.
That is the correct syntax for that instruction.

dwhitney67 is correct that you need to initialize each pointer to point to a valid object (or array of objects) before dereferencing the pointer. But I expect you already knew that and were only asking the syntax question.

Then we have the style questions:

Quote:
Code:
struct fat
{
    int files; 
    int dirs; 
    struct dir *dir_array; 
    struct files *file_array; 
};
The two uses of the keyword struct that I marked in red are such bad C++ style that I am suggesting you delete them (even though you didn't ask a style question). This is C++, not C. A declared struct is a type on it's own. It doesn't need to be qualified with the keyword struct when used.

Quote:
Originally Posted by dwhitney67 View Post
P.S. It is typical to use an uppercase letter at the beginning of a struct or class name. Hungarian Notation is also looked at with disdain.
I guess there is a lot of room for disagreement about which style details are so bad they should be commented on even when a non style question was asked.

I'm used to a coding standard in which a struct or class name is always entirely lower case. I see nothing wrong with that style decision. I certainly wouldn't change an OP's variable naming (making an example highlighting other issues harder to follow) just to "correct" that minor style difference.

I also personally hate Hungarian Notation. But I think that comment was in reference to a pointer variable name starting with p. That still wasn't a valid Hungarian Notation name, and I know lots of programmers who love Hungarian Notation. That whole debate doesn't belong here.

Also,
Code:
	pfat->dir_array->root_dir=0;
and
Code:
	pfat->dir_array[0].root_dir=0;
generate the same compiled code. The difference is in readability. I expect dwhitney67 correctly inferred the intended use of the dir_array pointer, so his suggested use of [0]. is better style than the original ->

Last edited by johnsfine; 05-24-2011 at 08:49 PM.
 
Old 05-24-2011, 11:23 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
Q: In my program if I want to access the struct fat member *dir_array or *file_array, given that this member are pointers to another struct how can I access this members from my main program?
A: Similar to what dwhitney already suggested, this should work fine:
Code:
  struct dir dirs[10];
  struct files files[10];
  struct fat fat;

  memset (&fat, 0, sizeof (fat));
  fat.dir_array = dirs;
  fat.file_array = files;

  fat.dir_array[0].root_dir = ...
Quote:
The two uses of the keyword struct that I marked in red are such bad C++ style that I am suggesting you delete them (even though you didn't ask a style question).
Nonsense.

To the extent you're using C-style structs (as opposed to C++ style classes), what you're doing is fine. And frankly, there's NOTHING wrong with using C-style structs if they happen to be appropriate to the job at hand.

IMHO .. PSM

Last edited by paulsm4; 05-25-2011 at 12:37 AM.
 
Old 05-25-2011, 05:15 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by paulsm4 View Post
To the extent you're using C-style structs (as opposed to C++ style classes), what you're doing is fine. And frankly, there's NOTHING wrong with using C-style structs if they happen to be appropriate to the job at hand.
The use of the keywords 'struct' and 'class' are virtually synonymous in C++; there's a minor difference in their properties (I explained that earlier), but one should not treat them differently simply because they may have experience programming in C. The majority C++ programmers drop the 'struct' keyword when declaring a variable of said type. It is similar to dropping the 'class' keyword.

You would (hardly) ever see someone do this:
Code:
class Foo
{
   ...
};

...

class Foo foo;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
C++ question on Structs mrplopez1972 Programming 3 01-24-2011 12:07 AM
C: structs and pointers question kpachopoulos Programming 3 03-27-2006 06:54 PM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 06:41 PM
static structs? simbo Programming 3 02-05-2004 05:00 AM
Self referential structs in C? MadCactus Programming 14 01-28-2004 06:29 PM

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

All times are GMT -5. The time now is 08:28 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration