Solved

character array

Posted on 1999-01-02
5
223 Views
Last Modified: 2013-12-03
I'm having problem parsing an array of characters into a function and then
retreiving the character array after the function has completed.
Here is a sample example of what I am trying to do with ??? beside my problems..

I need my character array to be changed in the function.. but I'm not
sure how to do it..

do i need to use pointers and &'s or something?

Here's the example:

I have a class..

class Fred
{

public:

???      int billybob(char ?????);

};


****
main.cpp

int main() {


      Fred fred = new Fred();

      char anArrayOfChars[100];
      strcpy(anArrayOfChars, "hello, world!");

???      fred->billyBob(anArrayOfChars????);

      printf("%s\n", anArrayOfChars);

      return 0;

}
0
Comment
Question by:mramsay
  • 2
  • 2
5 Comments
 

Author Comment

by:mramsay
ID: 1181416
anArrayOf characters will be changed in the function.. so it should'nt print out hello world
how do u do it
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1181417
One thing to remember: an array is passed to a function as a pointer.  Therefore:

    class Fred
    {
    public:
        int billybob(char* array);
    };

and:

    fred->billyBob(anArrayOfChars);

You could use an array designation:

    class Fred
    {
    public:
        int billybob(char array[]);
    };

It is more readable but the information about the size of the array is lost anyway.  This is one of the "features" C++ inherited from C.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1181418
Here is exactly what you wanna do...

class Fred
   {
   public:

      void billyBob(char *str) {
         //The code for the function, if it's not too big...
      }
   };

   int main()
  {


      Fred fred;

    char anArrayOfChars[100];
    strcpy(anArrayOfChars, "hello, world!");

    fred.billyBob(anArrayOfChars);

   printf("%s\n", anArrayOfChars);

   return 0;

   }

Hope that helps...

-Viktor
--Ivanov
0
 

Author Comment

by:mramsay
ID: 1181419
Ok I'll try that out.

I noticed that Viktor typed:

Fred fred
and used
fred.billyBob

why do I see most classes declared like

Fred *fred
and used like
fred->billyBob

what is the difference?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1181420
The difference is that the one I used is a variable that the compiler allocates and deallocats the memory for, and the on you use is a pointer and you can allocate and deallocate the memory..

Using a pointer instead of a whole structures saves some memory...

Here is how to do it with a pointer also...

class Fred
        {
        public:

           void billyBob(char *str) {
              //The code for the function, if it's not too big...
           }
        };

        int main()
       {
           Fred *fred = new Fred;  //Declares a pointer to a class and allocates enough memory..

         char anArrayOfChars[100];
         strcpy(anArrayOfChars, "hello, world!");

         fred->billyBob(anArrayOfChars);  //When it's apointer you use a -> instead of . to access member functions or variables...

        printf("%s\n", anArrayOfChars);
        delete fred; //deallocate memory (free it)
        return 0;
        }

Hope this helps... If you need more info let me know...

-Viktor
--Ivanov
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question