I just want to make sure this is correct, struct pointer array paramter in function.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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;
}
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;
}
> //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.
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:
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.
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.