Click to See Complete Forum and Search --> : arguments array's and structures used as pointers and by-reference
Defiant
June 18th, 2002, 06:33 AM
Hiya,
pointers shouldnt be a difficult subject, BUTT for the n00b like me, it is!! :(
In the books that i read, i cant really figure it out, regretfully.
my problem lies in handling an struct after is has been used as argument of an function.
With a struct as an:
value-based argument,
a reference and
as a pointer.
The way to acces the structures after you have passed it with one of those types of arguments.
I just dont get it. I can figure it out, programwise, but then i dont understand it. I got my piece of tutorial based source working, but i just dont really get it. The same thing goes partially with arrays and ofcourde an array of structures.
Another question:
Is it ONLY possible to input chars in an char array (in an struct) throu strcpy? or can you simply do this too?:
cin >> p.CharMember
Perhaps you ppl have example source of different variety which i can look at about arrays and structures passed as an argument and then used and accessed in different ways. I think that if is see enough source about this subject, i would be able to understand it.
I appreciate any help!!
Simon Wilkins
June 18th, 2002, 06:47 AM
Try this:
typedef struct MyStruct
{
int x;
long y;
}
ByRef( MyStruct& foo )
{
foo.x = 6;
foo.y = 10;
}
ByVal( MysTruct foo )
{
foo.x=1;
foo.y=2;
}
ByPtr( MyStruct* foo )
{
foo->x = 1;
foo->y = 2;
}
main()
{
MyStruct foo;
ByRef( foo );
ASSERT(foo.x==6 && foo.y==10);
ByVal( foo );
ASSERT(foo.x==6 && foo.y==10);
ByPtr( &foo );
ASSERT(foo.x==1 && foo.y==2);
}
You can access a char array just like any other array:
I think you can use cin >> p.CharMember however if the input is longer than the memory allocated for CharMember then you will corrupt the memory.
Hope this helps
Defiant
June 18th, 2002, 09:19 AM
Thanks for the quick respons, Simon.
i still have a issue with an array of structures.
because i thought they are accesed a little different.
I hope that you can help me with that perhaps aswell or someone else
Simon Wilkins
June 18th, 2002, 10:54 AM
Arrays are not any different to pointers. You can pass an array into a function as follows:
ByPtr( MyStruct* foo )
{
foo[0].x = 1;
foo[0].y = 2;
}
ByArray( MyStruct foo[] )
{
foo[0].x = 7;
foo[0].y = 8;
}
main()
{
MyStruct foo[100];
ByArray( foo );
ASSERT(foo[0].x == 7 && foo[0].y == 8);
ByPtr( foo ); //effectively &foo[0] pointer to the first array element
ASSERT(foo[0].x == 1 && foo[0].y == 2);
ByPtr( &foo[1] ); //Pointer to the second array element
ASSERT(foo[1].x == 1 && foo[1].y == 2);
}
The ByArray function is the same the ByPtr function, specifying an array index just dereferences the pointer. Therefore you can pass the pointer to an array element into the ByPtr function and it will work.
All that happens when you specify an array index is that the pointer to the first element is increment by the array index * the size of an array element.
Just because you can do all this doesn't always mean it would be good practice to :)
Federal102
June 18th, 2002, 10:57 AM
Try this out for arrays....
struct myStruct
{
char array[10];
}
void printByVal(myStruct structArray[])
{
cout << "You entered " << structArray[0].array << endl;
}
void printByRef(myStruct (&structArray)[10])
{
cout << "You entered " << structArray[0].array << endl;
}
void printByPointer(myStruct (*structArray)[10])
{
cout << "You entered " << structArray[0]->array << endl;
}
int main()
{
myStruct structArray[10];
cout << "Enter string : " ;
cin >> structArray[0].array;
printByVal(structArray);
printByRef(structArray);
myStruct (*pstructArray)[10];
pstructArray = &structArray;
printByPointer(pstructArray);
return 0;
}
Instead of using arrays, why don't you try vectors. That way you won't have to worry about memory corruption so much.
Defiant
June 19th, 2002, 05:24 AM
Thanks for the respons again dudes
ppl like you make this forum a sanctuarium for n00bs like me.
btw about vectors...
i am merely 90 pages away from that chapter :)
so thats why i am not using vectors yet.
thanks again, it is very helpfull.
signed,
Defiant
Federal102
June 19th, 2002, 06:19 AM
I would not get too heavy with arrays. Get through those 90 pages ASAP and start looking at some STL. You'll be glad you did!
Paul McKenzie
June 19th, 2002, 11:09 AM
Originally posted by Defiant
Thanks for the respons again dudes
ppl like you make this forum a sanctuarium for n00bs like me.
btw about vectors...
i am merely 90 pages away from that chapter :)
so thats why i am not using vectors yet.
thanks again, it is very helpfull.
signed,
Defiant Your book seems to be doing things backwards than what is done in modern, good C++ books that are meant to teach the language. The std::string and vector<> classes are introduced very early in the learning process, and pointer handling is left until the middle or end. Yes, pointers are talked about briefly, as to what they are, and some examples are given, but usage of the standard library is emphasized at the beginning instead of arrays and pointers. For example,
#include <string>
#include <iostream>
int main()
{
std::string s;
std::cin >> s;
}
This is how a first chapter, "how do you read in a string of characters?" example should be coded in a C++ book. There is no usage of pointers, so the beginner is not bogged down with the details of "what is a pointer", or "what is this new thing supposed to do?" and other such questions. To do the equivalent code without std::string is much more than just declaring an array of char. Since std::string is dynamic (grows when you add to it), the tiny bit of code above does much more than what a newcomer could write correctly (or understand) if they just used char arrays and pointers.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.