#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep 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++.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    7
    Rep 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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep 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.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    7
    Rep Power
    0
    ohhhh

    I think i see whats happening

    try passing the parameteres like this

    wordarray(string *sentence, string **words)
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep 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.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep 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;


IMN logo majestic logo threadwatch logo seochat tools logo