Casting an array?

Started by
7 comments, last by Zeke 22 years, 5 months ago
I have a struct: struct NAME { CString Name1; CString Name2; CString Name3; } and an array of these structs: NAME* NAME_ARR= new NAME[Number]; I also have a function that takes in an array of CString''s: void Func(CString* csArray, int iArray) { //do stuff } I need to pass in an array of NAME''s into this function: Func(NAME_ARR,Number); I only want Name1 from each NAME struct to be passed in. Is there a way of doing this? I know that if I just wanted a single NAME to be passed into a function that takes a CString I can just put an operator (CString) into the NAME struct (im a bit rusty on that so i might be explaining it wrong). But how can I do that with an array of NAME''s? Can anyone help me out? Thanks for any help
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Advertisement
struct NAME
{
CString Name1;
CString Name2;
CString Name3;
}

NAME* NAME_ARR= new NAME[Number];

shouldnt it be:
typedef struct NAME_ARR
{
CString Name1;
CString Name2;
CString Name3;
}

NAME_ARR* NAME = new NAME_ARR[Number];

?

void Func(CString* csArray, int iArray)
{
//do stuff
}

Func(NAME[...].or->Name1,number);

if it wasnt the answer, ask more clearly!

Thanks for the reply but I should have asked more clearly he he

I need to pass in the Name1 of each NAME struct as an array. The more I think about this the more I think it cant be done. Here would be a different way to do what i want.
    struct NAME{CString Name1;CString Name2;CString Name3;}NAME* NAME_ARR= new NAME[Number];void Func(CString* csArray, int iArray){//do stuff}void Main()//or whatever{    CString Name1Array=new CString[NumElementsInNAME_ARR];    for(int x=0;x<NumElementsInNAME_ARR;x++)        Name1Array[x]=NAME_ARR[x].Name1;    Func(Name1Array, NumElementsInNAME_ARR);}    


what i basically want to do is pass in an array consisting of only Name1 (from each element)in the NAME array without having to create a new array.

Im not sure if this is any clearer but hopefully it is.

Thanks for the help


Edited by - Zeke on October 31, 2001 10:16:17 AM
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
quote:Original post by Zeke
what i basically want to do is pass in an array consisting of only Name1 (from each element)in the NAME array without having to create a new array.

Simply not possible. However, you can pass a pointer to the NAME array and obtain the first string from there:
NAME *name_arr = new NAME[number];NAME *name_ptr = name_arr; // points to first NAMEname_ptr->Name1; // same as name_arr[0].Name1;++name_ptr;name_ptr->Name1; // now same as name_arr[1].Name1; 

Based on the above attributes, you should be able to modify your Func() to take to pointers (one pointing at the beginning of NAME_ARR and the other at the location just past the last element of NAME_ARR) and iterate through them:
void Func(NAME *begin, NAME *end){  while(begin != end)  {    // do something using begin->Name1;    ++begin;  }} 
Thanks for the reply.

The reason I cant change the func to take in a NAME* instead of CString* is that i need to call it at other times passing in a CString*.
Ill just have to create a new array of CStrings with the Name1 member of NAME.

Thanks for the help
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
quote:Original post by Zeke
Thanks for the reply.

The reason I cant change the func to take in a NAME* instead of CString* is that i need to call it at other times passing in a CString*.
Ill just have to create a new array of CStrings with the Name1 member of NAME.

Thanks for the help



You could use function overloading....


func( Name *string );
func( int *string );
func( CString *string );

Might also want to check out templates...

"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
[Cyberdrek | ]
I dont know about templates but I dont want to overload the function because I dont want to have to worry about having to update 2 functions for something that I can just create another array for (it will be easier for me to just create the new array rather than have to overload the function and keep both functions updated).

Templates, however, might be a good idea because i think they will let me have the one function and pass in different types of data right?. I dont know much about templates but ill definately have look at that idea. Thanks very much.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
If you don''t want to worry about having to update 2 functions, do something like this:

void Func(Name *pname, int iArray)
{
Func(Name1, iArray);
}
void Func(CString* csArray, int iArray)
{
//do stuff
}

Crude, but works
sorry, should be:

void Func(Name *pname, int iArray)
{
Func(pname->Name1, iArray);
}

This topic is closed to new replies.

Advertisement