Solved

character array

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now