Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning vectors 2

Status
Not open for further replies.

Maurader

Technical User
May 8, 2002
59
0
0
CA
Is it possible to return a vector from a function?
if so, what is the syntax?
 
also, is it possible to return a char array? again, i can't seem to find the syntax...

thanks!
 
To returning char array,you just have to proceed like this:

const int size;
char *function( .... )
{
char array[size];
....
...
return array;
}


 
does the declaration of size have to be outside the function definition?
 
no it doesn't have to be outside.
and by the way,you aren't forced to use these declaration "const int size".

so instead,you can do:

char *function( .... )
{
char array[15]; // or any other posive int number
....
...
return array;
}

or

char *function( .... )
{
char *array = new char[15];
...
.....
return array;
delete array;
}



 
i think it's not right:
char *function( .... )
{
char *array = new char[15];
...
.....
return array; //*** here, function ends and
delete array; //*** delete does not work,
//*** end of block delete array(?)
}
 
Ok,you are right.It should have been something like this instead;

char *array = new char[15];
char *function( char* , ..... )
{
array = "Hello world !"; // initialisation example
...
.....
return array;
}

......
....
....

delete array;


 
You are really confusing people Leibnitz!

First, you return a local variable, which gets toatlly destroyed after leaving the program body where got declared!

But, you've corrected that.

I'll pass the second error of trying to do anything after returning from a function.

Third, you allocate an array dynamically and you assign to it a const pointer???? Not that the program would crash if you do that... but is the next step comparing strings with equal sign?? Nevermind the fact that you return from the function a GLOBAL VARIABLE!!! Why bother?

I don't have anything personal with you, but, if you decide to post, be sure first you know what you are doing. If you are not sure, please test your replies first, or else, you have the chance of confusing innocent beginners and give them awful time.

-----------------------------------------------------------
To finally answer Mauraders' question:
to understand what is going on, be sure first you familiarize yourself with pointers as they are the only way of returning a vector from an array.

See my previous reply to you.
How does a pointer to an int translate into vector? Well, pointers just point at memory areas. You then can parse that memory area, but you have to be carefull not to cross vector's boundaries.

To get to my last example, here it is, completed:

Code:
int *returnVector(int &dimension) {
  dimension = 100;
  int *ret = new int[dimension];
  return ret;
};

void main()
{  int dim,i;
   int *vec = returnVector(dim);
   for (i = 0; i < dim; i++)
	printf(&quot;vec[%d] = %d\n&quot;,i,vec[i]);
   getch();
}

HTH [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Well Mr Nosferatu,i'm really sorry for you if you think that i'm confusing people because my purpose is not to confuse people and by the way,must of the time i always check what i'm posting .I had try the last code that i have post with my compiler (Visual c++ 6.0 professionnal version) and it did not crash,there were no warnings no mistakes.(pretty strange !)If you want to make some correction, go and make it but dont try to crush people by the same time,we all make mistakes.

Now lets take your code:
what happened to the memory that you allocate to to the dynamical array &quot;ret&quot; (no deleting after) i hope that you dont use a lot of those variables in a program,because it would be awful.

second part,what is the purpose of displaying and array that you have not initialise before ?

for (i = 0; i < dim; i++)
printf(&quot;vec[%d] = %d\n&quot;,i,vec);

&quot;vec&quot; have never been initialised.
And also,this is a C++ forum,why using &quot;printf&quot; instead of &quot;cout&quot; ?

Are you sure that you know what you are doing ?
Have you check what you are posting ?
 
Riiight... indeed, the code posted had the glithces you mentioned. My mistake, regarding displaying the unitilialized vector and forgotting the deletion of the array.
But anyway, lets comment on YOUR posts, again, because I see that you don't seem to understand your wrong deeds.

1. printf is a ANSI C function that can be used anywhere, anytime in a C or C++ code with no problems.

2. If you say that when returning from a function a statically declared variable, within that function and get NO ERRORS, than I will quit my job and start learning archeology.
Believe me, Visual C++ PROFESSIONAL does not imply your code gets written proffesionally, by default.

3. You really don'y know what
char *array = new char[strlen(&quot;What Am I Doing Here???&quot;)+1];
array = &quot;What Am I Doing Here???&quot;;
does?
The question string is a
const char[whatever_length_that_is]
and you are making there a pointer assign for which you shouldn't even allocate memory.

Please document yourself on pointers and modificators before making any more confusing posts and criticize others which DO know what they're doing.

To conclude, the final code with the modifications (thx Newton):

Code:
int *returnVector(int &dimension) {
  dimension = 100;
  int *ret = new int[dimension];
  int i;
  for (i = 0; i < dimension; i++)
	  ret[i] = i;
  return ret;
};


void main()
{	int dim,i;
	int *vec = returnVector(dim);
	for (i = 0; i < dim; i++)
		printf(&quot;vec[%d] = %d\n&quot;,i,vec[i]);
	getch();
        delete vec;
}
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Well,Well... why dont you try the code yourself ???
So then you will report to me what ERRORS that you are having.It's true i did not need a dynamic array here but this is not causing the program to CRASH or anything else like it.

&quot;What Am I Doing Here???&quot; &quot;It's Not Of Your Business !!&quot;

WHAT ARE YOU DOING HERE ???
ONLY DISTURBING AND CRITICISING PEOPLE.

&quot;CRITICISING PEOPLE&quot; you like it,dont you ?
You keep comming back on people mistakes.
It strange to say that,but you are making a lot of mistakes yourself. So why criticising ? Just for fun ?


( printf is a ANSI C function that can be used anywhere, anytime in a C or C++ code with no problems. )
Ok,in your first post you were talking about beginners.
Dont you know that some of them that learns C++ for the first time might not know anything about C.
So in that case,using expression such as &quot;printf&quot; and &quot;getch()&quot; could be VERY CONFUSING for them.
Since we are in a C++ forum here it will be more convenient to use only C++ expression only.

One last thing,i hope that if it happens that i get people confused with my posts that people who are only directly concerned with it notify me.In that case i will more than happy to clarify.Because having people who are not directly
concerned with your post starting to CRITICISE you can be very frustrating.

 
Whew! Ok...
E:\tests\123\123.cpp(14) : warning C4172: returning address of local variable or temporary

You're right a bit about printf and C++, but ANYBODY that has access to C++ will surely find what printf() means.

This is getting trivial, so I'll quit for now.
Oh... &quot;What am I doing here&quot; was about the actual assignment that was taking place there, it was not a question for you.

Best regards.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Ok i'm really sorry for the &quot;what are you doing here ?&quot;
and despite of the &quot;printf&quot; and &quot;getch()&quot; the last code that you have post is good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top