LinuxQuestions.org
Review your favorite Linux distribution.
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 08-20-2006, 06:30 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
I just want to make sure this is correct, struct pointer array paramter in function.


I've been doing things lately which have been 'no no's' and and so I would like to make sure this won't explode my computer or my head. It is a program that shows that I can have a struct pointer array as a parameter in a function. I was fighting this for about.. 2 minutes until I remembered that I read in C++ for Dummies that arrays are nothing more then pointers to locations of memory... strange... something useful came out of that book.. but anyway is that true? Or am I mistaken in someway. Here is the code for someone to dig around and burn me for something I did wrong .

Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <time.h>
using namespace std;

struct MyStruct
{
	int number;
};

// I remembered that line (written in the above paragraph) where arrays are nothing more then
// pointers, when I was fighting the splat sign to work with the paramter in func2.
void func2( MyStruct pms[], int size_of_struct )
{
	for ( int i = 0; i < size_of_struct; i++ )
		pms[i].number = 0;
}

int main()
{
	srand(time(NULL));
	
	MyStruct ms[8];
	
	func2(ms,8);
	
	for ( int i = 0; i < 8; i++ )
		cout << ms[i].number << endl;
	
	return 0;
}
--UPDATE--

I found a problem that I don't know how to fix. I want to pass a vector<struct> pointer, but I'm getting an error when I try to add values to anything within that struct. Here is the code followed by the error output:

Code:
struct MyStruct
{
    int number;
}

void func3( vector<MyStruct> * pvms )
{
	for ( unsigned i = 0; i < pvms->size(); i++ )
		pvms[i]->number = rand()%100; // Error is at this line. Read error output.
}

void func4 ( vector<MyStruct> pvms )
{
	func3( &pvms );
	
	for ( unsigned i = 0; i < pvms.size(); i++ )
	cout << pvms[i].number << endl;
}

int main()
{
	srand(time(NULL));
	
	vector<MyStruct> vms(8);
	
	func4(vms); // Doesn't change vms, but thats not what my problem is.
	
	return 0;
}
And here is the error output:

Code:
george@geotop ~/SpaceCarnage/LearningPros $ g++ -g ArrayARGUEMNT\!\!\!.cc -Wall -Wextra -o ArrayARGUEMNT\!\!\!
ArrayARGUEMNT!!!.cc: In function `void func3(std::vector<MyStruct, std::allocator<MyStruct> >*)':
ArrayARGUEMNT!!!.cc:29: error: base operand of `->' has non-pointer type `std::vector<MyStruct, std::allocator<MyStruct> >'
george@geotop ~/SpaceCarnage/LearningPros $

Last edited by RHLinuxGUY; 08-20-2006 at 07:25 AM.
 
Old 08-20-2006, 08:31 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
this line in func3 doesn't make any sense
Quote:
pvms[i]->number
pvms is a pointer to a vector; the index [i] dereferences the pointer but is not valid the second time (as theres only one vector). You then dereference something which isn't a pointer.

I changed you code and used iterators as this is the whole purpose of them, an iterator is a pointer to an element. begin() gives you the iterator which points to the first element of the structure( how strange lol) and end() gives you a iterator one element passed the last element; this is why the for loop checks i != pvms.end() .

Code:
#include <iostream>
#include <ctime>
#include <vector>
struct MyStruct
{
    int number;
};//missing ';'

void func3( std::vector<MyStruct> * pvms )
{
	for (std::vector<MyStruct> ::iterator i = pvms->begin(); i != pvms->end(); ++i )
		i->number = rand()%100;
}

void func4 ( std::vector<MyStruct> pvms )
{
	func3( &pvms );
	
	for (std::vector<MyStruct> ::iterator i = pvms.begin(); i != pvms.end(); ++i )
		std::cout <<i->number << std::endl;
}

int main(int argc, char* argv[ ])
{
	srand(time(NULL));
	
	std::vector<MyStruct> vms(8);//create a vector with a default size of 8 elements
	func4(vms);

	return 0;
}

Last edited by dmail; 08-20-2006 at 08:35 AM.
 
Old 08-20-2006, 01:36 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> I want to pass a vector<struct> pointer

why are you not passing a reference instead of a pointer?

> //Doesn't change vms, but thats not what my problem is.

it is not changing vms because you are passing by value which in this case means you are creating a copy of the vector to be passed to func4(). which is not only keeping your changes from being reflected in the vector from main, but is also extremely inefficient. passing by reference will solve this and will also allow your changes to be made to the vector from main.

i left the iterators that dmail had put in out. i dont usually bother with iterators for vectors. imo they are not needed if you are simply reading elements in order, or performing insertions at the beginning or end.

Code:
void func3(vector<MyStruct>& pvms)
{
	for ( unsigned i = 0; i < pvms.size(); i++ )
		pvms[i].number = rand()%100;
}

void func4 (vector<MyStruct>& pvms)
{
	func3( pvms );
	
	for ( unsigned i = 0; i < pvms.size(); i++ )
	    cout << pvms[i].number << endl;
}

int main()
{
	srand(time(NULL));
	
	vector<MyStruct> vms(8);
	
	func4(vms);
	
	return 0;
}

Last edited by xhi; 08-20-2006 at 01:38 PM.
 
Old 08-20-2006, 06:24 PM   #4
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
Quote:
Code:
> //Doesn't change vms, but thats not what my problem is.
O no, I know that it doesn't change the value of vms, I was doing that while I was learning to pass arrays into functions. I didn't leave it out though, yes I know it just copys the whole vector into the parameter and isn't changed throughout the program. But thanks for the information though guys, I appreciate it.

--By the way--
This is a shot in the dark, but is the reason why you can pass reference to an array instead of a pointer, is because arrays are just pointers? Or so says one of my books (C++ for Dummies, C++ Language 3rd Edition).

Last edited by RHLinuxGUY; 08-20-2006 at 06:28 PM.
 
Old 08-20-2006, 07:43 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Yes, in some ways C pointers and arrays are equivalent (Kernighan and Ritchie say so themselves). But no: they are not "the same" - there are important (albeit often subtle) distinctions!

And yes, you should also probably be using references instead of pointers.

This isn't the first time that your "C++ for Dummies" book has given you misleading information:

http://www.linuxquestions.org/questi...d.php?t=411727

I would definitely consider doing yourself a favor and buying a new book. Here are a couple of strong suggestions:

The C++ Programming Language, Bjarne Stroustrup
Thinking in C++, Bruce Eckel
Effective C++, Scott Meyers

These are my preferences; others will have other suggestions.

But it sounds like the book you have now is hurting you more than helping. I'd consider losing it ... and getting a new book.

IMHO .. PSM
 
Old 08-20-2006, 07:49 PM   #6
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
The C++ Programming Language, Bjarne Stroustrup, I bought that one just recently.. the third edtion. Not so easy to understand, but I have aquired great information. This book showed me vectors, maps, list, string manipulation functions, and a few do's and don't's.
 
Old 08-20-2006, 09:30 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by RHLinuxGUY
The C++ Programming Language, Bjarne Stroustrup, I bought that one just recently.. the third edtion. Not so easy to understand, but I have aquired great information. This book showed me vectors, maps, list, string manipulation functions, and a few do's and don't's.
i would second paulsm4's advice, along with the book list.

i would add that i think Stroustrup's book is a little thick to start out learning c++ with, it may be good to wait to dive into it when you a fairly firm grasp on the language (imo anyhow). i would recommend bruce eckel's books (vol1,2) to begin with.

as for the pointer array situation, there are many times when an array is/can-be treated as a pointer.. however to say it is always so, well that is just plain wrong.. that seems to be a fault in several books i have come across, i guess For Dummies is now on that list.. where is a good book burning when you need one..
 
  


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
array of struct ChimpFace9000 Programming 7 12-20-2017 11:46 PM
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
struct pointer in C LuderForChrist Programming 2 01-07-2005 07:44 AM
sending pointer array to function marek Programming 4 04-15-2004 04:46 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 07:53 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