SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old January 2nd, 2003, 05:26 PM
mtcx mtcx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 mtcx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy problem passing a pointer to an array of objects (strings) to a function

PHP Code:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>

basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > * wordarray(string strWordsstring(* pstrWordsArray)[64]){
  
string strTemp;
  
string charLast strWords.substr(strWords.length()-1,1);
  
int intIndex 0;     
  
int intSpaces 0;
  
int intLoc 0;
  
  if (
charLast=="?" || charLast=="." || charLast=="!"){
      
strWords.erase(strWords.length()-1);
  }
  while (
intLoc != string::npos){
    
intLoc strWords.find(" "intIndex);
    
intIndex = (intLoc 1);  
    if (
intLoc != -1){
       
intSpaces++;
    }  
  }
  
  
intIndex 0;
  
intLoc 0;
 
  for (
int x=0x<=intSpacesx++){
    
intLoc strWords.find(" "intIndex);
    (*
pstrWordsArray)[x].insert(0strWordsintIndexintLoc);
    
strTemp.insert(0strWordsintIndex+intLoc+1strWords.length()-intIndex+intLoc+1);
    
strWords strTemp;
    
strTemp "\0";
  }
  }
  
int main(int argcchar *argv[])
{
  
  
string strWords;
  
string strWordsArray[64];
  
string(* pstrWordsArray)[64];
  
pstrWordsArray =& strWordsArray;
  
  
wordarray(pstrWordsArraystrWords); // Line 46
  
  
system("PAUSE");    
  return 
0;



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:
 string wordarray(string strWordsstring(* pstrWordsArray)[64]) 


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++.

Reply With Quote
  #2  
Old January 3rd, 2003, 01:16 AM
TEMM TEMM is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 7 TEMM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old January 3rd, 2003, 01:30 PM
mtcx mtcx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 mtcx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old January 3rd, 2003, 03:54 PM
TEMM TEMM is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 7 TEMM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ohhhh

I think i see whats happening

try passing the parameteres like this

wordarray(string *sentence, string **words)

Reply With Quote
  #5  
Old January 3rd, 2003, 04:09 PM
mtcx mtcx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 mtcx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #6  
Old January 4th, 2003, 08:35 PM
mtcx mtcx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 mtcx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I figured out how to make it work, here's the final code.
PHP Code:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>

basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > * wordarray(string strWordsstring *pstrWordsArray){
  
string strTemp;
  
string charLast strWords.substr(strWords.length()-1,1);
  
int intIndex 0;     
  
int intSpaces 0;
  
int intLoc 0;
  
  if (
charLast=="?" || charLast=="." || charLast=="!"){
      
strWords.erase(strWords.length()-1);
  }
  while (
intLoc != string::npos){
    
intLoc strWords.find(" "intIndex);
    
intIndex = (intLoc 1);  
    if (
intLoc != -1){
       
intSpaces++;
    }  
  }
  
  
intIndex 0;
  
intLoc 0;
 
  for (
int x=0x<=intSpacesx++){
    
intLoc strWords.find(" "intIndex);
    
pstrWordsArray[x].insert(0strWordsintIndexintLoc);
    
strTemp.insert(0strWordsintIndex+intLoc+1strWords.length()-intIndex+intLoc+1);
    
strWords strTemp;
    
strTemp "\0";
  }
  }
  
int main(int argcchar *argv[])
{
  
  
string strWords;
  
string strWordsArray[64];
  
string *pstrWordsArray;
  
pstrWordsArray strWordsArray;
  
  
wordarray(strWordspstrWordsArray);
  
  
system("PAUSE");    
  return 
0;


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > problem passing a pointer to an array of objects (strings) to a function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway