LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-03-2006, 01:51 AM   #1
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Rep: Reputation: 30
C++ Return Char Arrays (not with pointers)


Hello, I would like to return an array of chars from a function so I can read/write to it. I cannot send the original array and edit it as a pointer, because I need to create a new array with some modifications in my program.

Code:
void main()
{
	int size = 10;
	char str [ size ];

	char newStr[ size ];
	*newStr = copyArray (str, size);
}

char* copyArray( const char str[], const unsigned int size)
{
	char newStr[ size ] = "\0";
	
	for(unsigned int i=0; i < size; i++) // copy the array
	{
		newStr[i]=str[i];
		// other modifications to the new array
	}
		
	return *newStr;
}
I know I have the pointers messed up with that code example, but that's the best I know. I want to be able to use the newStr array in main, how can I have it actually send a copy of the array in a way that won't be deleted?
 
Old 02-03-2006, 03:54 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Typically to do this you would pass the original and the new string by reference (similar to how strncpy or any of the other string.h functions work).

Here is a quick example I cooked up (using your example). This is hardly "good" code, just threw it together real quick...

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void copyArray( const char str[], const unsigned int size, char *newStr)
{
    for(unsigned int i=0; i < size; i++) // copy the array
    {
        newStr[i]=str[i];
        // other modifications to the new array
        newStr[i]-=32;
    }
}

void printArray (char str[], int len)
{
    for (int i = 0; i < len; i++)
    {
        printf ("%c",str[i]);
    }
    printf ("\n");
}

int main()
{
    int size = 10;
    char str [ size ];
    strncpy(str,"abcdefghij",size);

    char newStr[ size ];
    memset(newStr,0,size);
    copyArray (str, size, newStr);
    printf ("Original = ");
    printArray(str,size);
    printf ("New = ");
    printArray(newStr,size);

    return 0;
}
Just to illustrate that they are different I subtracted 32 from each char in the original... providing they are all lowercase letters (like in the example) this effectively changes them to upper case.
 
Old 02-03-2006, 12:44 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Standard C++ explicitly prohibits functions from returning arrays; you'll need to use a pointer or a class which embeds an array. Is the problem that you don't want to use heap memory (i.e. new[])?
ta0kira

Last edited by ta0kira; 02-04-2006 at 05:30 PM.
 
Old 02-03-2006, 05:39 PM   #4
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 171
Blog Entries: 1

Rep: Reputation: 19
Quote:
Originally Posted by sadarax
Hello, I would like to return an array of chars from a function so I can read/write to it. I cannot send the original array and edit it as a pointer, because I need to create a new array with some modifications in my program.

Code:
void main()
{
	int size = 10;
	char str [ size ];

	char newStr[ size ];
	*newStr = copyArray (str, size);
}

char* copyArray( const char str[], const unsigned int size)
{
	char newStr[ size ] = "\0";
	
	for(unsigned int i=0; i < size; i++) // copy the array
	{
		newStr[i]=str[i];
		// other modifications to the new array
	}
		
	return *newStr;
}
I know I have the pointers messed up with that code example, but that's the best I know. I want to be able to use the newStr array in main, how can I have it actually send a copy of the array in a way that won't be deleted?

OK, here's your basic problem: In your example, you have defined the char array newStr within the main function, and then within your copyArray() function, you define another, local char array with the same name. This is NOT the same char array. The array defined within copyArray() is locally defined, i.e, it exists on the stack, so when copyArray() returns to main, its memory allocation is released, and its contents may be overwritten. To correct this, the array must be defined within main or outside a function, in the common area. Here's one way to do this:

Code:
void main()
{
	int size = 10;
	char str [ size ];

	char newStr[ size ];
	copyArray (newStr, str, size);
}

copyArray(char newStr[], const char str[], const unsigned int size)
{
	
	for(unsigned int i=0; i < size; i++) // copy the array
	{
		newStr[i]=str[i];
		// other modifications to the new array
	}
		
	return;
}
 
Old 02-03-2006, 05:49 PM   #5
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Original Poster
Rep: Reputation: 30
Thank you all for your replies. Now that I see what you mention, it makes perfect sense and I am surprised I did not see it before (based on what I know about parameters and pointers).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 09:24 AM
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 11:06 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 06:13 PM
Comparing Char arrays n_ick2000 Programming 5 02-25-2003 10:02 AM
C Question: arrays and pointers tundra Programming 7 07-19-2002 04:38 AM


All times are GMT -5. The time now is 07:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration