Solved

character array

Posted on 1999-01-02
5
220 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

773 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