Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 30th, 2005, 06:05 PM
Jordan Tiona
Guest
 
Posts: n/a
Default Returning an Array a characters?

Is this not allowed in C++? I try in MSVC, but I always get an error, and
the function no longer shows up in my class view.


  #2  
Old December 30th, 2005, 06:15 PM
roberts.noah@gmail.com
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?


Jordan Tiona wrote:[color=blue]
> Is this not allowed in C++? I try in MSVC, but I always get an error, and
> the function no longer shows up in my class view.[/color]

Doesn't compute....need more input.

  #3  
Old December 30th, 2005, 06:15 PM
mlimber
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Jordan Tiona wrote:[color=blue]
> Is this not allowed in C++? I try in MSVC, but I always get an error, and
> the function no longer shows up in my class view.[/color]

Arrays are evil. Use a std::vector or std::string instead.

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Cheers! --M

  #4  
Old December 30th, 2005, 09:05 PM
David Harmon
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

On Fri, 30 Dec 2005 10:59:07 -0700 in comp.lang.c++, "Jordan Tiona"
<torahteen@comcast.net> wrote,[color=blue]
>Is this not allowed in C++?[/color]

It is not, nor is passing an array by value as a parameter.
I think the reason was, if that was allowed you would probably do a
lot of unnecessary copying of huge data without realizing the
mistake.

There are better ways, especially in C++ as compared to old C. What
are you trying to accomplish?
  #5  
Old December 31st, 2005, 03:35 PM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?


Jordan Tiona wrote:[color=blue]
> Is this not allowed in C++? I try in MSVC, but I always get an error, and
> the function no longer shows up in my class view.[/color]

In C and C++ you can not return an array by value. To do this you need
to wrap it in a struct:

struct arr { int value[0x20]; };
arr foo();

Depending on your task returning large objects by value may be costly.

  #6  
Old December 31st, 2005, 04:05 PM
Martin Schall
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Jordan Tiona schrieb:[color=blue]
> Is this not allowed in C++? I try in MSVC, but I always get an error, and
> the function no longer shows up in my class view.
>
>[/color]

Hello,
you can return a pointer to an array.

Example:
int* testfunction()
{
int array[3] = {1,2,3};
return array;
}

int main()
{
int* array = testfunction();
/* now access the array with *(array+index), for example *(array+0) is 1
in this example */
return 0;
}

I hope, this will help you. But returning 'real' arrays in C or C++ is
not possible.

Martin
  #7  
Old December 31st, 2005, 04:45 PM
Gavin Deane
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?


Martin Schall wrote:
[color=blue]
> Jordan Tiona schrieb:[color=green]
> > Is this not allowed in C++? I try in MSVC, but I always get an error, and
> > the function no longer shows up in my class view.
> >
> >[/color]
>
> Hello,
> you can return a pointer to an array.[/color]

There's a big difference between what you *can* do and what you
*should* do.
[color=blue]
> Example:
> int* testfunction()
> {
> int array[3] = {1,2,3};
> return array;[/color]

warning C4172: returning address of local variable or temporary

array is a local variable. It has automatic storage duration. After
this function returns, array is destroyed.
[color=blue]
> }
>
> int main()
> {
> int* array = testfunction();
> /* now access the array with *(array+index), for example *(array+0) is 1
> in this example */[/color]

You can't access the array because it doesn't exist any more. Any
attempt to do so invokes undefined behaviour.
[color=blue]
> return 0;
> }
>
> I hope, this will help you.[/color]

It won't.
[color=blue]
> But returning 'real' arrays in C or C++ is
> not possible.[/color]

Perhaps you meant to dynamically allocate the array within test
function.

int* p = new int[3];
return p;

This works. But the caller is now responsible for calling delete [] on
this pointer, whcih makes this approach rather brittle. Other posts in
this thread have suggested better solutions.

Gavin Deane

  #8  
Old January 1st, 2006, 04:25 PM
Martin Schall
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Gavin Deane schrieb:[color=blue]
> Martin Schall wrote:
>
>[color=green]
>>Jordan Tiona schrieb:
>>[color=darkred]
>>>Is this not allowed in C++? I try in MSVC, but I always get an error, and
>>>the function no longer shows up in my class view.
>>>
>>>[/color]
>>
>>Hello,
>>you can return a pointer to an array.[/color]
>
>
> There's a big difference between what you *can* do and what you
> *should* do.
>
>[color=green]
>>Example:
>>int* testfunction()
>>{
>>int array[3] = {1,2,3};
>>return array;[/color]
>
>
> warning C4172: returning address of local variable or temporary
>
> array is a local variable. It has automatic storage duration. After
> this function returns, array is destroyed.
>
>[color=green]
>>}
>>
>>int main()
>>{
>>int* array = testfunction();
>>/* now access the array with *(array+index), for example *(array+0) is 1
>>in this example */[/color]
>
>
> You can't access the array because it doesn't exist any more. Any
> attempt to do so invokes undefined behaviour.
>
>[color=green]
>>return 0;
>>}
>>
>>I hope, this will help you.[/color]
>
>
> It won't.
>
>[color=green]
>>But returning 'real' arrays in C or C++ is
>>not possible.[/color]
>
>
> Perhaps you meant to dynamically allocate the array within test
> function.
>
> int* p = new int[3];
> return p;
>
> This works. But the caller is now responsible for calling delete [] on
> this pointer, whcih makes this approach rather brittle. Other posts in
> this thread have suggested better solutions.
>
> Gavin Deane
>[/color]

Hello,
sorry, i didn't see the problems. But thanks for the explanaition. I'll
think about it next time.

Martin
  #9  
Old January 2nd, 2006, 03:05 AM
Jordan Tiona
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Ok, I'm sorry for not replying. I keep having problems. This is for a
highschool project, and I have to use dynamic data to store a collection of
CD data. My linked list is not working. I am getting tons of errors and I
don't know what to make of them. Here is my code

cddb.h:

#include <iostream>
#include <stdio.h>
#include <string.h>

class CDData {
public:
//Constructor/Destructor
CDData(char newName[], char newArtist[], int newYear);
CDData(){}
~CDData(){};
//Accessors
string GetName(){return name;}
string GetArtist(){return artist;}
int GetYear(){return year;}
void SetName(char newName[]){name = newName;}
void SetArtist(char newName[]){artist = newName;}
void SetYear(int newYear){year = newYear;}
private:
string name; //CD Name
string artist; //Artist's Name
int year; //Year released
};

class LinkedList{
public:
//Constructor/Destructor
LinkedList(){hNode->SetNext(NULL);
hNode->SetData(NULL);}
~LinkedList();
//Accessors
void Add(CDData* data);
int Delete(char match[]);
int Delete(int match);

private:
Node* hNode; //Head Node
}

class Node{
public:
//Constructor/Destructor
Node(){}
~Node(){delete data;}
//Accessors
Node* GetNext(){return next;}
CDData* GetData(){return data;}
void SetNext(Node* newNext){next = newNext;}
void SetData(CDData* newData){data = newData;}
private:
Node* next;
CDData* data;
}

cddb.cpp:
#include "cddb.h"
using namespace std;

int main(){
return 1;
}
void LinkedList::Add(CDData *data){
Node* curNode;
Node* newNode;
//Adds a new node to the linked list
/* 1. Check to see if this is the first node
1a. If so, then make new node, make hNode point to new node.
Set curNode to the new Node. Set curNode's data by calling
curNode->SetData(data).
1b. If not, then iterate through nodes using curNode->GetNext()
until curNode->GetNext() returns NULL. Create new node, setting
curNode->next to the new node. Set curNode to new node, then set
curNode's data to the appropriate data. Set curNode's next to NULL
using curNode->SetNext(NULL)
*/

if(hNode->GetNext() == NULL){
//This is the first node
newNode = new Node;
hNode->SetNext(newNode);

curNode = newNode;
curNode->SetData(data);
}
else {
//This is not the first node
bool quit = false;
curNode = hNode->GetNext();

while(quit == false){
if(curNode->GetNext() == NULL){
newNode = new Node;
curNode->SetNext(newNode);
curNode = newNode;
curNode->SetData(data);
curNode->SetNext(NULL);
quit = true;
}
else {
curNode = curNode->GetNext();
}
}
}
}

bool LinkedList::Delete(char match[]){
CDData* curData;
Node* curNode;
Node* prevNode;
bool quit = false;

//Deletes the node who's name matches "match".
/*Using a while statement:
1. Iterate to next node. Using the curNode->next->GetData() function,
set curData to curNode's data. Then, using curData->GetName(),
see if match[] matches curData's name.
2. If it does, then we continue to number 3, else, go back to 1.
3. To delete a node, we need to set curData's next to curData->next->next.
Then we delete curData. Simple.
*/

curNode = hNode->GetNext();
if(curNode == NULL) //If there are no nodes, then return an error
return false;

prevNode = hNode;

while(quit == false){
if(curNode == NULL) //If curNode is NULL, then we didn't find it
return 0;
if(curNode->GetData() != NULL) //If data isn't NULL
curData = curNode->GetData();

if(!strcmp(curData->GetName(), match)){
//We have a match, now we delete.
prevNode->SetNext(curNode->GetNext());
delete curNode;
return 1;
}
else{
//No match, we must iterate to next node
prevNode = curNode;
curNode = curNode->GetNext();
}
}
}

int LinkedList::Delete(int match){
//Same as other function, only comparing years

CDData* curData;
Node* curNode;
Node* prevNode;
bool quit = false;

curNode = hNode->GetNext();
if(curNode == NULL) //If there are no nodes, then return an error
return false;

prevNode = hNode;

while(quit == false){
if(curNode == NULL) //If curNode is NULL, then we didn't find it
return 0;
if(curNode->GetData() != NULL) //If data isn't NULL
curData = curNode->GetData();

if(curData->GetYear() == match){
//We have a match, now we delete.
prevNode->SetNext(curNode->GetNext());
delete curNode;
return 1;
}
else{
//No match, we must iterate to next node
prevNode = curNode;
curNode = curNode->GetNext();
}
}
}

I'm sorry if these are really simple errors, but this needs to get done.


  #10  
Old January 2nd, 2006, 08:35 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

"Jordan Tiona" <torahteen@comcast.net> wrote in message
news:E-WdnS_pD4zw6CjeRVn-hw@comcast.com...[color=blue]
> Is this not allowed in C++? I try in MSVC, but I always get an error, and
> the function no longer shows up in my class view.[/color]

Correct. You can not return an array. Although for your "array of
character" you should probably return a std::string instead.

std::string MyFunc( )
{
std::string ReturnString = "Return This";
return ReturnString;
}

You could wrap the char array in a structure and return that.

struct ReturnStruct
{
char CharString[100];
};

ReturnStruct MyFunc( )
{
ReturnStruct ReturnString;
strcpy( ReturnString.CharString, "Return This" );
return ReturnString;
}

You could allocate the char array with new and return a pointer to it and
remember to delete it later.

char* MyFunc( )
{
char* ReturnString = new char[100];
strcpy( ReturnString, "Return This" );
return ReturnString;
}

You do not want to return a char pointer to a char array local to the
function, as it'll be destroyed when the function returns. Do NOT do this:

char* MyBadFunc( )
{
char ReturnString[100];
strcpy( ReturnString, "Return This" );
return ReturnString;
}

The reason you don't want to do that:

int main()
{
char* SomeString = MyBadFunc();
std::cout << SomeString << std::endl; // The array has been destoryed by
now
}

HTH



  #11  
Old January 6th, 2006, 06:35 PM
David Harmon
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

On Sun, 1 Jan 2006 19:58:09 -0700 in comp.lang.c++, "Jordan Tiona"
<torahteen@comcast.net> wrote,[color=blue]
>Ok, I'm sorry for not replying. I keep having problems. This is for a
>highschool project, and I have to use dynamic data to store a collection of
>CD data. My linked list is not working.[/color]

And then it got lost in the older thread; sorry about that.

If your project is mainly to learn how to implement linked lists and
so forth, then you can proceed in that direction. In which case,
you will have to get really clear on what is an array, what is a
pointer, and what is something else. And you will have to fix all
of those problems one-by-one.

But, if you mainly want your program to work, and to learn good C++
practices, then all of your linked list stuff should surely be done
by std::list (or perhaps some other library container), and all of
your string stuff should surely be done by std::string, and never a
char* appear in application-level code.

So if you even see this, my question is still "what are you trying
to accomplish?"

  #12  
Old January 9th, 2006, 04:55 PM
Jordan Tiona
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Please?


  #13  
Old January 13th, 2006, 05:45 AM
Jordan Tiona
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Any possible help?


  #14  
Old January 13th, 2006, 06:35 PM
Default User
Guest
 
Posts: n/a
Default Re: Returning an Array a characters?

Jordan Tiona wrote:
[color=blue]
> Any possible help?[/color]


With what?


Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles