c++ and passing arrays (or not?!) .. arg

michaelh20

Senior member
Sep 4, 2000
482
0
0
well I guess, it seems that passing strings is a no no in C++, but can I pass arrays at all period? The compiler (g++) seems to spit chunks when I try to do a function that returns an array. Here is my little proggie:

I was just playing with string stuff and wanted to get a string that was the actual length of the input, not a whole 64 or 32 characters or whatever.

Do I *have* to write this using pointers instead?

//
#include <stdio.h>
#include <iostream.h>
#include <strings.h>

// supposed to take a string as input, and return a string
// with the return string only as long as it needs to be, using
// spaces and null as the delimiter.

char[] chopString(char input[]){
int trueLength=0;
int inputLength = strlen(input);

// Get the length of the string up to the first space or null char
for (int i = 0; i < inputLength; i ++){
if ((input== 0) || (input==32)){ // if space or null char
break;
}
else trueLength++;
}
// declare the array result
char result[trueLength];


// read the characters up to the true length into the dest string
for (int i = 0; i < trueLength; i ++){
result=input;
}
// return the dest string?
return result;
}



int main(int nNumberofArgs, char* pszArgs[])
{
//cout << &quot;This is a test\n&quot;
// << &quot;This is only a test\n&quot;;

// print out the original string and the chopped string and their lengths

char szString1[32]; cout << &quot; x is : &quot; << x;
cout << &quot; Give me a String: &quot;;
cin.getline(szString1,32);
cout << &quot;The String was&quot;;
cout << szString1 << &quot;\n&quot;;
int length = strlen(szString1);
cout << &quot;The length was : &quot; << length;

cout << &quot;CHopping the String \n&quot;;
szString1 = chopString(szString1);
cout << &quot;The String now is :&quot;;
cout << szString1 << &quot;\n&quot;;
int length = strlen(szString1);
cout << &quot;The length now is : &quot; << length;



return 0;
}

Here is what the compiler spits out, it doesn't seem to like the char [] thingee:
-----------------
-------------------------------------------------------------------
hoff0103@superman (~/cstuff) % g++ stringTest.cpp

stringTest.cpp:15: parse error before `['
stringTest.cpp:17: `input' was not declared in this scope
stringTest.cpp:20: parse error before `for'
stringTest.cpp:20: parse error before `;'
stringTest.cpp:20: syntax error before `++'
stringTest.cpp:27: `trueLength' was not declared in this scope
stringTest.cpp:31: parse error before `for'
stringTest.cpp:31: `trueLength' was not declared in this scope
stringTest.cpp:31: parse error before `;'
stringTest.cpp:31: syntax error before `++'
stringTest.cpp: In function `int main(int, char **)':
stringTest.cpp:47: `x' undeclared (first use this function)
stringTest.cpp:47: (Each undeclared identifier is reported only once
stringTest.cpp:47: for each function it appears in.)
stringTest.cpp:56: implicit declaration of function `int chopString(...)'
stringTest.cpp:56: incompatible types in assignment of `int' to `char[32]'
stringTest.cpp:59: redeclaration of `int length'
stringTest.cpp:52: `int length' previously declared here
hoff0103@superman (~/cstuff) % exit

 

Engine

Senior member
Oct 11, 1999
519
0
0
Passing arrays is a biatch... I always had problems with that in college :)

Try declaring the parameter as a pointer instead of an array. I'm pretty sure C++ wants you to pass in a pointer to the beginning of the array. Something like this:

char* chopString( char *input)
{
blah blah...

}

I think that might work. I dunno, I usually just played around with it until it finally compiled :)
 

Cyph3r

Senior member
Jan 20, 2000
790
0
0
Since you are returning a string in your chopString function, simply make that a char* and you'll still be returning a string. This is a better way..

Now to address the real point of your program which is to create a string JUST long enough to hold your text and no more. Use new on the trueLength and return that..This should work fine..Regards
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
Although you are using some of the C++ built in functions like cin and cout, this is really just a C program. To take advantage of C++, you really need to be embracing object oriented logic by creating classes. C++ supports abstraction, inheritance, polymorphism, and encapsulation, which can and should be used to create very powerful object oriented solutions to programming problems... My 2 cents...
 

TGCid

Platinum Member
Oct 9, 1999
2,201
0
0
Try using apstring. It has a lot of addons stuff that really help string manipulation.
 

skriefal

Golden Member
Apr 10, 2000
1,418
3
81
You could also use malloc() to create the string on the heap, and then use realloc() to &quot;chop off&quot; any unneeded space. I think that'll work; it's been a while since I've done much with C/C++. Of course, this would require that you free() the memory when you're finished.

Alternatively, doesn't the C++ Standard Library (aka &quot;STL&quot;) provide a string class that would make this even easier? i.e.:

string myString;
cin >> myString;

... and then you don't have to worry about the length of the string at all.
 

michaelh20

Senior member
Sep 4, 2000
482
0
0
Well that wouldn't be any fun now, would it?

Also, where can I get the documentation on the standard libraries?? Do have to buy some compiler with the manual or something? Or is there documentation on the web somewhere? Would think so, but can't seem to find it.
 

michaelh20

Senior member
Sep 4, 2000
482
0
0
ok so now we have :
which uses pointers now, also prints out the ascii numbers associated
with the values.

=============

//
// A blank template
//
//
//
//
#include <stdio.h>
#include <iostream.h>
#include <strings.h>

// supposed to take a string as input, and return a string
// with the return string only as long as it needs to be, using
// spaces and null as the delimiter.

char* chopString(char input[]){
int trueLength=0;
int inputLength = strlen(input);

// Get the length of the string up to the first space or null char
for (int i = 0; i < inputLength; i ++){
if ((input== 0) || (input==32)){ // if space or null char
break;
}
else trueLength++;
}
// declare the array result (+ 1 for null character)
char* result;
result = new char[trueLength+1];


// read the characters up to the true length into the dest string
for (int i = 0; i < trueLength; i ++){
result=input;
}
// null terminate the string doh!
*(result +trueLength)='\0';

// return the dest string? // aka string pointer
return result;
}



int main(int nNumberofArgs, char* pszArgs[])
{
//cout << &quot;This is a test\n&quot;
// << &quot;This is only a test\n&quot;;

// print out the original string and the chopped string and their lengths

char szString1[32]={&quot;\0&quot;};
szString1[31]='\0';
cout << &quot; Give me a String: &quot;;
cin.getline(szString1,32);
cout << &quot;The String was&quot;;
cout << szString1 << &quot;\n&quot;;
int length = strlen(szString1);
cout << &quot;The length was : &quot; << length;
cout << &quot;\n&quot;;

for (char* i = &amp;szString1[0]; i< &amp;szString1[0]+31; i ++){
cout << (int)*i << &quot; &quot;;
}

cout << &quot;\n&quot;;



cout << &quot;CHopping the String \n&quot;;
char* result = chopString(szString1);
cout << &quot;The String now is : &quot;;
cout << result << &quot;\n&quot;;
length = strlen(result);
cout << &quot;The length now is : &quot; << length << &quot;\n&quot;;

for (char* i = result; i< result+31; i ++){
cout << (int)*i << &quot; &quot;;
}

cout << &quot;\n&quot;;



return 0;
}
========
output:
hoff0103@superman (~/cstuff) % ./a.out
Give me a String: arg
The String wasarg
The length was : 3
97 114 103 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CHopping the String
The String now is : arg
The length now is : 3
97 114 103 0 0 0 0 0 0 0 0 8 0 0 0 0 0 3 -57 -56 0 0 0 0 0 0 0 8 0 0 0
hoff0103@superman (~/cstuff) %
=====

Sooo, ok now there is something obvious here, the string length thingee doesn't give a whisker about where the array is, just where a zero is. Now here's a question, if I wanted to turn this pointer to the array was stored on the heap (right? -- maybe?) back into a real array, is there a way to do that, minus the obvious declaring of a new array and reading all the values up to zero into the new array?



Also: need function to find length of an Array.
Also: is there a &quot;array pointer&quot; like *result[32] or *result[] instead of just a pointer to a member in the array like char* or int* ?
also also: can I get it to write something other than a.out?? Or must I always rename the thing?

Also Also Also: if just for the sake of it, I was going to use this function in a real program, I would have to use the new &quot;string&quot; and then destroy it, right? Anyway to accomplish this without having to always destroy the thing outside of the function that created it?

Nothing serious, I am just playing with pointers before going to classes, etc...

Arg.. why can't I just pass an array? :)

Thanks....
 

michaelh20

Senior member
Sep 4, 2000
482
0
0
sorry to bug you guys as I read this book, but is there such a thing as a project file with GNU C++? Just a text file? Nada?
 

Engine

Senior member
Oct 11, 1999
519
0
0
Since you're using g++ instead of MS Visual C++, I don't know if you'll actually have the libraries. I would think so, since they're the standard classes instead of the MFC ones.

Anyway, there's a bunch of info on the standard libraries here.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< Now here's a question, if I wanted to turn this pointer to the array was stored on the heap (right? -- maybe?) back into a real array, is there a way to do that, minus the obvious declaring of a new array and reading all the values up to zero into the new array? >>



nope, once on a heap always on a heap, until you deallocate it or OS does it for you.



<< Also: need function to find length of an Array. >>



you declared it so you should know its size :) if you are talking about strings you can use strlen(const char*) function in stdlib, simply include <string.h>



<< Also: is there a &quot;array pointer&quot; like *result[32] or *result[] instead of just a pointer to a member in the array like char* or int* ? >>



char *result[32]; decalres an array of 32 pointers to type char* (string if you like)
char *result[] is not a legal declaration ( you have to allocate size ), if you don't know it ahead of the time simply declare it as char**
result; and later when you ready to allocate memory for it simply allocate appropriate size - result = new char*[32]; for example.

Also, back to your original question, C/C++ does not allow arrays ( nor functions ) to be specified as return types ( sorry ). Simply return a pointer to either an array of function...


char[32] return_char(...) // WRONG
char* return_char(...) // OK




<< also also: can I get it to write something other than a.out?? Or must I always rename the thing? >>



g++ -o nameofyourprogram source.cxx

writes out binary nameofyourprogram intead of a.out



<< Also Also Also: if just for the sake of it, I was going to use this function in a real program, I would have to use the new &quot;string&quot; and then destroy it, right? >>



Yes, although if you don't clean up after yourself and the application terminates, the OS ( assuming it's a UNIX clone ( including Linux ) will clean up after you. Windows is not that clever :)



<< anyway to accomplish this without having to always destroy the thing outside of the function that created it? >>



declare it in a caller function, pass it in to allocate and then destroy it...


char *allocate_io(char *io)
{
io = new char[32]; // allocated
return io;
}

int main( )
{
char *_io = allocate_io(_io); // decalred
delete [] _io; // destroyed
}


_io is decalred and destroyed within the scope of main(...), but it's allocated in allocate_io(...).
 

michaelh20

Senior member
Sep 4, 2000
482
0
0
Thanks, I will probably save that for future reference :)

C++ for Dummies doesn't quite go into that detail :)