|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
PHP Code:
The top function wordarray() should convert a group of words connected by spaces into an array with a word per index (similar to PHP's explode()). In the declaration of that function, I originally put PHP Code:
but that gave me errors, so I used the whole basic_string thing. The variable strWords is the group of words connect by spaces. The variable strWordsArray[64] is the array that will contain the words after going through the function (64 words maximum). The variable pstrWordsArray is a pointer used to point at strWordsArray. I wanted to use a pointer so I could modify the array directly, since I could not return a string array. The errors I received were basically: [Warning] In function `int main(int, char **)': Line 46: conversion from `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > (*)[64]' to non-scalar type `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >' requested I would really appreciate any advice. I'm basically a newbie to C++. |
|
#2
|
|||
|
|||
|
im not really sure about C++ but in C you would do something like:
(this isnt totally coded im just going to half *** it in pseudo-pseudo code )Code:
#includes
main {
char *sentence;
char **words;
get sentence
getWords(sentence,words)
}
getWords(char *sentence, char**words) {
char buffer[buffersize];
for (i <- 0 to length of sentence)
if (sentence[i] not seperator)
append sentence[i] to buffer
else
add buffer to next array slice in words
}
hope thats what you wanted to do and helps, you can fill that in to make it work ![]() If C++ works anything like C, you cant return a pointer from a function unless the pointer was allocated outside a function or you risk having the data destroyed by some other alloc. so you can pass in the pointer for storage. |
|
#3
|
|||
|
|||
|
It seems like the psedo-code you wrote uses C strings which are different than the standard C++ strings (which I am using) which are objects. Testing each character of the string was a way of doing this that I hadn't thought of. My problem really is not that I don't know how to make the inner part of the function work (I worked on it before as a seperate program and it worked fine), but that I do not know how to pass a pointer to an array of objects to a function. The pointer I am talking about was made in main(), but I need to pass it to wordarray(). I am not returning the pointer, but just using the function to modify the array directly by passing a pointer instead of passing a copy of the array. I keep getting those errors that I put in my first original post. Thank you for offering your advice.
|
|
#4
|
|||
|
|||
|
ohhhh
I think i see whats happening try passing the parameteres like this wordarray(string *sentence, string **words) |
|
#5
|
|||
|
|||
|
I tried passing it that way and it gave me an error:
Line 55 (the line with the function call on it) no match for `*basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > &' Why would this work anyway? Could you try to explain using the code I originally posted since I cannot use yours (doesn't use C++ strings and rewrites my already working function body). Thank you for your help. |
|
#6
|
|||
|
|||
|
I figured out how to make it work, here's the final code.
PHP Code:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > problem passing a pointer to an array of objects (strings) to a function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|